Area of intersection between circle and rectangle

user67424 picture user67424 · Mar 7, 2009 · Viewed 27.8k times · Source

I'm looking for a fast way to determine the area of intersection between a rectangle and a circle (I need to do millions of these calculations).

A specific property is that in all cases the circle and rectangle always have 2 points of intersection.

Answer

Daniel LeCheminant picture Daniel LeCheminant · Mar 7, 2009

Given 2 points of intersection:

0 vertices is inside the circle: The area of a circular segment

    XXXXX              -------------------
   X     X               X            X Circular segment
  X       X               XX        XX 
+-X-------X--+              XXXXXXXX 
|  X     X   |
|   XXXXX    |

1 vertex is inside the circle: The sum of the areas of a circular segment and a triangle.

    XXXXX                   XXXXXXXXX
   X     X       Triangle ->X     _-X
  X       X                 X   _-  X 
  X    +--X--+              X _-   X <- Circular segment 
   X   | X   |              X-  XXX 
    XXXXX    |              XXXX
       |     | 

2 vertices are inside the circle: The sum of the area of two triangles and a circular segment

    XXXXX                   +------------X
   X     X                  |      _--'/'X 
  X    +--X---    Triangle->|   _--   / X
  X    |  X                 |_--     /XX <- Circular segment
   X   +-X----              +-------XX
    XXXXX                 Triangle^

3 vertices are inside the circle: The area of the rectangle minus the area of a triangle plus the area of a circular segment

    XXXXX
   X  +--X+             XXX
  X   |   X         -------XXX-----+ <- Triangle outside
 X    |   |X        Rect ''.  XXX  |
 X    +---+X                ''.  XX|  
 X         X                   ''. X <- Circular segment inside 
  X       X                       ^|X 
   X     X                         | X 
    XXXXX

To calculate these areas: