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

Baskara!!!

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....