直线的DDA算法
#include <windows.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <stdio.h>
#include <math.h>
int getValue(){
//scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
int k;
scanf("%d",&k);
return k;
}
void MyInit()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
void LineDDA()
{
//getValue();
glClear(GL_COLOR_BUFFER_BIT);
int x1,y1, x2,y2;
//x1=0,y1=0,x2=500,y2=500;
x1=getValue();
y1=getValue();
x2=getValue();
y2=getValue();
int k;
float x,y,dx,dy;
if(abs(x2-x1)>=abs(y2-y1))
{
k=abs(x2-x1);
}
else
{
k=abs(y2-y1);
}
dx=float(x2-x1)/k;
dy=float(y2-y1)/k;
x=float(x1);
y=float(y1);
glBegin(GL_POINTS);
for(GLint i=0;i<k;i++)
{
glVertex2i((int)(x+0.5),(int)(y+0.5));
//putpixel(int(x+0.5),int(y+0.5));
x=x+dx;
y+=dy;
}
glEnd();
glFlush();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv); //初始化工具包
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);//设置显示的模式
glutInitWindowPosition(100,100);//设置屏幕上窗口的位置
glutInitWindowSize(400,400);// 设置窗口的大小
glutCreateWindow("Hello");// 打开带窗口的大小
glutDisplayFunc(&LineDDA);//注册重画回掉函数
MyInit();
glutMainLoop(); //进入循环
return 0;
}