Line Segment Circle Intersection

前端 未结 5 987
刺人心
刺人心 2020-12-09 05:55

I am trying to determine the point at which a line segment intersect a circle. For example, given any point between P0 and P3 (And also assuming that you know the radius),

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 06:46

    From the center of the circle and the radius you can write the equation describing the circle. From the two points P0 and P1 you can write the equation describing the line.

    So you have 2 equations in 2 unknowns, which you can solved through substitution.

    Let (x0,y0) = coordinates of the point P0

    And (x1,y1) = coordinates of the point P1

    And r = the radius of the circle.

    The equation for the circle is:

    (x-x0)^2 + (y-y0)^2 = r^2
    

    The equation for the line is:

    (y-y0) = M(x-x0)  // where M = (y1-y0)/(x1-x0)
    

    Plugging the 2nd equation into the first gives:

    (x-x0)^2*(1 + M^2) = r^2
    
    x - x0 = r/sqrt(1+M^2)
    

    Similarly you can find that

    y - y0 = r/sqrt(1+1/M^2)
    

    The point (x,y) is the intersection point between the line and the circle, (x,y) is your answer.

    P3 = (x0 + r/sqrt(1+M^2), y0 + r/sqrt(1+1/M^2))
    

提交回复
热议问题