直线在实际上是一个点、一个点构成的 ,采用DDA的方法划线实际上是利用这种特征。
具体的方法和步骤如下:
DDA
1.给起始点 终点
2.算△x 和△y
3.比较两者大小
4.:steps= △x>△y?△x△:y;
5.x方向步长 stepX=△x/steps;
stepY=△y/steps;
6.循环 每次循
#include <iostream>
#include <GL/glut.h>
#include <math.h>
using namespace std;
//DDA
void myDDA(GLfloat x1,GLfloat y1,GLfloat xn,GLfloat yn)//起始点、终点
{
float dx = fabs(xn - x1);
float dy = fabs(y1 - yn);
float steps = dx > dy ? dx : dy;
float stepX = dx / steps;
float stepY = dy / steps;
glBegin(GL_POINTS);
for (int i = 0; i < (int)steps; i++)
{
glVertex2f(x1, y1);
x1 += 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, 500);
}
void main(int argc, char* argv[])
{
glutInit(& argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(300, 100);
glutInitWindowSize(600, 500);
glutCreateWindow("DDA applicaton");
init();
glutDisplayFunc(myDisplay);
glutMainLoop();
}
环更新点