在OpenGL中绘制对角半圆

问题描述 投票:1回答:1

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

enter image description here

我已经尝试修改代码以使圆圈倾斜,但它不起作用。有人可以告诉我哪里出错了,这真是令人气愤!

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();
c opengl draw
1个回答
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(); 
© www.soinside.com 2019 - 2024. All rights reserved.