How can I tell if a point belongs to a certain line?

前端 未结 11 2199
你的背包
你的背包 2020-12-01 08:22

How can I tell if a point belongs to a certain line?

Examples are appreciated, if possible.

11条回答
  •  Happy的楠姐
    2020-12-01 08:54

    In the simplest form, just plug the coordinates into the line equation and check for equality.

    Given:

    Point p (X=4, Y=5)
    Line l (Slope=1, YIntersect=1)
    

    Plug in X and Y:

       Y = Slope * X + YIntersect
    => 5 = 1 * 4 + 1
    => 5 = 5
    

    So yes, the point is on the line.

    If your lines are represented in (X1,Y1),(X2,Y2) form, then you can calculate slope with:

     Slope = (y1 - y2) / (x1-x2)
    

    And then get the Y-Intersect with this:

     YIntersect = - Slope * X1 + Y1;
    

    Edit: I fixed the Y-Intersect (which has been X1 / Y1 ...)

    You'll have to check that x1 - x2 is not 0. If it is, then checking if the point is on the line is a simple matter of checking if the Y value in your point is equal to either x1 or x2. Also, check that the X of the point is not 'x1' or 'x2'.

提交回复
热议问题