//drawLine1 //showing two examples of the function drawLine() //void drawLine(int x1, int y1, int x2, int y2) // T. Kosaka CS TNCT 2001 #include #include #include void setColor(float red,float green,float blue) { glColor3f(red, green, blue); } void drawDot(int x,int y) { glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } /* void drawLine(int x1, int y1, int x2, int y2) { int i, len, width = x2 - x1, height = y2 - y1; int delta; float width_diff, height_diff; if(abs(width) > abs(height)) { len=abs(width); delta=width/len; height_diff = (float)height / (float)len; for(i = 0; i <= len; i++) drawDot((int)(x1 + i * delta), (int)(y1 + i * height_diff+0.5)); } else { len=abs(height); delta=height/len; width_diff = (float)width / (float)len; for(i = 0; i <= len; i++) drawDot((int)(x1 + i * width_diff+0.5), (int)(y1 + i * delta)); } } */ void drawLine(int x1, int y1, int x2, int y2) { int i, len, width = x2 - x1, height = y2 - y1; float width_diff, height_diff; if(abs(width) > abs(height)) len = abs(width); else len = abs(height); width_diff = (float)width / (float)len; height_diff = (float)height / (float)len; for(i = 0; i <= len; i++) drawDot((int)(x1 + i * width_diff+0.5), (int)(y1 + i * height_diff+0.5)); } void userdraw(void); void display(void) { glClear( GL_COLOR_BUFFER_BIT); userdraw(); glutSwapBuffers(); } void userdraw(void) { setColor(1,1,1); drawLine(320+20,240+10,320+200,240+100); drawLine(320+10,240+20,320+100,240+200); drawLine(320-10,240+20,320-100,240+200); drawLine(320-20,240+10,320-200,240+100); drawLine(320-20,240-10,320-200,240-100); drawLine(320-10,240-20,320-100,240-200); drawLine(320+10,240-20,320+100,240-200); drawLine(320+20,240-10,320+200,240-100); } int main(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowPosition(100,100); glutInitWindowSize(640,480); glutCreateWindow ("Line 1"); glClearColor(0.0, 0.0, 0.0, 0.0); gluOrtho2D(0., 640., 0.0, 480.0); // Define the dimensions of the Orthographic Viewing Volume glutDisplayFunc(display); glutMainLoop(); return 0; }