opengl - ellipse midpoint algorithm counterclockwise version -
i'm trying convert standart clockwise ellipse midpoint algorithm taken book "computer graphics opengl" work counter-clockwise starting region 2. got work , draw ellipse not identical 1 original algorithm draws assume have small bug in code can't seem find, can please help?
this original algorithm:
void ellipsemidpoint(int xcenter, int ycenter, int rx, int ry) { int rx2 = rx * rx; int ry2 = ry * ry; int tworx2 = 2 * rx2; int twory2 = 2 * ry2; int p; int x = 0; int y = ry; int px = 0; int py = tworx2 * y; void ellipseplotpoints(int, int, int, int); /* plot initial point in each quadrant. */ ellipseplotpoints(xcenter, ycenter, x, y); /* region 1 */ p = round(ry2 - (rx2 * ry) + (0.25 * rx2)); while (px < py) { x++; px += twory2; if (p < 0) p += ry2 + px; else { y--; py -= tworx2; p += ry2 + px - py; } ellipseplotpoints(xcenter, ycenter, x, y); } /* region 2 */ p = round(ry2 * (x + 0.5) * (x + 0.5) + rx2 * (y - 1) * (y - 1) - rx2 * ry2); while (y > 0) { y--; py -= tworx2; if (p > 0) p += rx2 - py; else { x++; px += twory2; p += rx2 - py + px; } ellipseplotpoints(xcenter, ycenter, x, y); } } void ellipseplotpoints(int xcenter, int ycenter, int x, int y) { setpixel(xcenter + x, ycenter + y); setpixel(xcenter - x, ycenter + y); setpixel(xcenter + x, ycenter - y); setpixel(xcenter - x, ycenter - y); }
and version:
void ellipsemidpointcounterclockwise(int xcenter, int ycenter, int rx, int ry) { int rx2 = rx * rx; int ry2 = ry * ry; int tworx2 = 2 * rx2; int twory2 = 2 * ry2; int p; int x = rx; int y = 0; int px = twory2 * x; int py = 0; void ellipseplotpoints(int, int, int, int); /* plot initial point in each quadrant. */ ellipseplotpoints(xcenter, ycenter, x, y); /* region 2 */ p = round(ry2 * (x - 0.5) * (x - 0.5) + rx2 * (y + 1) * (y + 1) - rx2 * ry2); while (py < px) { y++; py += tworx2; if (p > 0) p += rx2 - py; else { x--; px -= twory2; p += rx2 - py + px; } ellipseplotpoints(xcenter, ycenter, x, y); } /* region 1 */ p = round(ry2 * (x - 1.0) * (x - 1.0) + rx2 * (y + 0.5) * (y + 0.5) - rx2 * ry2); while (x > 0) { x--; px -= twory2; if (p < 0) p += ry2 + px; else { y++; py += tworx2; p += ry2 + px - py; } ellipseplotpoints(xcenter, ycenter, x, y); } }
i appreciate helping finding did wrong.
the first problem original code not symmetric respect x , y (and ellipsemidpointcounterclockwise replaces x , y).
ellipsemidpoint(0,0,3,2) generates first quadrant
0 2 1 2 2 1 3 0
whereas ellipsemidpoint(0,0,2,3) generates first quadrant
0 3 1 3 2 2 2 1 2 0
when exchanging coordinates , reversing order get:
0 2 1 2 2 2 3 1 3 0
graphically means:
002 | 12 +--0
where +
denotes center, 0
denotes ellipse points in both results, 1 point in first result, 2 points in second result (mirrored).
if want ellipsemidpointcounterclockwise generate same points - instead of replacing x , y - go negative x-direction (i.e. x-- instead of x++ in original code). otherwise have live difference in symmetry.
the second problem did not replace x , y other strange replacements. if replace x , y within regions correct result:
void ellipsemidpointcounterclockwise(int xcenter, int ycenter, int rx, int ry) { int rx2 = rx * rx; int ry2 = ry * ry; int tworx2 = 2 * rx2; int twory2 = 2 * ry2; int p; int x = rx; int y = 0; int px = twory2 * x; int py = 0; void ellipseplotpoints(int, int, int, int); /* plot initial point in each quadrant. */ ellipseplotpoints(xcenter, ycenter, x, y); /* region 2 */ p = round(rx2 - (ry2 * rx) + (0.25 * ry2)); while (py < px) { y++; py += tworx2; if (p < 0) p += rx2 + py; else { x--; px -= twory2; p += rx2 + py - px; } ellipseplotpoints(xcenter, ycenter, x, y); } /* region 1 */ p = round(rx2 * (y + 0.5) * (y + 0.5) + ry2 * (x - 1) * (x - 1) - ry2 * rx2); while (x > 0) { x--; px -= twory2; if (p > 0) p += ry2 - px; else { y++; py += tworx2; p += ry2 - px + py; } ellipseplotpoints(xcenter, ycenter, x, y); } }
Comments
Post a Comment