I'm currently writing a script that determines the shortest distance from a point to a line created by two vector points. This script is going to be used in a graphics program in order to do bezier clipping, so it'll be executed thousands of times.
Due to this requirement, I'm trying to utilize a normalized line between two points so that calculating distance won't be so taxing on the user. My problem, is that my algorithm doesn't seem to work despite my efforts to fix it.
CODE
for i=0,4 do
p0:Set(math.random(-100,100),math.random(-100,100))
p1:Set(math.random(-100,100),math.random(-100,100))
v1:Set(math.random(-100,100),math.random(-100,100))
local NormVec = p1-p0 --determine difference between 2 vectors
NormVec:NormMe() --normalizes vector
local slope = NormVec.y / NormVec.x
local YIntercept = p0.y-slope*p0.x
local Dist = math.abs(NormVec.y*v1.x + NormVec.x*v1.y +
YIntercept)
end
In this code, I define some random vectors p0,p1,v1. I then determine a line between p0 and p1. I normalize the line. I then find the slope and use that to find the YIntercept. I finally plug this into the formula for distance for a normalized curve, but my results are always wrong.
Info on the Perpendicular Distance can be found on the following link: A mathematics website regarding relevant equations