I was just working on my map and made some of these up. Thought I might as well post them if someone else wanted them and didn't know how to make them.
What this does is, the bottom function takes 3 locations, the 1st two form a line, and then it tells you how far the 3rd point is from the line that the 1st and 2nd make.
The above function finds which location along the line that the 1st and 2nd location make is nearest to the 3rd location.
function GetClosestLocation
takes location A,
location B,
location P
returns location local location AP = Location
(GetLocationX
(P
) - GetLocationX
(A
), GetLocationY
(P
) - GetLocationY
(A
)) local location AB = Location
(GetLocationX
(B
)- GetLocationX
(A
), GetLocationY
(B
) - GetLocationY
(A
)) local location LocReturn
local real ab2 =
(GetLocationX
(AB
) * GetLocationX
(AB
)) +
(GetLocationY
(AB
) * GetLocationY
(AB
)) local real t =
((GetLocationX
(AP
) * GetLocationX
(AB
)) +
(GetLocationY
(AP
) * GetLocationY
(AB
)))/ab2
if t < 0.0
then set t = 0.0
elseif t > 1.0
then set t = 1.0
endif call MoveLocation
(AB, GetLocationX
(AB
) * t, GetLocationY
(AB
) * t
) set LocReturn = Location
(GetLocationX
(A
) + GetLocationX
(AB
), GetLocationY
(A
) + GetLocationY
(AB
)) call RemoveLocation
(AP
) call RemoveLocation
(AB
) set AP =
null set AB =
null return LocReturn
endfunctionfunction LineLocationDistance
takes location A,
location B,
location P
returns real local location ClosestPoint = GetClosestLocation
(A,B,P
) local real ReturnThis = DistanceBetweenPoints
(P, ClosestPoint
) call RemoveLocation
(ClosestPoint
) set ClosestPoint =
null return ReturnThis
endfunction
Now this isn't the version I actually use. Because I have made a VectorLoc struct in vJass to do this. So to get rid of the leak I would change LocReturn to a global variable.
I posted it this way so they could understand my explanation.