'Scaling the X axis of a rotated ellipse

I have a scatter plot and I'm allowing points to be selected by drawing an ellipse like so:

enter image description here

In the above image the X axis and Y axis are scaled differently. I'm trying to implement functionality to allow the scale of each axis to be changed, but I'm having trouble scaling the ellipse shape with the axis. Changing the scale of the X axis to match the Y axis gives this:

enter image description here

I know I can scale the cx value of the ellipse with the X axis to get the new centre of the ellipse, but I don't know how to work out the new rx, ry and rotation angle. So my question is given a rotated ellipse with values cx, cy, rx, ry and rotation in radians, how do I calculate the new values for the ellipse given a scale value s for either the X axis or Y Axis? For clarity the rotation is done about the centre (cx, cy) of the ellipse.



Solution 1:[1]

I think I understand your problem. For an axis-aligned ellipse with points (x,y), the shape of (a*x,b*y) is still an ellipse with some arbitrary scaling factors a and b acting on the major and minor radii.

But for a rotated ellipse this is not true. In addition to the major and minor radii scaling differently, the rotation angle might differ also.

So mathematically the problem is as such: The parametric points of a rotated ellipse are

ellipse1

with ? the rotation angle r_1 and r_2 the original ellipse radii. The parameter t goes from 0 to 2?.

Now to find the modified parameters such that the ellipse matches a scaled version of the above

ellipse2

The above two are combined into one equation to be solved for r_1' and r_2' as well as ?' for all values of t.

equation

Unfortunately, there is no solution that I can think of, because the combined 2×2 matrix on the RHS in front of the vector needs to be diagonalized by finding the appropriate rotation ?' to bring it down to this form

equation2

which is trivially solved for r_1' and r_2'.

But the rotation needs to satisfy two contradictory equations

tan(?') = (a/b)*tan(?)    and     tan(?') = (b/a)*tan(?)

which can only be solved if the two scaling factors are identical => a==b.

I suggest posting this problem into the [Mathematics.SE] as math problem, before trying to implement it as an algorithm. Maybe you will have better luck.

Solution 2:[2]

The computation is not that easy.

The equation of a rotated ellipse centered at the origin is

(c x + s y)² / a² + (s x - c y)² / b² = 1 = A x² + 2B xy + C y²

(c, s denote the cosine and sine of the rotation angle.) After development, the quadratic coefficients are

A = c² / a² + s² / b², 2B = 2 cs / a² - 2 sc / b², C = s² / a² + c² / b².

When you have the coefficients, you can retrieve 1/a², 1/b², c, s as the Eigenvalues and first Eigen vector of the 2x2 matrix

|A   B|
|B   C|

and the rotation angle is given by tan ? = s / c.


Now let us stretch the abscissas by applying a coefficient r, giving

A' = r² A, B' = r B, C' = C.

The matrix becomes

|r²A   rB|
|rB     C|

and again, you will find the axis and cosine/sine of the angle by computing the Eigenvalues and first Eigenvector.

The Eigenvalues are roots of the polynomial

z² + (r²A + C) z + r²(AC - B²)

where A, B, C are computed as above, and the Eigenvector follows

(r²A - z) c + rB s = 0

from which you draw s/c.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Alexiou
Solution 2 Yves Daoust