/* chap3-59.cpp * This program draws a yin-yang symbol. * 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 drawSolidArc(Point2 center, float radius, float startAngle, float sweep) { // startAngle and sweep are in degrees const int n = 40; // number of intermediate segments in arc float angle = startAngle * 3.14159265 / 180; // initial angle in radians float angleInc = sweep * 3.14159265 /(180 * n); // angle increment float cx = center.getX(), cy = center.getY(); // cvs.moveTo(cx + radius * cos(angle), cy + radius * sin(angle)); // angle += angleInc; // for(int k = 0; k < n; k++, angle += angleInc) // cvs.lineTo(cx + radius * cos(angle), cy + radius * sin(angle)); glBegin(GL_POLYGON); // glVertex2f(cvs.getCP().getX(), cvs.getCP().getY()); glVertex2f(cx, cy); glVertex2d(cx + radius * cos(angle), cy + radius * sin(angle)); angle += angleInc; for(int k = 0; k < n; k++, angle += angleInc) glVertex2f(cx + radius * cos(angle), cy + radius * sin(angle)); glEnd(); } void myDisplay(void) { Point2 p; cvs.clearScreen(); // clear the screen cvs.setWindow(-worldWidth, worldWidth, -worldHeight, worldHeight); cvs.setViewport(0, screenWidth, 0, screenHeight); p.set(0.0, 0.0); drawSolidArc(p, radius, 0.0, 180.0); p.set(0.0 - radius/2, 0.0); drawSolidArc(p, radius/2, 180.0, 180.0); cvs.setColor (1.0, 1.0, 1.0); // re-set the drawing color drawSolidArc(p, radius/8, 0.0, 360.0); p.set(0.0 + radius/2, 0.0); drawSolidArc(p, radius/2, 0.0, 180.0); cvs.setColor (0.0, 0.0, 0.0); // re-set the drawing color drawSolidArc(p, radius/8, 0.0, 360.0); ngon(50, 0.0, 0.0, radius, 0.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; }