【问题标题】:Circular Arc Through a Center Point通过中心点的圆弧
【发布时间】:2014-06-23 23:14:54
【问题描述】:

我正在尝试通过 OpenGL 中的三个指定点来渲染 Arc。到目前为止,我已经设法计算出这样做所需的所有不同值(通过三个点的圆的中心和半径,以及开始和结束角度)。我的问题在于决定画弧的方向,顺时针或逆时针,这取决于中点的位置。我的代码如下:(hacky实验代码,points[0]是start,points[1]是middle,points[2]是end)

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: 例如,在下图中,蓝色点是我定义的三个点,红色是圆心。在上图中,弧线穿过我的中点。但是在底部图像中,即使我的中间点现在在右侧,圆弧仍然是顺时针绘制的。对于不同的起点/终点组合也会发生同样的情况。有没有统一的方法保证圆弧总是通过中心点?

右:

错误:

【问题讨论】:

  • 你能添加一张你想要达到的目标的图片吗?
  • 没有足够的代表来添加图片,但我已经链接了一些。
  • 这看起来很像 stackoverflow.com/questions/24178416/… 的副本。我无法将其标记为重复,因为另一个问题没有接受/赞成的答案,但那里的内容应该仍然有用。
  • 是的,只需在此处使用地雷角度校正...并使用 glVertex 更改 MoveTo/LineTo ...

标签: c++ opengl geometry


【解决方案1】:

你可以检查中间的位置。

(...)

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)
   (...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    相关资源
    最近更新 更多