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 = 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 に答える