Intersection of a rectangle and a circle (or arc)

I've read so many posts about this, either my brain isn't working or I just can't find the right answer.

I have sunk a segmented circle (i.e. a circle using arcs) and I have an image (rectangle) that moves. How can I know when rectangle and circle intersect and which arc or intersection point?

I can get a straight view of an image and a straight / center / radius of a circle

Your help is greatly appreciated Thanks

+3


source to share


2 answers


Finding intersections of rectangular circles and finding intersection points

You can decompose the problem into two smaller problems. There will be an intersection between the rectangle and the circle if:

  • The circle lies entirely within the rectangle, OR
  • Any of the four sides of the rectangle intersect the circle

The first case is simple, just check if the center point of the circle is inside the rectangle and is greater than or equal to the distance of the radius from each side. In this case, there is an intersection, and the arc of intersection is the whole circle. Between zero and four points of intersection there is: one for each edge, if this edge is exactly equal to the radius from the center point and zero, if it is greater than the radius: done.

In the second case, use four line-segment-to-circle intersection tests , one for each side of the rectangle, which will tell you if there is an intersection and which intersection points. There can be zero, one or two for each side.



Finding arcs of intersection

This applies only to the second case. Once you have intersection points, you can determine the arcs of intersection (in terms of start and end angles) by looking at the pairs of points where the edge of the rectangle enters the circle and one where the edge (perhaps not the same edge) leaves it at according to an arbitrary direction of the winding passing along the edges of the rectangle. Then for each point of each pair, take the x and y difference between the intersection point and the center point of the circle and work out the angle using atan2 (). These are the start and end angles of the intersection arc, and there can be up to four such arcs in total.

However, the special case must be ruled out first. If there is only 1 intersection point, then the circle "kisses" the rectangle: one of the sides is exactly equal to the radius from the center point of the circle, and they just touch each other, and there is no intersecting arc.

Testing rectangle against arcs instead of circle

If your circle is already segmented into predefined arcs before running the test, you can check to which arc each intersection point belongs by comparing its angle (calculated with atan2 ()) with the start and end angles of each predefined arc. Likewise, if you only want to test a rectangle along one arc rather than the entire circle, make sure the angle of the given intersection is within the arc's origin range, and if not, ignore it.

+2


source


I use another way, enlarging the rectangle along the radius of the circle and checking if the center of the circle contains the rectangle:

PointF center;
float radius;
RectF rect;

// Copy rect in another RectF instance if you need it later
rect.inset (-radius, -radius);

if (rect.contains (center.x, center.y)) {
     // Intersection
}

      



I hope this helps

0


source







All Articles