Conic Drawing Algorithm
So now we know how to compute the discriminator, and the slope at every point along a conic
and we can write the conic-drawing routine.
The code is rather long. It must decide at every step whether to step in positive or negative directions
in x or y, and whether the slope is less than or greater than 1. This gives us 8 choices at every point corresponding
to octants of the coordinate system.
One of the most difficult aspects of using the conic routine is specifying the
desired conic. Directly specifying the coefficients, A, B, C, D, E, and F, is not
very intuitive. The interface seen on the right was designed for specifying ellipses.
The user gives three points, the first two are on the ellipse and the third specifies
the intersection of the major and minor axes.
|
 |
Lecture 6 |
|
Slide 16 |
|
6.837 Fall '98 |
 |
final static int OCTANTS = 0x12650374;
public void conic(
int x0, int y0, // starting point
int x1, int y1, // ending point
Color color, // color of curve
float A, float B, float C, // coefficients of conic
float D, float E, float F)
{
int pix = color.getRGB();
int dxDiag, dyDiag, dxLine, dyLine;
int octant = 0;
float d, u, v;
F = 0;
D = 2*A*x0 + B*y0 + D;
E = B*x0 + 2*C*y0 + E;
if (D >= 0) {
dxDiag = dxLine = 1;
octant += 1;
} else {
dxDiag = dxLine = -1;
}
if (E >= 0) {
dyDiag = dyLine = 1;
octant += 2;
} else {
dyDiag = dyLine = -1;
}
if (Math.abs(E) > Math.abs(D)) {
dxLine = 0;
u = dxDiag*dyDiag*B/2 + C + dyDiag*E;
d = u + A/4 + dxDiag*D/2 + F;
v = u + dxDiag*D;
octant += 4;
} else {
dyLine = 0;
u = dxDiag*dyDiag*B/2 + A + dxDiag*D;
d = u + C/4 + dyDiag*E/2 + F;
v = u + dyDiag*E;
}
octant = (OCTANTS >> (4*octant)) & 7;
float k1 = 2*(A + dyLine*dyDiag*(C - A));
float k2 = dxDiag*dyDiag*B;
float k3 = 2*(A + C + k2);
k2 += k1;
loop: do {
if ((octant & 1) == 0) {
while (2*v <= k2) {
raster.setPixel(pix, x0, y0);
if ((x0 == x1) & (y0 == y1)) break loop;
if (d < 0) {
x0 += dxLine;
y0 += dyLine;
u += k1;
v += k2;
d += u;
} else {
x0 += dxDiag;
y0 += dyDiag;
u += k2;
v += k3;
d += v;
}
}
d = d - u + v/2 - k2/2 + 3*k3/8;
u = -u + v - k2/2 + k3/2;
v = v - k2 + k3/2;
k1 = k1 - 2*k2 + k3;
k2 = k3 - k2;
int t = dxLine; dxLine = -dyLine; dyLine = t;
} else {
while (2*u < k2) {
raster.setPixel(pix, x0, y0);
if ((x0 == x1) & (y0 == y1)) break loop;
if (d > 0) {
x0 += dxLine;
y0 += dyLine;
u += k1;
v += k2;
d += u;
} else {
x0 += dxDiag;
y0 += dyDiag;
u += k2;
v += k3;
d += v;
}
}
float dk = k1 - k2;
d = d + u - v + dk;
v = 2*u - v + dk;
u = u + dk;
k3 = k3 + 4*dk;
k2 = k1 + dk;
int t = dxDiag; dxDiag = -dyDiag; dyDiag = t;
}
octant = (octant + 1) & 7;
} while (true);
}