3

I read so many posts about this, either my brain is not working or I am just not finding the right answer.

I have drown a segmented circle (ie circle using arcs) and I have an imageview (rectangle) that is moving around. How can I know when the rectangle and the circle intersects and which arc or point of intersection?

I am able to obtain the rect of the imageview and the rect/center/radius of the circle

Your help is greatly appreciated Thanks

Snake
  • 14,228
  • 27
  • 117
  • 250
  • Possible duplicate of [Find the arc of the intersection between circle and rectangle](https://stackoverflow.com/questions/22605134/find-the-arc-of-the-intersection-between-circle-and-rectangle) – Max MacLeod Apr 05 '19 at 10:27

2 Answers2

3

Detecting rectangle-circle intersections 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 4 sides of the rectangle intersect the circle

The first case is easy, just check if the center-point of the circle is inside the rectangle and greater than or equal to radius distance away from each side. In this case there is an intersection, and the intersection arc is the entire circle. There are between zero and four intersection points: one for each edge if that edge is exactly radius away from the center-point and zero if it is greater than radius: done.

For 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 what the intersection points are. There can be zero, one or two of them for each side.

Finding intersection arcs

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

However, first you have to exclude a special case. If there is only 1 intersection point then the circle is "kissing" the rectangle: one of the sides is exactly radius away from the circle center-point and they are just touching, and there is no intersection arc.

Testing a rectangle against arcs instead of a circle

If your circle is already segmented into pre-defined arcs before you do the test, then you can check which arc each intersection point belongs to by comparing its angle (calculated with atan2()) to the start and end angles of each pre-defined arc. Similarly if you only want to test a rectangle against a single arc instead of an entire circle, check that the angle of a given intersection point is within the start-end range of the arc and if not then ignore it.

Community
  • 1
  • 1
samgak
  • 23,944
  • 4
  • 60
  • 82
1

I use a different way: growing the rectangle by the circle radius and checking if the center of the circle is contained by 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 it helps

Urizev
  • 561
  • 6
  • 24