要求:构造完整的DDA画线算法程序
DDA算法:计算机图形学中基于直线的微分方程来生成直线的算法。
具体步骤:
- 输入两个端点,将左端点保存在中;
- 计算;
- 根据m的值,分情况计算:
①
#include <iostream>
#include <windows.h>
#include <GL/glut.h>
using namespace std;
GLint x1,y1,x2,y2;
void drawLine(GLint x1,GLint y1,GLint x2,GLint y2)
{
float m = 1.0*(y2-y1)/(x2-x1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
float x,y;
if (m>=-1 || m<=1)
for(x=x1,y=y1;x<=x2;x++,y+=m)
glVertex2f(x,y);
else if (m>1) {
m=1/m;
for(x=x1,y=y1;y<=y2;x+=m,y++)
glVertex2f(x,y);
} else {
m=1/m;
for(x=x1,y=y1;y<=y2;x-=m,y++)
glVertex2f(x,y);
}
glEnd();
glFlush();
}
void display(void)
{
glClearColor(1,1,1,1);
glColor3f(1,0,0);
gluOrtho2D(0,400,0,300);
glClear(GL_COLOR_BUFFER_BIT);
drawLine(x1,y1,x2,y2);
}
int main()
{
glutInitWindowSize(400, 300);
glutCreateWindow("DDA Algorithm");
cout<<"请输入起点(x1,y1)和终点(x2,y2)的坐标:\n"<<"(0<x<400,0<y<300)"<<endl;
cin>>x1>>y1>>x2>>y2;
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
网友评论