Search code examples
javascriptmathpseudocode

Detecting if a point is of a line segment


If I have a line, with the points x,y,endx and endy how can I detect if another point is on the line? A simple equation, or example functions in JavaScript or pseudocode will be most helpful.

EDIT: This is for a game I'm working on, I'm trying to detect if a laser is colliding with an object, Here is the sample http://jefnull.com/references/lasers/ The file that will be most descriptive is http://jefnull.com/references/lasers/lasers.js


Solution

  • Since my previous answer said how to determine if a point was on the line, and the real question appears to be "how can I tell if the point is near the line segment", I'm adding a new answer.

    Here's the trick: first find the distance from your obstacle to each of the two endpoints of your line segment. These two distances don't uniquely determine the location of the obstacle, but they do uniquely determine a triangle with three specific side lengths, and then we can immediately use a bunch of geometry.

    Triangle with sides A, B, C

    I fiddled with the colors a little. Anyway, I mentioned in a comment above that you should use the point-line distance formula to find the distance between the obstacle and the line. But that won't actually work. The reason is that is is the point-line distance. So, for both examples below, the formula will calculate the bold distance H in the picture.

    Acute and Obtuse Triangle Diagrams

    That isn't right!!

    So instead, here is the pseudocode for finding the distance from your obstacle to the line segment formed by the laser:

    Find the distance from my point to the line segment!
    
    if the angle at (x,y) is obtuse
        return A
    else if the angle at (endx,endy) is obtuse
        return B
    else
        return H
    

    Here is the math you can use to implement the above pseudocode:

    • To see if the angle at (x,y) is obtuse, find whether B^2 > A^2 + C^2. If so, the angle is obtuse.
    • To see if the angle at (endx, endy) is obtuse, find whether A^2 > B^2 + C^2. If so, the angle is obtuse.
    • To calculate H, use two different methods for finding the area of the triangle -- the usual base*height/2 and Heron's Formula.

    This means you should:

    set s = (A+B+C)/2
    The area of the triangle is C*H/2
    The area of the triangle is also sqrt(s*(s-A)*(s-B)*(s-C)) 
    So H = 2/C * sqrt(s*(s-A)*(s-B)*(s-C)).
    

    The end result is something like:

    if B^2 > A^2 + C^2
        return A
    else if A^2 > B^2 + C^2
        return B
    else
        s = (A+B+C)/2
        return 2/C * sqrt(s*(s-A)*(s-B)*(s-C))
    

    I think that should give you enough to accomplish what you are actually setting out to do. Good luck, and don't give up!