4
$\begingroup$

I have the data of an incomplete ellipse and I need to retreive the minimun information in order to describe an elliptical arc. In particular following are my ellipse data:

  • Major axis vector (x, y)
  • Major/minor axis ratio
  • Start angle
  • End angle
  • Center point (x, y)

And for the arc I need the following information:

  • Start point (x, y)
  • End point (x, y)
  • Radius X
  • Radius Y
  • Rotation angle

What equations I need to use in order to get this data?

Following is an example. Given this ellipse:

  • Major axis vector = (-120, -185)
  • Major/minor axis ratio = 0.267
  • Start angle = 0 deg
  • End angle = 180 deg
  • Center point = (0, 0)

The ellipse is:

ellipse

With start point in (-120, -185) and end point in (120, 185).

I tried by using the classic ellipse equation but it works with some ellipses and not for other (like for example the above one).

$\endgroup$
  • $\begingroup$ The SVG specification seems relevant: w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands. $\endgroup$ – lhf Jul 2 '15 at 17:26
  • $\begingroup$ @lhf thanks, actually I have to convert a DXF file with the ellipse data to an SVG arc path. $\endgroup$ – Nick Jul 2 '15 at 17:29
  • $\begingroup$ What I don't remember about SVG is which is the zero direction and which way the rotation angle turns (clockwise or counterclockwise). Another question (maybe about DXF) is about "start angle" and "end angle": are they actual directions from the center of the ellipse to the start or end point, or are they the directions to where the start and end points would move to if we stretched the ellipse to a circular shape. (The latter is what you get if the ellipse is plotted by the usual parameterization, something like $(x,y)=(a\cos\theta,b\sin\theta)$.) $\endgroup$ – David K Jul 2 '15 at 17:38
  • $\begingroup$ @DavidK In SVG the abscissa follows the same direction as in DXF (that you can see in the picture), while the ordinate is reversed. If I don' remeber wrong the angle turns counterclockwise. Regarding the start angle it is the angle between the major axis vector and the first point, while the end angle is the angle between the vector and the end point. $\endgroup$ – Nick Jul 2 '15 at 17:46
2
$\begingroup$

Suppose we start with these (generalized) input data:

  • Center point = $(x_0, y_0)$
  • Major axis vector = $(a_x, a_y)$
  • Major/minor axis ratio = $k$
  • Start angle = $\theta_1$
  • End angle = $\theta_2$

For simplicity of exposition, I'll assume that angles are given in the same units that are expected as input by the sine and cosine functions that you will use when implementing this algorithm. I will also assume that in both the coordinate systems you work in (input and output), to rotate a vector that (initially) points in the direction of the positive $x$-axis so that it ends up pointing in the direction of the positive $y$-axis, you would rotate it through a positive angle of $90$ degrees or $\frac\pi2$ radians.

I will assume that the vector $(a_x, a_y)$ represents the displacement vector from the center of the ellipse to one end of the major axis. Then the direction of this vector is $\psi = \text{atan2}(a_y,a_x)$, where $\text{atan2}(\cdot)$ is the two-parameter arc tangent function available in many computer math libraries.

The semi-major axis length ("radius X") is then $a = \sqrt{a_x^2 + a_y^2}.$ The semi-minor axis length ("radius Y") is $b = ka.$

There are at least two ways that angular values measured from the center of an ellipse are used to describe points on the ellipse. One is the actual angle between the vector from the center to the point in question and a vector parallel to the major axis. The other way is to use the so-called eccentric anomaly. The eccentric anomaly is what you see when someone parameterizes an ellipse in the form $(x,y) = (a \cos \theta, b \sin \theta).$ One way to tell which of these your graphic library uses is to draw an ellipse whose minor axis is $0.1$ times the major axis, starting at an angle of $45$ degrees (or $\frac\pi4$) and ending at an angle of $135$ degrees (or $\frac{3\pi}4$). If the library interprets the angles as actual angles, it will draw an arc whose length is about $20\%$ of the length of the major axis. If the library interprets each angle as an eccentric anomaly then the arc length will be about $70\%$ of the length of the major axis.

I'll assume we're using real angles, not eccentric anomaly. The polar equation for an ellipse centered at the origin, with axes $a$ and $b$ parallel to the coordinate axes is $$ r = \frac{ab}{\sqrt{b\cos^2\theta + a\sin^2\theta}}. \tag 1$$

This gives a parametric equation of the ellipse, $$ (x(\theta),y(\theta)) = \left(r(\theta) \cos\theta, r(\theta) \sin\theta\right) \tag 2$$ with $r(\theta)$ defined by Equation $(1)$.

Recalling that we already computed $\psi$, the direction of the major axis, we can use the rotation matrix $$ M = \begin{pmatrix} \cos \psi & -\sin \psi \\ \sin \psi & \cos \psi) \end{pmatrix}$$ to "rotate" the ellipse. (If this results in a rotation in the wrong direction, replace $M$ with its transpose.) So, translating the ellipse of Equation $(2)$ so its center is at $(x_0,y_0)$ and rotating it by angle $\psi$ to align the major axis correctly, we have $$ (x(\theta),y(\theta)) = (x_0,y_0) + M \left(r(\theta) \cos\theta, r(\theta) \sin\theta\right). $$

Plug in $\theta_1$ to get your starting point, or $\theta_2$ to get your ending point.

Actually I seem to recall that SVG lets you translate and rotate pieces of the drawing individually, so maybe you could just use Equation $(2)$ to find out where the starting and ending points would be if the ellipse were centered at $(0,0)$ at an angle of $0$, then move the resulting arc to where it actually should be.

It's possible that some of this is based on mistaken assumptions about how the two graphics systems work. If it is a simple mistake such as drawing the figure upside down or rotating it in the wrong direction, a judicious change of sign in one of the equations will probably fix it. I usually end up figuring out that sort of thing by trial and error (since there are usually only two choices and one will clearly be wrong).

$\endgroup$
  • $\begingroup$ Thanks for your answer, but I'm having some trouble with it. Consider the first point for example, according to your answer and my example we have a = 221, b = 59, ψ = 237 deg and this values are right for me. But when I compute theabscissa of the first point I get x(0) = r(0) * cos(0) = 1699, y(0)= r(y) * sin(0) = 0 that are quite wrong as results. Am I missing something? $\endgroup$ – Nick Jul 4 '15 at 16:54
  • $\begingroup$ Moreover, regarding the last part of your answer, do you refer to the SVG rotation transform? w3.org/TR/SVG/coords.html#TransformAttribute $\endgroup$ – Nick Jul 4 '15 at 16:55
  • $\begingroup$ I found the problem with your equation: it was related with the square of the cos() and sin(), as stated here en.wikipedia.org/wiki/Ellipse#Polar_form_relative_to_focus. $\endgroup$ – Nick Jul 4 '15 at 19:01
  • $\begingroup$ @Nick, the equation I used was a polar equation with origin at the center, not the focus: en.wikipedia.org/wiki/Ellipse#Polar_form_relative_to_center. I am measuring $\theta$ counterclockwise from the semi-major axis of the ellipse. For $\theta=0$ this yields $r(0) = ab/\sqrt{b\cdot 1^2 + a\cdot 0^2} = a$, just as it should (no idea how you got $1699$ there), and $(r(0) \cos 0, r(0) \sin 0) = (a,0)$; but then remember you need to apply a rotation matrix to find the $x$ and $y$ coordinates after the ellipse is rotated. $\endgroup$ – David K Jul 6 '15 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.