OPENGL—DDA画直线

来源:互联网 发布:office 2017 mac 破解 编辑:程序博客网 时间:2024/06/10 20:29
// 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);elseepsl=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; }

运行结果:



原创粉丝点击