我想画一个对角半圆。到目前为止,我只能绘制在水平轴或垂直轴上开始和结束的图形,如下所示:
我试过修改代码使圆倾斜,但没有用。谁能告诉我哪里出错了,这真让人生气!
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();
最佳答案
我推荐用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();
关于c - 在 OpenGL 中绘制对角半圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52187007/