Circle line-segment collision detection algorithm?

前端 未结 28 1810
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:38

I have a line from A to B and a circle positioned at C with the radius R.

\"Image\"

What is a good alg

28条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 07:01

    Okay, I won't give you code, but since you have tagged this algorithm, I don't think that will matter to you. First, you have to get a vector perpendicular to the line.

    You will have an unknown variable in y = ax + c ( c will be unknown )
    To solve for that, Calculate it's value when the line passes through the center of the circle.

    That is,
    Plug in the location of the circle center to the line equation and solve for c.
    Then calculate the intersection point of the original line and its normal.

    This will give you the closest point on the line to the circle.
    Calculate the distance between this point and the circle center (using the magnitude of the vector).
    If this is less than the radius of the circle - voila, we have an intersection!

提交回复
热议问题