1
現在、C++プログラムでミッドポイントサークルアルゴリズムを使用してサークルを描画しています。ここで私が使用しているコードの例です。(C++)サークル内の2D位置をクランプする(中点サークルアルゴリズムを使用して描画する)
void drawcircle(int x0, int y0, int radius)
{
int x = radius-1;
int y = 0;
int dx = 1;
int dy = 1;
int err = dx - (radius << 1);
while (x >= y)
{
putpixel(x0 + x, y0 + y);
putpixel(x0 + y, y0 + x);
putpixel(x0 - y, y0 + x);
putpixel(x0 - x, y0 + y);
putpixel(x0 - x, y0 - y);
putpixel(x0 - y, y0 - x);
putpixel(x0 + y, y0 - x);
putpixel(x0 + x, y0 - y);
if (err <= 0)
{
y++;
err += dy;
dy += 2;
}
if (err > 0)
{
x--;
dx += 2;
err += (-radius << 1) + dx;
}
}
}
私は「クランプ」を返し、その後、2次元位置をとり、その2次元位置が円内にあるかどうかをチェックし、もしできなかったか、このdrawcircle(100, 100, 100);
のように円を描くていた場合さて、私の質問があります円の端にある2Dの位置。
これは、私が少し良くしていることを説明するのに役立ちます。
void _2DPostionToCirclePosition(Vector2D in, Vector2D *out)
{
//assume in = Vector2D(800, 1100)
//here we somehow calculate if the in 2D
//position is within the circle we
//are drawing drawcircle(100, 100, 100).
//if the in 2D vector is within the circle
//just return in. but if it outside of the
//circle calculate and return a clamped position
//to the edge of the circle
(*out).x = calculatedOutX;
(*out).y = calculatedOutY;
}
x、yは式(x-xo)^ 2 +(y-y0)^ 2
macroland