以下是使用中点算法和Bresenham算法在OpenGL上绘制圆的代码:
```c++
#include <GL/glut.h>
#include <cmath>
#include <iostream>
using namespace std;
// 中点算法画圆
void midPointCircle(int x0, int y0, int r) {
int x = r, y = 0;
int p = 1 - r;
glBegin(GL_POINTS);
while (x >= y) {
glVertex2i(x0 + x, y0 + y);
glVertex2i(x0 + y, y0 + x);
glVertex2i(x0 - y, y0 + x);
glVertex2i(x0 - x, y0 + y);
glVertex2i(x0 - x, y0 - y);
glVertex2i(x0 - y, y0 - x);
glVertex2i(x0 + y, y0 - x);
glVertex2i(x0 + x, y0 - y);
y++;
if (p <= 0)
p += 2 * y + 1;
else {
x--;
p += 2 * (y - x) + 1;
}
}
glEnd();
}
// Bresenham算法画圆
void bresenhamCircle(int x0, int y0, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
glBegin(GL_POINTS);
while (x <= y) {
glVertex2i(x0 + x, y0 + y);
glVertex2i(x0 + y, y0 + x);
glVertex2i(x0 - y, y0 + x);
glVertex2i(x0 - x, y0 + y);
glVertex2i(x0 - x, y0 - y);
glVertex2i(x0 - y, y0 - x);
glVertex2i(x0 + y, y0 - x);
glVertex2i(x0 + x, y0 - y);
if (d < 0)
d = d + 4 * x + 6;
else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
// 输入两个圆的圆心坐标和半径长
int x1, y1, r1, x2, y2, r2;
cout << "请输入第一个圆的圆心坐标和半径:";
cin >> x1 >> y1 >> r1;
cout << "请输入第二个圆的圆心坐标和半径:";
cin >> x2 >> y2 >> r2;
// 绘制第一个圆
glColor3f(1.0f, 0.0f, 0.0f);
midPointCircle(x1, y1, r1);
glColor3f(0.0f, 1.0f, 0.0f);
bresenhamCircle(x1, y1, r1);
// 绘制第二个圆
glColor3f(0.0f, 0.0f, 1.0f);
midPointCircle(x2, y2, r2);
glColor3f(1.0f, 1.0f, 0.0f);
bresenhamCircle(x2, y2, r2);
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Circle Drawing Algorithms");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
```
在程序中,我们通过`midPointCircle`和`bresenhamCircle`函数分别使用中点算法和Bresenham算法绘制圆。在`display`函数中,我们输入两个圆的圆心坐标和半径长,并依次使用两种算法绘制出这两个圆。最终,我们使用OpenGL的基本绘图函数`glBegin`和`glVertex2i`将圆绘制在屏幕上。