# Find Ellipse knowing 2 points, half-axises, rotation angle

• Jun 7th 2013, 03:52 AM
sekelsenmat
Find Ellipse knowing 2 points, half-axises, rotation angle
Hello,

I am implementing a SVG renderer and I just found a very nasty trigonometry problem. I already wrote a lot of geometric code, but nothing gave me so many headaches like "elliptical arcs" which area feature of SVG =D

Basically I receive as input 2 points which bellong to the ellipse, the rotation of the ellipse in relation to axis X, and the sizes of the half-axises of the ellipse.

And I want to find the center of the ellipse. I know that there are 2 solutions to the problem, and I need both.

There are images ilustrating the problem here:

Path - Elliptical Curve
and
SVG Basics Tutorials - Elliptical Arcs with SVG

Here is what I tried so far:

I know the following information:

X,Y -> this is a point which bellongs to the ellipse. I have this point, but I don't know which parameter t=t2 is at this point
Xstart,Ystart -> Another point of the ellipse that I know, has parameter t=t1
CX,CY -> This is what I want! The center of the ellipse!

solve 4 equations and 4 unknown values (CX, CY, t1, t2)
X,Y has t=t2 // Xstart,Ystart has t=t1
CX := X - RX*Cos(t2)*Cos(XRotation) + RY*Sin(t2)*Sin(XRotation);
CX := XStart - RX*Cos(t1)*Cos(XRotation) + RY*Sin(t1)*Sin(XRotation);
CY := Y - RY*Sin(t2)*Cos(XRotation) - RX*Cos(t2)*Sin(XRotation);
CY := YStart - RY*Sin(t1)*Cos(XRotation) - RX*Cos(t1)*Sin(XRotation);

Substituting we get 2 equations and 2 unknown values (t1, t2)
X - RX*Cos(t2)*Cos(XRotation) + RY*Sin(t2)*Sin(XRotation) = XStart - RX*Cos(t1)*Cos(XRotation) + RY*Sin(t1)*Sin(XRotation)
Y - RY*Sin(t2)*Cos(XRotation) - RX*Cos(t2)*Sin(XRotation) = YStart - RY*Sin(t1)*Cos(XRotation) - RX*Cos(t1)*Sin(XRotation)

Simplify:

A = RX*Cos(XRotation)
B = RY*Cos(XRotation)
C = RY*Sin(XRotation)
D = RX*Sin(XRotation)

X - A*Cos(t2) + C*Sin(t2) = XStart - A*Cos(t1) + C*Sin(t1)
Y - B*Sin(t2) - D*Cos(t2) = YStart - B*Sin(t1) - D*Cos(t1)

Attempt to corner Sin(t1):

X - A*Cos(t2) + C*Sin(t2) = XStart - A*sqrt(1-Sin(t1)^2) + C*Sin(t1)
X - A*Cos(t2) + C*Sin(t2) - XStart - C*Sin(t1) = - A*sqrt(1-Sin^2(t1))
Mega = X - A*Cos(t2) + C*Sin(t2) - XStart
Mega^2 - 2*Mega*C*Sin(t1) + C^2*Sin^2(t1) = A^2*(1-Sin^2(t1))
Mega^2 - A^2 - 2*Mega*C*Sin(t1) + (C^2+A^2)*Sin^2(t1) = 0

AB = (C^2+A^2)
BB = -2*Mega*C
CB = Mega^2

DeltaB=BB^2-4*AB*CB
Sin(t1)=(-BB+/-sqrt(DeltaB))/(2*AB)

Now roll it back!

X - A*Cos(t2) + C*Sin(t2) = XStart - A*sqrt(1-Sin(t1)^2) + C*Sin(t1)

But at this point I am totally lost ... the fully expanded equation is *huge*! And I have no idea if I am getting anywhere...

Does anyone know what I could do to solve this in a simpler way? Because the way that I am trying so far is really big and nasty....
• Jun 7th 2013, 10:46 PM
chiro
Re: Find Ellipse knowing 2 points, half-axises, rotation angle
Hey sekelsenmat.

Why don't you rotate the points so that the axis are aligned with y and z axes and use parameterized equations to get the solution?

If you have a general ellipse at some custom CX,CY then an axis aligned parameterization would be:

EX = CX + RX*cos(t)
EY = CY + RY*sin(t)

From here you need to find the values of t that give CX and CY and the values that do this will be t = 0 and t = pi/2.

If the ellipse has been rotated, then you need to rotate it so that it is axis aligned. You can rotate something by the inverse of its angle quite easily with a standard rotation matrix (with -theta as your argument).

Overall though, once you get it so that the rotation axis are aligned to Y and Z, then given your points for EX and EY for two points, you obtain values of RX and RY by picking a value of t that eliminates the RX RY terms to get CX and CY (like mentioned above).
• Jun 10th 2013, 11:58 AM
johng
Re: Find Ellipse knowing 2 points, half-axises, rotation angle
Hi,
I think it's easier if you don't use parametric equations. The attachment indicates a solution using the "standard" equations for an ellipse. As you probably know, arbitrary values for the current point, (xEnd,yEnd), rx and ry can possibly have no solution. I don't know what SVG does in this case; I didn't look very hard to find what happens. However, your code should take this possibility into account. If you have trouble turning the discusssion into code, post your questions in this thread and I'll try to answer.

Attachment 28590

Attachment 28591