简体   繁体   English

圆画算法说明

[英]Circle draw algorithm explanation

Given the following circle drawing algorithm (variation of Bresenham) : 给定以下圆图绘制算法(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

    }
}

I don't seem to understand why it works. 我似乎不明白它为什么起作用。 I do understand that it draws a pixel at each octant because the circle is symmetric but I don't get what the variables u, v and e stand for..? 我确实知道它在每个八分圆处都画一个像素,因为圆是对称的,但是我不知道变量u,v和e代表什么。 e stands for the error I suppose but when does it change? e代表我猜想的错误,但是何时更改? and why? 为什么呢?

help will be appreciated! 帮助将不胜感激! thanks 谢谢

Its always hard to reverse-engineer optimized algorithms, because the underlying invariants are often obscured or totally omitted in the final code. 它总是很难对优化算法进行逆向工程,因为在最终代码中,基本不变式通常会被遮盖或完全省略。

In that case take a look behind the scenes, for example Wikipedia has a detailed explanation of the math used: http://en.wikipedia.org/wiki/Bresenham%27s_circle_algorithm 在这种情况下,请查看幕后情况,例如Wikipedia对使用的数学进行了详细说明: http : //en.wikipedia.org/wiki/Bresenham%27s_circle_algorithm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM