Optimally place a pie slice in a rectangle.

Posted by Lisa on Stack Overflow See other posts from Stack Overflow or by Lisa
Published on 2010-04-07T19:50:36Z Indexed on 2010/04/07 19:53 UTC
Read the original article Hit count: 230

Filed under:
|
|

Given a rectangle (w, h) and a pie slice with start angle and end angle, how can I place the slice optimally in the rectangle so that it fills the room best (from an optical point of view, not mathematically speaking)?

I'm currently placing the pie slice's center in the center of the rectangle and use the half of the smaller of both rectangle sides as the radius. This leaves plenty of room for certain configurations.

Examples to make clear what I'm after, based on the precondition that the slice is drawn like a unit circle:

  • A start angle of 0 and an end angle of PI would lead to a filled lower half of the rectangle and an empty upper half. A good solution here would be to move the center up by 1/4*h.
  • A start angle of 0 and an end angle of PI/2 would lead to a filled bottom right quarter of the rectangle. A good solution here would be to move the center point to the top left of the rectangle and to set the radius to the smaller of both rectangle sides.

This is fairly easy for the cases I've sketched but it becomes complicated when the start and end angles are arbitrary. I am searching for an algorithm which determines center of the slice and radius in a way that fills the rectangle best. Pseudo code would be great since I'm not a big mathematician.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about computer