版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LY_624/article/details/72833964
// 改进Bresenham画直线
#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 BresenhamLine(int x0,int y0,int x1,int y1)
{
int dx,dy,e,x,y;
dx=x1-x0;
dy=y1-y0;
e=-dx;
x=x0;
y=y0;
while(x<=x1)
{
putpixel(x,y);
x++;
e=e+2*dy;
if(e>0)
{
y++;
e=e-2*dx;
}
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
BresenhamLine(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("改进Bresenham画直线");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
运行结果: