画家算法完整代码opengl
时间: 2023-09-14 09:07:45 浏览: 342
以下是一个使用OpenGL实现画家算法的示例代码,主要实现了画线、画圆、椭圆、矩形和填充矩形等基本图形的绘制:
```c++
#include <GL/glut.h>
int width = 800, height = 600; // 窗口宽高
int x1, y1, x2, y2; // 两个点的坐标
int radius, x, y; // 圆的半径和圆心坐标
int a, b; // 椭圆的长短半轴
bool fillRect = false; // 是否填充矩形
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, (GLdouble) width, 0.0, (GLdouble) height);
}
void drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void drawLine(int x1, int y1, int x2, int y2) {
int dx = x2 - x1, dy = y2 - y1;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float xInc = dx / (float) steps, yInc = dy / (float) steps;
float x = x1, y = y1;
for (int i = 0; i <= steps; i++) {
drawPixel((int) x, (int) y);
x += xInc;
y += yInc;
}
}
void drawCircle(int radius, int x, int y) {
int d = 3 - 2 * radius;
int xc = 0, yc = radius;
while (xc <= yc) {
drawPixel(x + xc, y + yc);
drawPixel(x + xc, y - yc);
drawPixel(x - xc, y + yc);
drawPixel(x - xc, y - yc);
drawPixel(x + yc, y + xc);
drawPixel(x + yc, y - xc);
drawPixel(x - yc, y + xc);
drawPixel(x - yc, y - xc);
if (d < 0) {
d = d + 4 * xc + 6;
} else {
d = d + 4 * (xc - yc) + 10;
yc--;
}
xc++;
}
}
void drawEllipse(int a, int b, int x, int y) {
float d1 = b * b + a * a * (-b + 0.25);
float dx = 2 * b * b * x, dy = 2 * a * a * y;
float d2;
while (dx < dy) {
drawPixel(x, y);
drawPixel(-x, y);
drawPixel(x, -y);
drawPixel(-x, -y);
if (d1 < 0) {
x++;
dx += 2 * b * b;
d1 += dx + b * b;
} else {
x++;
y--;
dx += 2 * b * b;
dy -= 2 * a * a;
d1 += dx - dy + b * b;
}
}
d2 = b * b * (x + 0.5) * (x + 0.5) + a * a * (y - 1) * (y - 1) - a * a * b * b;
while (y >= 0) {
drawPixel(x, y);
drawPixel(-x, y);
drawPixel(x, -y);
drawPixel(-x, -y);
if (d2 > 0) {
y--;
dy -= 2 * a * a;
d2 += a * a - dy;
} else {
y--;
x++;
dx += 2 * b * b;
dy -= 2 * a * a;
d2 += dx - dy + a * a;
}
}
}
void drawRectangle(int x1, int y1, int x2, int y2) {
drawLine(x1, y1, x2, y1);
drawLine(x2, y1, x2, y2);
drawLine(x2, y2, x1, y2);
drawLine(x1, y2, x1, y1);
}
void fillRectangle(int x1, int y1, int x2, int y2) {
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
drawPixel(i, j);
}
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
if (fillRect) {
fillRectangle(x1, y1, x2, y2);
} else {
drawRectangle(x1, y1, x2, y2);
}
drawCircle(radius, x, y);
drawEllipse(a, b, x, y);
glFlush();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'r':
glColor3f(1.0, 0.0, 0.0); // 红色
break;
case 'g':
glColor3f(0.0, 1.0, 0.0); // 绿色
break;
case 'b':
glColor3f(0.0, 0.0, 1.0); // 蓝色
break;
case 'y':
glColor3f(1.0, 1.0, 0.0); // 黄色
break;
case 'm':
glColor3f(1.0, 0.0, 1.0); // 紫色
break;
case 'c':
glColor3f(0.0, 1.0, 1.0); // 青色
break;
case 'w':
glColor3f(1.0, 1.0, 1.0); // 白色
break;
case 'f':
fillRect = !fillRect;
break;
case 27: // ESC键
exit(0);
break;
default:
break;
}
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
x1 = x;
y1 = height - y;
} else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
x2 = x;
y2 = height - y;
glutPostRedisplay();
} else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
x = x;
y = height - y;
radius = 0;
} else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
x = x;
y = height - y;
radius = (int) sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
glutPostRedisplay();
} else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) {
x = x;
y = height - y;
a = abs(x - x1);
b = abs(y - y1);
} else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP) {
x = x;
y = height - y;
a = abs(x - x1);
b = abs(y - y1);
glutPostRedisplay();
}
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glutCreateWindow("Painter's Algorithm");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
```
注意:以上代码仅供参考,实际应用时需要根据具体的需求进行修改和调整。
阅读全文