Ellipses (for SVG): parameter values

TL;DR

After the center (see Ellipses (for SVG): finding the center), it’s time to find the values for t that represent our arc of ellipse.

Last post was a bit of a roller-coaster to find the center of the ellipse. Now we’re going to find the parameters values, but this is definitely easier at this point.

If we go back to the translated-then-rotated representation, i.e. the one centered in C, we can easily translate the origin on the center and obtain:

P1=(x1,y1)=(x1Cx,y1Cy)P2=(x2,y2)=(x1Cx,y1Cy)

Remember: in the first translation, the origin was put in the midpoint of P1 and P2, so they happen to have opposite coordinate values.

These are all known quantities at this point. To find the respective values of t, we remember that this is the angle of the point corresponding to re-normalizing the ellipse back to a unitary circle, so for t1 we have:

cos(t1)=x1rx=x1Cxrxsin(t1)=y1ry=y1Cyry

This will allow us to find the right value of t1. Many programming languages provide the atan2 function, which takes two parameters (in the Y and X direction, in our case) and avoids infinites, so we can calculate t1 as:

t1=atan2(y1Cyry,x1Cxrx)

Of course the atan2 function does not complain if we scale both arguments by the same factor, so we can use the equivalent expression:

t1=atan2(rx(y1Cy),ry(x1Cx))

The same goes for t2, of course:

t2=atan2(rx(y1Cy),ry(x1Cx))

Now we have that t1 and t2 are values in the interval ]π,π], and we have to:

  • establish which comes first and which second (based on which arc of the ellipse we are interested into);
  • find an equivalent contiguous range.

This is basically finding tbegin and tend. We can do like this:

  • initialize tbegin=t1 and tend=t2;
  • make sure that tbegin is not greater tend. To do this, we subtract 2π from tbegin if it is greater than tend:
tbegin>tendtbegintbegin2π
  • at this point, we can campute δ=tendtbegin and assign to tbegin the value of tend in either of the following cases:
    • δπ (i.e. it’s the short arc of ellipse) but fA=1 (i.e. we need the long one), OR
    • δ>π (i.e. it’s the long arc of ellipse) but fA=0 (i.e. we need the short one).
  • shift tbegin in a range we like (e.g. [0,2π[), if we want;
  • last, re-calculate tend=tbegin+δ

Now we have a contiguos interval [tbegin,tend] that allows us sweep the whole arc.

The sweeping in this interval might make your pen start from the end point P2 and go back to P1. As we are eventually interested into the bounding box, anyway, this is not an issue here.

Problem solved… theoretically, stay tuned for the implementation!


Comments? Octodon, , GitHub, Reddit, or drop me a line!