Advertisement

Circle-Rotated Rectangle Intersection

Started by January 02, 2002 04:33 AM
2 comments, last by shekky 23 years, 1 month ago
How could I determine if a Circle intersects a rectangle that has been rotated at an angle? I know I can do four line segment-circle intersection checks, but that seems like too much...is there a better way to do it?
Treat the rectangle''s orientation as the coordinate system for doing the intersection check. Transform the center of the circle into the rectangle''s local coordinate system (with, say, the "lower left" corner of the rectangle being the origin). You can do one quick check: if the center of the circle---as transformed into the rectangle''s coordinate system---is in the inside of the rectangle (0 <= x <= width of rectangle and 0 <= y <= height of rectangle), then the circle does intersect the rectangle. Otherwise, you can do a check that is like a rectangle/rectangle intersection, using 4 simple rules: 1) circle_center.x-radius is greater than the width of the rectangle; 2) circle_center.x+radius is less than zero; 3) circle_center.y-radius is greater than the height of the rectangle; 4) circle_center.y+radius is less than zero. If all 4 rules result are true, the circle does not intersect the rotated rectangle. If any one is false, then the circle might intersect the rectangle.Those basic checks will give you a quick idea of whether the circle intersects the rotated rectangle---but it won''t be a perfect check.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Advertisement
If you define your rectangle as two lengths and two unit vectors then you can get the coordinates as described above by calculating the dot product of a vector from a corner to the center of the circle with each of the vectors defining the rectangle. There can only be an intersection if the coordinates of the center are between -radius and edge length + radius. Within that rectangle and outside your rectangle there are eight sectors. If the center is within the four side sectors there is an intersection because you are within the radius distance from the side. Within the corner sectors you have to be within the radius distance of the corner for an intersection to occur because every other point on the edges is further away from every point in that sector. The line dividing the sectors would go with the edge sectors. If you drew the region where an intersection occurs at it would be a rectangle with rounded corners. The radius of the corners would be the radius of the circle.

That doesn''t allow for the circle being able to completely enclose the rectangle. There I think you would have to do a line segment/circle intersection.
Keys to success: Ability, ambition and opportunity.
Thanks for the good followup, LilBudyWizer!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

This topic is closed to new replies.

Advertisement