2017-03-01 7 views
1

座標が2点あり、円を描く必要があります。 1点が中心で、もう1点が円の端にあるので、基本的に2点間の距離は円の半径です。私はMFCでこれを行う必要があります。私はこれを試しましたが、円は適切に描画されていません。通常はそれよりも大きくなります。2つの点に基づいて円を描く

double radius = sqrt((c->p1.x*1.0 - c->p1.y) * (c->p1.x - c->p1.y) + 
        (c->p2.x - c->p2.y) * (c->p2.x - c->p2.y)); 

CPen black(PS_SOLID,3,RGB(255,0,0)); 
pDC->SelectObject(&black); 

pDC->Ellipse(c->p1.x-radius , c->p1.y-radius, c->p1.x+radius, c->p1.y+radius); 

p1p2がポイントです。円は四角形の内接円として描画されます。 Ellipse()の引数は、四角形の左上と右下にあります。

答えて

2

あなたの半径の計算が間違っている...それは次のようになります。ここでは

double radius = sqrt(((c->p2.x - c->p1.x)*(c->p2.x - c->p1.x)) 
        +((c->p2.y - c->p1.y)*(c->p2.y - c->p1.y))); 
2

はそれが読みやすく(そして正しい)だ、半径を計算するための実装です:

#include <cmath> 

int findRadius(const CPoint& p1, const CPoint& p2) { 
    // Calculate distance 
    CPoint dist{ p2 }; 
    dist -= p1; 
    // Calculate radius 
    double r = std::sqrt((dist.x * dist.x) + (dist.y * dist.y)); 
    // Convert to integer with appropriate rounding 
    return static_cast<int>(r + 0.5); 
} 

これはレンダリングコードから使用できます:

int radius = findRadius(c->p1, c->p2); 
CPen black(PS_SOLID, 3, RGB(255, 0, 0)); 
// Save previously selected pen 
CPen* pOld = pDC->SelectObject(&black); 
pDC->Ellipse(c->p1.x - radius, c->p1.y - radius, 
       c->p1.x + radius, c->p1.y + radius); 
// Restore DC by selecting the old pen 
pDC->SelectObject(pOld); 
関連する問題