Mapping rectangle to ellipse

My maths isn’t what it used to be…Maybe 15 years ago I could have done this :slight_smile:

I want to map the X,Y coords of the points inside a rectangle to a point inside an ellipse which has axis the same as the width/height of the rectangle i.e. the ellipse would just fit inside the rectangle.

So points on the edge of the rectangle map to the edge of the ellipse i.e. the intersect with the ellipse of a line drawn from the center to the point on the edge of the rectangle.

Points inside the rectangle also map. e.g. all the points along the major/minor axis would map directly.

Obviously there is overlap since the area of the ellipse is less than the area of the rectangle.

Thanks,

Stuart.

FWIW My first thoughts were

0,0 is the center of the rectangle (and ellipse)
For point Px,Py find the intersect with the ellipse edge (Ex,Ey) and rectangle edge (Rx,Ry) using a line drawn from 0,0 through Px,Py.
Then get the ratio of the lengths of the two vectors, sqrt(Ex*Ex+Ey*Ey)/sqrt(Rx*Rx+Ry*Ry)
Multiple the length of the original vector, sqrt(Px*Px+Py*Py), by that value and that gives the distance along the original line of the mapped point.
  

Thoughts?

–Stuart.

There are lots of ways to do this. The simplest in OpenGL is to draw a circle of radius 1 (optionally inscribed in a -1,+1 x,y unrotated square) with a transform scale and rotation applied that matches the desired output rectangle.

By scaling and rotating a transform matrix, you scale and rotate everything it is applied to, including the square->rectangle and the points of your circle->ellipse. In other words, anything you do to the square to make it a rectangle will also happen to the circle to make it an ellipse.

I think what you are suggesting is how to draw the ellipse. It’s the mapping I need; a way to turn a X,Y inside the rectangle to an X,Y inside the ellipse. i.e. a function that takes Rx,Ry and returns Ex,Ey.

e.g. I could map all the points outside the ellipse to the edge of the ellipse, but that doesn’t give a “smooth” mapping (if you see what I mean). I think my suggestion above does, but it feels like there should be a better way.

–Stuart.

Assuming you’re not talking about morphing or walking the perimeter of the rectangle into/as an ellipse, it works pretty much as I described, with one more step.

If you take any given point and multiply it by the inverse transform of the matrix that drew the stretched/rotated ellipse and/or rectangle, you get a point in a uniform coordinate space that’s mapped to the boundaries of the perfect square, and similarly to the uniform circle.

The difference between the rectangle and ellipse in that case is just

isPtInside = xx + yy < 1;

for the transformed circle and

isPtInside = -1 < x < +1, -1 < y < +1

for the rectangle.

Note I skipped the sqrt, which is not needed in this case because sqrt of 1 = 1.

If you want to track points along the perimeter, it can work similarly, but you’d need to treat the rectangle as a parametric line from t=0 to t=1 as it walks along the four edges and comes full “circle” so to speak.