Ellipse Pixels AlgorithmsMy goal today is to generalize the circle algorithm of the last few days to drawing ellipses. My subgoal is to just ignore all this WebEQ/MathML/MathPlayer crap. It’s just worn me down to where I just can’t stand it that it still doesn’t work.
A geometric definition of an ellipse is:
the set of all points the sum of whose distances from two fixed points is a constant.” That is,
Assuming the foci are at (x1,y1) and (x2,y2) the rectangular form of this equation is By equating squares, isolating the radical and again equating squares, you’ll arrive at and equation equivalent to the basic conic form where the parameters A, B, C, D, E and F depend on where the ellipse is centered and the lengths of its major and minor axes.
For an ellipse centered at (h,k) with , the standard rectangular form is often written as or in polar coordinates, An ellipse
has somewhat less symmetry than a circle, so we may need to do more
computations to find its pixel coordinates.
To avoid computations where the slope (rate of change of one variable
with respect to change in the other) is too large, we’ll split the first
quadrant into a region where
The analogous decision function will involve which has the nice property that As with the circle algorithm, we can start at the top point (0,b) and take unit steps in the x direction until we reach the point where the slope is -1 where we switch to unit steps in the (negative) y direction. At each step we compute the slope of the tangent line:
Following the process for the circle midpoint algorithm of yesterday, we seek a recurrence relation for the decision parameter: Grouping the terms of pxk and observing that, in the second case, So that we again a nice tidy recurrence relation: and since the values of 2b2xk+1 and 2a2yk+1 can easily be obtained by recursion, the formula requires only addition and subtraction! Initially, at (0,b), these terms are and then updated by adding 2b2 to the x term and subtracting 2a2 from the y term:
Updated increments are computed until we reach the point where b2x>a2y (the tangent line has slope of -1) and then we switch to other region.
In the first region we have the initial decision parameter value In the second region, we switch from steadily incrementing x by 1 to steadily decrementing y by 1. Thus the decision parameter is the f function evaluated at the midpoint of the boundary between the pixel directly below the current pixel and the the pixel immediately to its right: If pyk > 0 then the midpoint sample is outside the boundary so we don’t move over, only down. Otherwise, we increase the x coordinate by 1.
where yk+1 = yk−1. and xk+1
= xk+
Hearn and Baker observe that you can express this function this way The last position from crossing the border into the second region can then be the first position for walking through the second region, and for simplicity we can rename it so that
If you start at (a,0) rather than at (0,b) and then move counterclockwise. In this case you’ll increment y coordinates steadily until entering the other region, where you’ll steadily decrement x values. Or you can mix to start on the axis and move towards the interior of the first quadrant.
You can
adapt this midpoint algorithm to plot ellipses with rotated axes by
using
So here’s pseudo code for an ellipse midpoint algorithm:
Tomorrow maybe investigate implementating.
|