/* draw_ngons.cpp * This program demonstrates the drawing of a set of regular polygons * Click the right button to exit. */ #include << X11/Xlib.h> #include << fstream.h> #include << math.h> #include << GL/glut.h> #include << GL/gl.h> #include << stdlib.h> #include << stdio.h> #include "Canvas.h" const int numVerts = 6; const double radius = 2.0; const double worldWidth = 4.0; const double worldHeight = 3.0; const int screenWidth = 640; const int screenHeight = 480; Canvas cvs(screenWidth, screenHeight, "Try out Canvas"); void myInit(void) { cvs.setBackgroundColor (1.0, 1.0, 1.0); // set black background color cvs.setColor (0.0, 0.0, 0.0); // set the drawing color glPointSize (4.0); // set dot size 2 x 2 } void ngon(int n, float cx, float cy, float radius, float rotAngle) { // assumes global Canvas object, cvs if(n < 3) return; // bad number of sides double angle = rotAngle * 3.14159265 / 180; // initial angle double angleInc = 2 * 3.14159265 /n; //angle increment cvs.moveTo(radius * cos(angle) + cx, radius * sin(angle) + cy); for(int k = 0; k < n; k++) // repeat n times { angle += angleInc; cvs.lineTo(radius * cos(angle) + cx, radius * sin(angle) + cy); } } void myDisplay(void) { double i = 0.0; cvs.clearScreen(); // clear the screen cvs.setWindow(-worldWidth, worldWidth, -worldHeight, worldHeight); cvs.setViewport(0, screenWidth, 0, screenHeight); while (i < 360) { ngon(numVerts, 0.0, 0.0, radius, i); i += 17.0; } } void myMouse(int button, int state, int x, int y) { switch (button) { case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) exit (-1); break; default: break; } } int main(int argc, char** argv) { myInit (); glutDisplayFunc(myDisplay); // register redraw function glutMouseFunc(myMouse); // register myMouse function glutMainLoop(); // go into a perpetual loop return 0; }