2014-06-23 56 views
0

我試圖通過OpenGL中的三個指定點呈現弧。到目前爲止,我已經設法計算了所需的所有不同的值(通過三點的圓的中心和半徑,以及開始和結束角度)。我的問題是根據中點的位置決定繪製圓弧的方向,順時針還是逆時針。我的代碼如下:(哈克實驗代碼,點[0]開始,點[1]是中間和點[2]是結束)通過中心點的圓弧

void render() 
{ 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

glColor3f(0.0f, 0.0f, 0.0f); 

// Circle Parameters 
Point2D center = circleCenter(points[0], points[1], points[2]); 
double circleRadius = radius(points[0], points[1], points[2]); 

// Arc Parameters 
double start = atan2(points[0].y - center.y, points[0].x - center.x) * (180/M_PI); 
double middle = atan2(points[1].y - center.y, points[1].x - center.x) * (180/M_PI); 
double end = atan2(points[2].y - center.y, points[2].x - center.x) * (180/M_PI); 

if (start < 0) 
    start += 360; 
if (middle < 0) 
    middle += 360; 
if (end < 0) 
    end += 360; 

// Render circle 
glBegin(GL_LINE_STRIP); 
for (int i = start; i < end; i++) 
{ 
    double degInRad = i * M_PI/180; 
    glVertex2d(cos(degInRad)*circleRadius + center.x, sin(degInRad)*circleRadius + center.y); 
} 
glEnd(); 

glutSwapBuffers(); 
} 

正如你可以看到電弧目前只是正在再現在起點和終點之間。我需要做些什麼才能確保它在正確的方向上穿過中心點?

更新1: 例如,在下圖中,藍色點是我的三個定義點,紅色是圓心。在頂部圖像中,弧線穿過我的中間點。但在底部圖像中,即使我的中間點位於右側,弧線仍然是順時針方向。不同的起點/終點組合也會發生同樣的情況。是否有統一的方法來確保電弧始終通過中心點?

右:

Right

錯誤:

Wrong

+0

你可以添加你想要實現的圖像嗎? – mrVoid

+0

沒有足夠的代表添加圖像,但我已經鏈接了一些。 – user3750097

+0

這大多看起來像http://stackoverflow.com/questions/24178416/arc-via-3-points-in-specific-direction的副本。我不能將它標記爲重複,因爲另一個問題沒有被接受/有迴應的答案,但其中的內容仍應該有幫助。 –

回答

0

您可以檢查其中中間的謊言。

(...) 

if (end < start) 
    end += 360; 
if (middle < start) 
    middle += 360; 

if (middle < end) 
    doNormal = true; 
else 
    doNormal = false; 

// Render circle 
glBegin(GL_LINE_STRIP); 
for (int i = doNormal ? start : end; i < doNormal ? end : start; i += doNormal ? 1 : -1) 
    (...)