版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LY_624/article/details/72833917
// DDA画直线
#include "stdafx.h"
#include <gl/glut.h>
#include <cmath>
void init(void)
{
glClearColor(1.0,1.0,1.0,1.0); //设置背景颜色为白色
glMatrixMode(GL_PROJECTION); //对投影相关进行操作
gluOrtho2D(0.0, 30.0, 0.0, 30.0);
}
void putpixel(int x, int y)
{
glColor3f(1.0, 0.0, 0.0);
glPointSize(2.0f);
glBegin(GL_POINTS);
glVertex2f(15+x, 15+y);
glEnd();
glFlush();
}
void DDALine(int x0,int y0,int x1,int y1)
{
int dx,dy,epsl,k;
float x,y,xIncre,yIncre;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
if(abs(dx)>abs(dy))
epsl=abs(dx);
else
epsl=abs(dy);
xIncre=(float)dx/(float)epsl;
yIncre=(float)dy/(float)epsl;
for(k=0;k<=epsl;k++)
{
putpixel(int(x+0.5),(int)(y+0.5));
x+=xIncre;
y+=yIncre;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
DDALine(0,0,8,6);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(0,0);
glutCreateWindow("DDA画直线");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
运行结果: