# Line Location Distance.

#### Zergleb

Level 4
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.

JASS:
``````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
endfunction

function 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.

Last edited:

#### Element of Water

Level 18
Might be better (and more efficient since no GetLocationX/Y) if you make them take coordinates rather than locations, but other than that, I like it!

#### PurplePoot

Level 40
Locations are deprecated whenever you can possibly avoid them... look into using raw coordinates.

Replies
19
Views
2K
Replies
5
Views
527
Replies
4
Views
862
Replies
1
Views
741
Replies
5
Views
600