• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[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