## Fitting an arc inside a rectangle

### Fitting an arc inside a rectangle

> I've been trying to fit a arc inside a rectangle.  The circle can
> start at any angle, sweep in any direction, and cover any arc
> (including a full 360), and the rectangle can be any size.

You have explained lots of things, but not the most central term of
your problem: what it means when you say "to fit".  What distinguishes
a well fitting solution from a bad one, to you?  What is the actual
input, and what do you get to vary, to make a good fit?

--

Even if all the snow were burnt, ashes would remain.

### Fitting an arc inside a rectangle

On Mon, 07 Jul 2003 23:48:10 GMT, Alex Pavloff

>I've been trying to fit a arc inside a rectangle.  The circle can
>start at any angle, sweep in any direction, and cover any arc
>(including a full 360), and the rectangle can be any size.

Use quadrant information for your sweep. If you stay in one quadrant
the minimum enclosing rectangle of an arc is the bounding box of the
start and end points. Otherwise you hit one or more x and/or y limits
of the circle itself at multiples of 90 degrees; in which case include
any such extrema in your bounding box calculation.

Examples.
Sweep from 30deg to 60deg. Bound (sqrt(3)/2,1/2), (1/2,sqrt(3)/2).

Sweep from 30deg to 120deg. Bound (sqrt(3)/2,1/2), (-1/2,sqrt(3)/2),
and (0,1).

Sweep from -30deg to 120deg. Bound (sqrt(3)/2,-1/2), (-1/2,sqrt(3)/2),
(1,0), and (0,1).

### Fitting an arc inside a rectangle

On Tue, 08 Jul 2003 23:25:01 GMT, Alex Pavloff

>My problem at this point is taking this bounding box and the
>corresponding center point and calcing the largest radius possible
>that will keep this arc in the bounding box.

Do you mean to say that knowing the height and width of two boxes you
cannot find a uniform scale factor that fits one inside the other?!

Let Ah and Aw be the arc height and width, Bh and Bw be that of the
box in which it must fit. Then Sh=Bh/Ah scales the arc height to fit
perfectly, and Sw=Bw/Aw scales its width. Take the minimum as radius!

Incidentally, if you need to keep the circle center inside the box as
well, include (0,0) when you compute the arc bounds.

Clear thinking is all that's required; the mathematics is trivial.

### Fitting an arc inside a rectangle

On Wed, 09 Jul 2003 17:19:29 GMT, Alex Pavloff

>>Let Ah and Aw be the arc height and width, Bh and Bw be that of the
>>box in which it must fit. Then Sh=Bh/Ah scales the arc height to fit
>>perfectly, and Sw=Bw/Aw scales its width. Take the minimum as radius!

>That's what I'm doing now, but it doesn't always create the largest
>possible arc.  Example: If I have an arc that goes from 35 degrees to
>45 degrees,  the maximum radius is neither the minimum height or
>width.

It seems unlikely you are doing precisely what I say and yet getting
different results.

Specifically, if the arc goes from 35 degrees to 45 degrees I said to
take the bounding box of those end points, because the arc crosses no
quadrant boundaries. When you bound

(0.819,0.574)    (0.707,0.707)

you should get

x IN (0.707,0.819)
y IN (0.574,0.707)

so the width is 0.112 and the height is 0.133. Now suppose you want to
fit that arc in a unit square. For a perfect width fit you would scale
the arc by 8.9, while for a perfect height fit you would use 7.5. The
smaller of those two is height scale, so that's what you must use as
your radius.

My example assumes, since you ignored the issue, that you do not wish
to include the center of the circle.

I do not wish to belabor this question any further. If your code does
not work, it's up to you to make the effort to debug it.

I have an arc, described with the following :

X,Y centre of ellipse
X axis radius
Y axis radius
start angle - anticlockwise from positive X axis
end angle - likewise
possible rotation of whole object about centre

I need to determine the bounding rectangle of the rendered arc.

One way I can think of is to approximate the arc using a polyline and
trivially determine the bounding box of the polyline. The two things
would obviously be combined in one pass for efficiency.

Does anyone know of a more elegant/efficient solution that I have
overlooked ?

Thanks for your help,

Dave.
--
Dave Vint.
Tenet Systems Ltd.