【问题标题】:Drawing a diagonal semicircle in OpenGL在OpenGL中绘制一个对角半圆
【发布时间】:2018-09-05 14:00:27
【问题描述】:

我正在尝试绘制一个对角线半圆。到目前为止,我只能绘制在水平或垂直轴上开始和结束的那些,如下所示:

我尝试修改代码以倾斜圆圈,但它不起作用。谁能告诉我哪里出错了,这太令人生气了!

float theta, tanTheta, x, y, dx, dy;
int circle_points = 1000, radius = 70;

glBegin(GL_POLYGON);

    for(int i = 0; i < circle_points; i++)
    {
        dx = pts[1].x - pts[0].x;
        dy = pts[1].y - pts[0].y;

        tanTheta = tan(dy / dx);

        // get the inverse
        theta = atan(tanTheta);

        x = radius * cos(theta);
        y = radius * sin(theta);

        glVertex2f(x, y);
    }

glEnd();

【问题讨论】:

  • 您是否尝试过循环遍历i 的不同值范围?向theta 添加一个恒定的偏移值怎么样?
  • “我试过修改代码来倾斜圆圈,但是不行”你能把原始代码和你的修改展示给我们吗?
  • 您是否考虑过使用atan2 而不是分两步计算角度?它“使用参数的符号计算 y/x 的反正切以确定正确的象限”.
  • 这似乎是数学/几何,而不是编程问题。应该在每本中学数学书上都有解释。
  • 很高兴您找到了解决问题的方法。但是,实际的答案/解决方案不应编辑到您的问题中。一般来说,您应该edit 问题来澄清问题,但不要在问题中包含答案。您应该使用用于解决问题的代码创建自己的答案,然后接受它(系统可能需要 48 小时延迟才能接受您自己的答案)。当您自己解决问题后,answering your own question is encouraged

标签: c opengl draw


【解决方案1】:

我建议用atan2计算到起点的角度和到终点的角度。
插入起始角和结束角之间的角度,并沿着圆上的相应点画一条线:

float ang_start, ang_end, theta, x, y;

ang_start = atan2( pts[0].y, pts[0].x );
ang_end   = atan2( pts[1].y, pts[1].x );
if ( ang_start > ang_end )
    ang_start -= 2.0f * M_PI;

glBegin(GL_LINE_STRIP);

for(int i = 0; i <= circle_points; i++)
{
    float w = (float)i / (float)circle_points;
    float theta = ang_start + w * ( ang_end - ang_start );

    x = radius * cos(theta);
    y = radius * sin(theta);

    glVertex2f(x, y);
}

glEnd(); 

【讨论】:

    猜你喜欢
    • 2017-06-06
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多