2014-06-23 50 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) 
    (...)