首页 文章

按顺序计算圆周围的点

提问于
浏览
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);
  }
}

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

我可以计算所有的点并对它们进行排序,但是我希望有人可能知道如何正确地完成它,可能使用多个循环,每个循环计算一个段?

非常感谢 .

2 回答

  • 2

    这样的事情应该这样做:

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

    这是一篇关于在不连续的步骤中找出圆圈的文章 . 它的动机是数控机床步进电机控制器,但也许它可以用于您的目的 .

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

相关问题