DAA算法流程:
1.有了起始点(x1,y1)和终点(xn,yn)
2.△x=|xn-x1|,△y=|yn-y1|
3.比较△x和△y的大小
4.steps=△x和△y的大者
5.stepx=△x/steps,stepy=△y/steps
#include <glut.h>
#include<math.h>
void myDDA(GLfloat xl, GLfloat y1, GLfloat xn, GLfloat yn)
{
float dx = fabs(xn - xl);
float dy = fabs(yn - y1);
float steps;//步长
if (dx > dy)
steps = dx;
else
steps = dy;
float stepX = dx / steps;
float stepY = dy / steps;
glBegin(GL_POINTS);//画点
//画线
for (int i = 0; i < (int)steps; i++)
{
glVertex2f(xl, y1);
xl += stepX;
y1 += stepY;
}
glEnd();
}
//绘制函数
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1);
glColor3f(0.9, 0.5, 0.8);
myDDA(1.5, 3.8, 189.8, 267.5);
glFlush();
}
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 600, 0, 5