(20 points) Ellipse drawing
In class, the following procedure to draw one octant of a circle was presented:
sector(int x)
{
int x = r;
int y = 0;
int e = -r;
while(x > y) {
point(x,y);
e += 2*y + 1;
y += 1;
if( e >= 0) {
e += 2*x + 2;
x -= 1;
}
}
}
Suppose instead of a circle we want to draw the ellipse: x^2 + (y^2)/4 =
r^2. Modify the above procedure to draw this ellipse by filling in the
following blanks:
sector(int x)
{
int x =
int y =
int e =
while( ) {
point(x,y);
if( e >= 0 ) {
}
}
}
sector(int x)
{
int x = r;
int y = 0;
int e = -4*r - 1;
while( 4*x > y ) {
point(x,y);
e += 2*y + 1;
y += 1;
if( e >= 0 ) {
e += -8*x + 8;
x -= 1;
}
}
}
Derivation:
while should continue until we can no longer move up
without having to move more than one column to the left. This is true as long
as the slope of the tangent is less than -1: