计算机图形学上机实验之DDV算法

来源:互联网 发布:电脑flash制作软件 编辑:程序博客网 时间:2024/06/05 20:37
#include <gl/glut.h>//实用工具包#include<math.h>#include<stdio.h>#include<windows.h>#include<gl/gl.h>#include<gl/glu.h>#include<gl/glaux.h>#include<stdlib.h>struct GLintPoint{GLint x;GLint y;};GLintPoint p[2];bool sig=false;int scrw=640,scrh=480;void drawline(){int dx=p[1].x-p[0].x;int dy=p[1].y-p[0].y;int steps,k;float xIncrement,yIncrement;float x=p[0].x,y=p[0].y;if (fabs(dx)>fabs(dy)){steps=fabs(dx);}else {steps=fabs(dy);}xIncrement=float(dx)/float(steps);//计算两个方向增量yIncrement=float(dy)/float(steps);glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glBegin(GL_LINES);glVertex2i(x,y);//绘制起点for(k=0;k<steps;k++){x+=xIncrement;y+=yIncrement;glVertex2i(x,y);}glEnd();glFlush();}void myDisplay(){glClear(GL_COLOR_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glColor3f(0.0,0.0,1.0);if(sig)drawline();glutSwapBuffers();}void  myMouse(int button,int state,int x,int y){if(button==GLUT_LEFT_BUTTON&& state==GLUT_DOWN){p[0].x=x;p[0].y=scrh-y;sig=true;}glutPostRedisplay();}void myMotion(int x,int y){p[1].x=x;p[1].y=scrh-y;glutPostRedisplay();}int main(int argc,char **argv){glutInit(&argc,argv);glutInitWindowSize(scrw,scrh);glutInitWindowPosition(0,0);glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);    glutCreateWindow("gujinjie");glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0,scrw,0,scrh);glMatrixMode(GL_MODELVIEW);glClearColor(0.0f,0.0f,0.0f,0.0f);glViewport(0,0,scrw,scrh);glutMouseFunc(myMouse);glutDisplayFunc(myDisplay);glutMotionFunc(myMotion);glutMainLoop();return 0;}

0 0
原创粉丝点击