PDA

View Full Version : Twiddling some ellipse numbers...


Raptisoft
07-27-2005, 07:53 AM
Hi all,

I have a quick mathy question. Suppose you have a rectangle, you want the four corners of that rectangle to touch the edges of an ellipse (i.e. the rectangle rests perfectly within the ellipse).

Knowing the four corners of the rectangle, how can you figure out an x and y radius of the ellipse?

Thanks,
John

mahlzeit
07-27-2005, 08:30 AM
You know four points that are on the ellipse already (the corners of the rectangle) and the center of the ellipse (the center of the rectangle). From that you can derive the radius (pythagoras). Now you just have to figure out some way to split that radius into an x and a y component (probably: get the angle with tan, then y is sin, x is cos).

EDIT: or maybe not... I can't get my head clear enough to think about this. :)

Didier
07-27-2005, 08:35 AM
I think if you calculate the exact center of the rectangle:
X= rectangle length / 2
Y= rectangel width / 2

you have also the center of your ellipse, from then on it's simple to know the exact radius.

Edit: So Radius Ellipse= Rectangle length / 2

Didier

Raptisoft
07-27-2005, 08:38 AM
So simple it need not even be mentioned, eh?

Raptisoft
07-27-2005, 08:44 AM
Okay, the only comprehensive thing I can think of to do is to take the corner points and then rotate them 45 degrees, and then squash the rotation by the rectangle's aspect ratio. But that doesn't sound quite right to me. Isn't there some standard number that one can multiply the radius of an ellipse bounded by a rectangle by to get the radius of the ellipse that bounds the rectangle?

Didier
07-27-2005, 08:54 AM
Hi John,
Just calculate the center of your rectangle (as mentioned above:
X= rectangle length / 2
Y= rectangel width / 2 )

From that point, draw your ellipse with half the length of your rectangle (that will be the X value like you calculated above)

This should give you something like you want (maybe you still need a little pixel tuning after that ;)

Didier

mahlzeit
07-27-2005, 08:56 AM
@Didier: That only works when the rectangle encompasses the ellipse, but here the ellipse encompasses the rectangle.

@Raptisoft: There is only one bounding rectangle for the ellipse, but infinitely many rectangles can be drawn inside, so I doubt there's a quick number to go from one to the other...

But the aspect ratio thing sounds promising. :)

Didier
07-27-2005, 09:05 AM
Okay, i guess i found the answer.

When you draw a line from the center of the rectangle to 1 of your rectangle corners, that line is the EXACT radius of your ellipse.

When you then draw the ellipse from the center of your rectangle, all corners should fit perfectly!

What do you think?

Didier

digriz
07-27-2005, 09:09 AM
Surely, that wouldn't be the radius of the ellipse. It would just be the radius of a circle.

Raptisoft
07-27-2005, 09:10 AM
That'll work great, if it's a circle. But since it's not, I need to apply the squashing method, which really doesn't sound like an efficient way to do it for me.

Didier
07-27-2005, 09:13 AM
Maybe this could help: http://mathworld.wolfram.com/Ellipse.html

The animated pic looks interesting.

Didier

Nexic
07-27-2005, 09:38 AM
I don't actually know the answer, but it seems as if most of the suggestions you are giving are for a square drawn inside a circle, not a rectangle drawn inside an oval.

I'm guessing you need to get the 45 degree radius by getting the center to corner distance, then using some kind of quadratic equation to scale that radius for each given degree. I don't know what that equation would be, but I'm sure it exists

HunterSD
07-27-2005, 09:40 AM
You'll want to do this by a rectangle of xwidth, ywidth and theta (four coordinates get a lot harder, because they can form something that's not a rectangle).

The radius of a circle around an axis aligned unit square is sqrt( 2 ). Thus, an elipse around that rectangle would be:

radiusx = sqrt(2) * rectx
radiusy = sqrt(2) * recty

[Edit: Oops, you can't find a axis bound scale values for a rotated elispe!].

digriz
07-27-2005, 10:10 AM
Just found this. Might be what you're looking for. Or at least have some information you could use.

Ellipse fitter (http://rsb.info.nih.gov/ij/docs/source/ij/process/EllipseFitter.java.html)

asc
07-27-2005, 12:41 PM
Well... I think an ellipse is just a squashed circle, so if your rectangle has width w and height h, the ellipse will have width = sqrt(2) * w, and height = sqrt(2) * h.
(Assuming your rectangle is aligned with the axes.)

Andy.

GameStudioD
07-27-2005, 10:29 PM
Suppose you have a rectangle, you want the four corners of that rectangle to touch the edges of an ellipse (i.e. the rectangle rests perfectly within the ellipse).

Knowing the four corners of the rectangle, how can you figure out an x and y radius of the ellipse?

I may be missing something here, but finding the x and y radius of the ellipse with only the coordinates of the rectangle's corners would be impossible. Given an arbitrary rectangle, more than one ellipse exists which satisfies your condition. Further, given an arbitrary ellipse, multiple rectangles can satisfy your condition. The equation for the solution would have an infinite(?) number of solutions. Seems to me that either the x or y radius of the ellipse must be constrained...

Sharkbait
07-28-2005, 12:06 AM
GameStudioD is right... there is in fact an infinity of ellipses that intersect with a the corners of a given rectangle so you would have to restrict the ratio. I don't have a proof at hand for this (and too lazy to check! :)), but intuitively I would say that an ellipse with the same aspect ratio as the rectangle's would be the smallest bounding ellipse.

Raptisoft
07-28-2005, 04:16 AM
Just for the record-- and to maybe close this thread-- the sqrt(2) thing works... nice simple solution!

Pyabo
07-28-2005, 01:10 PM
I don't think there are an infinite number of solutions... I think there are only two ellipses that satisfy the conditions...? In the case of a square, there is only one, which happens to be a circle.

HunterSD
07-28-2005, 10:44 PM
There are infinite. If you make an ellipse wider, you can make it shorter to still fit around that same square. If an ellipse has a width of infinity, it'll only need a height of one.