2

我从一篇很棒的文章中改编了一些代码,该文章是 Mukund Sivaraman 绘制的关于圆绘制的文章,以便为给定圆上的每个点执行传递的函数:

template<class Function>
static void For_each_point_on_circle(Image *image, int radius, Function function)
{
    int x, y;
    int l;
    l = (int) radius * cos (M_PI / 4);
    for (x = 0; x <= l; x++)
    {
        y = (int) sqrt ((double) (radius * radius) - (x * x));
        function(image, x, y);
        function(image, x, -y);
        function(image, -x, y);
        function(image, -x, -y);
        function(image, y, x);
        function(image, y, -x);
        function(image, -y, x);
        function(image, -y, -x);
  }
}

但是,我真正需要的是按顺序计算圆周围的点,因此对 function(image, x, y) 的调用将从 0 到 360 度按顺序进行而不是跳过,这在绘制圆时是可以接受的.

我可以计算所有点并对它们进行排序,但我希望有人可能知道一种正确的方法,也许使用多个循环,每个循环计算一个段?

非常感谢。

4

2 回答 2

4

像这样的事情应该这样做:

template<class Function>
static void For_each_point_on_circle(Image *image, int radius, Function function)
{
    int x, y;
    int l;
    l = (int) radius * cos (M_PI / 4);
    for (x = -l; x < l; x++)
    {
        y = (int) sqrt ((double) (radius * radius) - (x * x));
        function(image, x, y);
    }
    for (x = -l; x < l; x++)
    {
        y = (int) sqrt ((double) (radius * radius) - (x * x));
        function(image, y, -x);
    }
    for (x = -l; x < l; x++)
    {
        y = (int) sqrt ((double) (radius * radius) - (x * x));
        function(image, -x, -y);
    }
    for (x = -l; x < l; x++)
    {
        y = (int) sqrt ((double) (radius * radius) - (x * x));
        function(image, -y, x);
    }
}
于 2012-06-13T18:49:12.527 回答
2

这是一篇关于分步走圆的文章。它的动机是用于 CNC 机器步进电机控制器,但也许它可以满足您的目的。

https://github.com/Falmarri/cnc/blob/master/BresenHam-3D-helix.pdf

于 2012-06-13T18:26:29.173 回答