Circle line-segment collision detection algorithm?

前端 未结 28 1761
被撕碎了的回忆
被撕碎了的回忆 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:00

    If the line's coordinates are A.x, A.y and B.x, B.y and the circles center is C.x, C.y then the lines formulae are:

    x = A.x * t + B.x * (1 - t)

    y = A.y * t + B.y * (1 - t)

    where 0<=t<=1

    and the circle is

    (C.x - x)^2 + (C.y - y)^2 = R^2

    if you substitute x and y formulae of the line into the circles formula you get a second order equation of t and its solutions are the intersection points (if there are any). If you get a t which is smaller than 0 or greater than 1 then its not a solution but it shows that the line is 'pointing' to the direction of the circle.

提交回复
热议问题