/* -*- C -*-
*
* point.c - 中点画线算法 *
*
* Created On : Wed Apr 16 12:37:37 2008
* Last Modified On: Wed Apr 16 13:03:43 2008
*
*/
#include <GL/glut.h> /* glut.h includes gl.h and glu.h*/
#include <stdio.h>
void midLine( int x0,int y0,int x1,int y1 ){
int dx,dy,incrE,incrNE,d,d_pre,x,y;
dx = x1 - x0;
dy = y1 - y0;
d = dy * 2 - dx;
incrE = dy * 2;
incrNE = ( dy - dx ) * 2;
x = x0;
y = y0;
while( x < x1 ){
if( d <= 0 ){
d_pre = d;
d+= incrE;
x ++;
}
else {
d_pre = d;
d += incrNE;
x ++;
y ++;
}
glBegin(GL_POINTS);
//Note the convetion.
glVertex2f((float)x/300,(float)y/300);
glEnd();
}
}
void display(void)
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
glPointSize( 2.0 );
midLine( -200,-200,200,200 );
/* flush GL buffers */
glFlush();
}
int main(int argc, char** argv)
{
/* Initialize mode and open a window in upper left corner of screen */
/* Window title is name of program (arg[0]) */
glutInit(&argc,argv);
/* glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0); */
glutCreateWindow("MiddleLine");
glutDisplayFunc(display);
glutMainLoop();
}
|