#include <math.h>
#include <stdio.h>
void drawline() //画坐标轴线
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f); //坐标线为黑色
glBegin(GL_LINES);
float i=0.0f;
for( i=0.0f;i<1.0f;i=i+0.02f)
{
//第一象限坐标
glVertex2f(0, i);
glVertex2f(1, i);
glVertex2f(i, 0);
glVertex2f(i, 1);
//第四象限坐标
glVertex2f(i,0);
glVertex2f(i,-1);
glVertex2f(0,-i);
glVertex2f(1,-i);
}
for( i=0.0f;i>-1.0f;i=i-0.02f)
{
//第二象限坐标
glVertex2f(i, 0);
glVertex2f(i, 1);
glVertex2f(0,-i);
glVertex2f(-1,-i);
//第三象限坐标
glVertex2f(i,0);
glVertex2f(i,-1);
glVertex2f(0,i);
glVertex2f(-1,i);
}
glEnd();
glFlush();
}
void putpixel(float x,float y) //绘制一个点,这里用正方形表示
{
glPointSize(3.0f);
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glFlush();
}
//扫描转换直线段的中点算法
void MidPointLine(float x0,float y0,float x1,float y1)
{
printf("中心算法!\n");
glColor3f(0.0f,0.0f,0.0f);
float dx,dy, incrE,incrNE,d,x,y;
dx=x1-x0;
dy=y1-y0;
d=dx-2*dy;
incrE=-2*dy;
incrNE=2*(dx-dy);
x=x0;y=y0;
putpixel(x,y);
while(x<x1)
{
if(d>0.0f)
{
d+=incrE;
}
else
{
d+=incrNE;
y=y+0.02f;
}
x=x+0.02f;
putpixel(x,y);
}
}
void myDisplay(void)
{
drawline();
MidPointLine(-1.0f,-1.0f,1.0f,0.6f);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow("扫描转换直线段的中点算法");
glClearColor(1.0f,1.0f,1.0f,0.0f); //设置窗口颜色为白色
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}