角度离散法可以利用已有的直线算法来分段绘制圆弧、椭圆弧。这种方法的优点在于可以自由控制所绘制的弧的角度范围。
完整代码:
#include<GL/glut.h>
#include<windows.h>
#include<math.h>
void Arcellipse(int xc, int yc, double r, double ts, double te)
{
double pi = 3.1415926;
if (te < ts) //当终止角比起始角还小时,则将终止角加上2π
te += 2 * pi;
double dt = 0.4 / r; //取角度离散值,使其与半径r成反比
int n = (int)((te - ts) / dt + 0.5); //确定总步数
double ta = ts;
int x = xc + int(r*cos(ts));
int y = yc + int(r*sin(ts));
glBegin(GL_LINE_STRIP); //如果绘制整圆,选GL_LINE_LOOP更好
glVertex2f(x, y);
for (int i = 1; i <= n; i++)
{
ta += i * dt;
double cost = cos(ta);
double sint = sin(ta);
x = int(xc + r * cost);
y = int(yc + r * sint);
glVertex2f(x, y);
}
glEnd();
}
void ChangeSize(GLsizei w, GLsizei h)
{
if (h == 0) h = 1;
// 设置视区尺寸
glViewport(0, 0, w, h);
// 重置坐标系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// 建立修剪空间的范围
if (w <= h)
glOrtho(0.0f, 250.0f, 0.0f, 250.0f*h / w, 1.0, -1.0);
else
glOrtho(0.0f, 250.0f*w / h, 0.0f,