是否有中点椭圆算法?

时间:2013-03-18 10:07:07

标签: javascript algorithm graphics

是否存在类似于中点圆算法的中点椭圆绘图算法?

我在google上搜索了一些示例,但是我发现任何一个都不起作用或者是填充椭圆,而不是绘制。此外,中点圆圈算法的维基百科页面指的是椭圆版本的存在,但有一个谷歌似乎无法帮助解决的死链接。

感谢任何帮助。

1 个答案:

答案 0 :(得分:6)

最终在这里找到答案:

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

转载和调整后更普遍适用于......

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);
    }
}