2017-11-08 24 views
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; 
} 
+1

x、yは式(x-xo)^ 2 +(y-y0)^ 2 macroland

答えて

1

ポイントは、あなたはそれがここにあなたが(100100)持っており、円の中心からの距離だ計算する必要がある最初の円の中にあるかどうかを判断するには。このような 何か:

#include <math.h> //for pow and sqrt 

double getPointDistance(int x, int y) { 
    return sqrt (pow ((circleCenterX - x), 2.0) + 
     pow ((circleCenterY - y), 2.0)); 
} 

次に、あなただけのそれは小さいだ場合、それは内部のだと、それは同じだならば、それは右端上にある、距離が半径よりも大きい場合には、それは外だ、あなたの円の半径と比較することができます。これを行うには、次のようなものを使用できます:

bool isInCircle(int x, int y) { 
    if (circleRadius >= getPointDistance(x, y)) { 
     //it's inside the circle (we assumed on the edge is inside) 
     return true; 
    } else { 
     //given point is outside of the circle 
     return false; 
    } 
} 
関連する問題