• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Solved] Distance between point and line

Status
Not open for further replies.
Level 10
Joined
Jun 6, 2007
Messages
392
I've been trying to make a function that calculates the distance between a point and a line. I know one point from a line, let's call it (xl, yl), and the point whose distance I need to calculate is (xp, yp). I also know the angle of the line (just called angle). So the equation of the line is
y-yl = tan (angle) * (x-xl), in normal form

-tan (angle) * x + y - yl + tan (angle) * xl = 0

The distance between line and point can be calculated with formula

|a*x0 + b*y0 + c| / sqrt (a^2 + b^2)

in this case:

|-tan (angle) * xp + yp - yl + tan (angle) * xl| / sqrt ( (tan (angle))^2 + 1^2))

Here's my function:
JASS:
    function DistancePointLine takes location pointInLine, location pointToCheck, real angle returns real
        local real xl = GetLocationX (pointInLine)
        local real yl = GetLocationY (pointInLine)
        local real xp = GetLocationX (pointToCheck)
        local real yp = GetLocationY (pointToCheck)
        local real a = Tan (angle)

        return RAbsBJ ((-a*xp + yp + a*xl - yl) / SquareRoot (Pow (a, 2) + 1))

    endfunction
The problem is that this function doesn't return the correct value.
 
Status
Not open for further replies.
Top