Home > Back-end >  I need Method that takes a vector and a circle and looks if they intersect
I need Method that takes a vector and a circle and looks if they intersect

Time:05-12

I have a vector in 2d space that starts at A and ends at B. I also have a circle in 2d space with the Position C and has a radius of r

I need to know if the vector AB does intersect with the circle(center:C,radius:r)

what would be the most efficient way to do this calculation?

CodePudding user response:

Given A and B are endpoints of a line. It can be represented as a vector with direction from A to B with a magnitude as the distance of AB.

The line AB doesn't intersect with the circle when the distance between the center of the circle C and the line AB perpendicular to the direction of the line is greater than the radius r of the circle.
Refer to the image below for visualization.

enter image description here

Calculating the distance between the line and the center of the circle:

Step 1: Get the line equation of AB from the formula below.
Here x1 = A.x, x2 = B.x, y1 = A.y, y2 = B.y.

enter image description here

Step 2: Calculate the distance d from the formula below.
Here x0 = C.x and y0 = C.y.

enter image description here

Finally, check whether the distance between the line and the center of the circle d is greater than the radius of the circle r or not. If yes, then the line lies outside the circle.

  • Related