views:

5727

answers:

4

While working on SVG implementation for Internet Explorer to be based on its own VML format I came to a problem of translation of an SVG elliptical arc to an VML elliptical arc.

In VML an arc is given by: two angles for two points on ellipse and lengths of radiuses, In SVG an arc is given by: two pairs of coordinates for two points on ellipse and sizes of ellipse boundary box

So, the question is: How to express angles of two points on ellipse to two pairs of their coordinates. An intermediate question could be: How to find the center of an ellipse by coordinates of a pair of points on its curve.

Update: Let's have a precondition saying that an ellipse is normally placed (its radiuses are parallel to linear coordinate system axis), thus no rotation is applied.

Update: This question is not related to svg:ellipse element, rather to "a" elliptical arc command in svg:path element (SVG Paths: The elliptical arc curve commands)

A: 

An ellipse cannot be defined by only two points. Even a circle (a special cased ellipse) is defined by three points.

Even with three points, you would have infinite ellipses passing through these three points (think: rotation).

Note that a bounding box suggests a center for the ellipse, and most probably assumes that its major and minor axes are parallel to the x,y (or y,x) axes.

ΤΖΩΤΖΙΟΥ
Indeed, I forgotten to add a note on that. Will do now.
Sergey Ilinsky
+1  A: 

The intermediate question is fairly easy... you don't. You work out the centre of an ellipse from the bounding box (namely, the centre of the box is the centre of the ellipse, as long as the ellipse is centred in the box).

For your first question, I'd look at the polar form of the ellipse equation, which is available on Wikipedia. You would need to work out the eccentricity of the ellipse as well.

Or you could brute force the values from the bounding box... work out if a point lies on the ellipse and matches the angle, and iterate through every point in the bounding box.

workmad3
I am talking about elliptic arcs in svg:path, not an ellipse.As for the maths, I've done some three papers of solving equations both in polar and linear coordinate systems, still no luck.
Sergey Ilinsky
It should still be the same process. An elliptic arc is just a section of an ellipse with 2 'bounding points' of the ellipse. The bounding rectangle should be sufficient to give you all the information to construct the full ellipse. You then just need to feed the angles in to the equation.
workmad3
Again, I did not formulate question properly. It is not bounding rectangle that is given, but the sizes of radius.
Sergey Ilinsky
well, the radii given would let you formulate a bounding rectangle. Namely the x radius will let you gain the +x and -x values, and the same for the y. The combinations of these will give you all the points of the bounding rectangle. If a centre isn't given, then the only conclusion is that it's 0,0
workmad3
This is the problem, center is not in 0,0 (it can indeed be found anywhere) and an intermediate step could be finding the coordinate of center, later it would be easier to calcaulate the angles (given coordinates of points and sizes of bounding rectangle)
Sergey Ilinsky
A: 

I don't quite understand your second "intermediate" question. If you got the bounding box, the center is the center of the bounding box.

If I understand your other question right, you want to get from angles to karthesian coordinates. To do this, you can use the following formulas:

x = x0 + (h / 2) * cos(angle)
y = y0 + (w / 2) * sin(angle)

x0 and y0 are the center coordinates of the ellipse. h and w are the height and width of the bounding box.

Simon Lehmann
Sorry for misleading topic title, it is not bounding rectangle that is given, but the sizes of radius.
Sergey Ilinsky
+8  A: 

So the solution is here:

The parametrized formula of an ellipse:

x = x0 + a * cos(t)
y = y0 + b * sin(t)

Let's put known coordinates of two points to it:

x1 = x0 + a * cos(t1)
x2 = x0 + a * cos(t2)
y1 = y0 + b * sin(t1)
y2 = y0 + b * sin(t2)

Now we have a system of equations with 4 variables: center of ellipse (x0/y0) and two angles t1, t2

Let's subtract equations in order to get rid of center coordinates:

x1 - x2 = a * (cos(t1) - cos(t2))
y1 - y2 = b * (sin(t1) - sin(t2))

This can be rewritten (with product-to-sum identities formulas) as:

(x1 - x2) / (2 * a) = sin((t1 + t2) / 2) * sin((t1 - t2) / 2)
(y2 - y1) / (2 * b) = cos((t1 + t2) / 2) * sin((t1 - t2) / 2)

Let's replace some of the equations:

r1: (x1 - x2) / (2 * a)
r2: (y2 - y1) / (2 * b)
a1: (t1 + t2) / 2
a2: (t1 - t2) / 2

Then we get simple equations system:

r1 = sin(a1) * sin(a2)
r2 = cos(a1) * sin(a2)

Dividing first equation by second produces:

a1 = arctan(r1/r2)

Adding this result to the first equation gives:

a2 = arcsin(r2 / cos(arctan(r1/r2)))

Or, simple (using compositions of trig and inverse trig functions):

a2 = arcsin(r2 / (1 / sqrt(1 + (r1/r2)^2)))

or even more simple:

a2 = arcsin(sqrt(r1^2 + r2^2))

Now the initial four-equations system can be resolved with easy and all angles as well as eclipse center coordinates can be found.

Sergey Ilinsky