Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a midpoint ellipse algorithm?

Is there a midpoint ellipse plotting algorithm similar to the midpoint circle algorithm?

I've searched on google for examples but any I have found either don't work or are for filled ellipses, not plotted. Also, the wikipedia page on the midpoint circle algorithm refers to the existance of an ellipse version but has a dead link which google seems unable to help resolve.

Any help would be gratefully appreciated.

like image 341
izb Avatar asked Mar 18 '13 10:03

izb


1 Answers

Eventually found an answer here:

http://geofhagopian.net/sablog/Slog-october/slog-10-25-05.htm

Reproduced and tweaked to be more generally applicable below...

function ellipsePlotPoints (xc,yc,  x,  y)
{
    setPixel (xc + x, yc + y);
    setPixel (xc - x, yc + y);
    setPixel (xc + x, yc - y);
    setPixel (xc - x, yc - y);
}

function ellipse(xc,yc,  a,  b)
{
    var a2 = a * a;
    var b2 = b * b;
    var twoa2 = 2 * a2;
    var twob2 = 2 * b2;
    var p;
    var x = 0;
    var y = b;
    var px = 0;
    var py = twoa2 * y;

    /* Plot the initial point in each quadrant. */
    ellipsePlotPoints (xc,yc, x, y);

    /* Region 1 */
    p = Math.round (b2 - (a2 * b) + (0.25 * a2));
    while (px < py) {
        x++;
        px += twob2;
        if (p < 0)
        p += b2 + px;
        else {
        y--;
        py -= twoa2;
        p += b2 + px - py;
        }
        ellipsePlotPoints (xc,yc, x, y);
    }

    /* Region 2 */
    p = Math.round (b2 * (x+0.5) * (x+0.5) + a2 * (y-1) * (y-1) - a2 * b2);
    while (y > 0) {
        y--;
        py -= twoa2;
        if (p > 0)
        p += a2 - py;
        else {
        x++;
        px += twob2;
        p += a2 - py + px;
        }
        ellipsePlotPoints (xc,yc, x, y);
    }
}
like image 154
izb Avatar answered Oct 01 '22 23:10

izb