圆绘制算法解释

时间:2014-05-28 14:48:28

标签: java algorithm graphics geometry

给出以下圆绘制算法(Bresenham的变体):

public static void
drawCircle(Graphics g, MyPoint center, MyPoint point)
{
    // calculate radius of circle
    final int radius = (int)(Math.sqrt((point.x()-center.x())*(point.x()-center.x())+(point.y()-center.y())*(point.y()-center.y())));

    int x = 0, y = radius, u = 0, v = 2 * radius - 1, E = 0;
    while (x < y)
    {
        drawPixel(g, new MyPoint(center.x() + x, center.y() + y)); // NNE
        drawPixel(g, new MyPoint(center.x() + y, center.y() - x)); // ESE
        drawPixel(g, new MyPoint(center.x() - x, center.y() - y)); // SSW
        drawPixel(g, new MyPoint(center.x() - y, center.y() + x)); // WNW
        ++x;
        E += u;
        u += 2;
        if (v < 2 * E){
            y--; E -= v; v -= 2;
        }
        if (x > y) break;
        drawPixel(g, new MyPoint(center.x() + y, center.y() + x)); // ENE
        drawPixel(g, new MyPoint(center.x() + x, center.y() - y)); // SSE
        drawPixel(g, new MyPoint(center.x() - y, center.y() - x)); // WSW
        drawPixel(g, new MyPoint(center.x() - x, center.y() + y)); // NNW

    }
}

我似乎不明白为什么会这样。我知道它在每个八分圆绘制一个像素,因为圆是对称的,但我不知道变量u,v和e代表什么? e代表我想的错误,但它何时改变?为什么?

帮助将不胜感激!谢谢

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

它总是难以对优化算法进行逆向工程,因为底层不变量通常在最终代码中被模糊或完全省略。

在这种情况下,请看一下幕后,例如维基百科对所用数学有详细解释: http://en.wikipedia.org/wiki/Bresenham%27s_circle_algorithm