views:

421

answers:

2

I've googled till I'm blue in the face, and unless I'm missing something really obvious, I can't find any algorithms for calculating the bounding box of a 2D sector.

Given the centre point of the enclosing circle, the radius, and the angles of the extent of the sector, what's the best algorithm to calculate the axis-aligned bounding rectangle of that sector?

+4  A: 
  • Generate the following points:
    • The circle's center
    • The positions of the two radii on the circle
    • The points on the circle for every angle between the two that divides by 90o (maximum of 4 points)
  • Calculate the min and max x and y from the above points. This is your bounding box
yairchu
Ah yes. I get it. Thanks!
Steve Folly
+3  A: 

I'm going to rephrase yairchu's answer so that it is clearer (to me, anyway).

Ignore the center coordinates for now and draw the circle at the origin. Convince yourself of the following:

  1. Anywhere the arc intersects an axis will be a max or a min.
  2. If the arc doesn't intersect an axis, then the center will be one corner of the bounding rectangle, and this is the only case when it will be.
  3. The only other possible extreme points of the sector to consider are the endpoints of the radii.

You now have at most 4+1+2 points to find. Find the max and min of those coordinates to draw the rectangle.

The rectangle is easily translated to the original circle by adding the coordinates of the center of the original circle to the rectangle's coordinates.

Glenn
+1 good phrasing (and reasoning) :)
yairchu
+1 for you as well Glenn. I got the gist of yairchu's explaination, but you did make it a bit clearer. Cheers.
Steve Folly