// File: spinmen.cxx // Written by Michael Main (main@colorado.edu) // This program opens a large, square graphics window and // draws five tilted snowmen of various sizes and locations. // The program then does an animation (with double buffering), // so that the snowmen's heads spin around their bodies. // Directives: #include #include #include #include using namespace std; // Named constants: const int S = 500; // Width and height of the graphics window. const int X3 = int(0.93*S); // Prototypes: // This function draws a snowman with the center of the body // at (x,y), the radius of the body given by the 3rd // paramter. The head is at an angle of theta radians from the // center of the body (with 0 radians being to the right), // and the head has a radius that is half that of the body. void draw_snowman(int x, int y, int radius, float theta); // Function definitions: //---------------------------------------------------------- int main( ) { double delta = 0.0; // How far have the head's spun so far? // 1. Initialize the graphics window w/double buffering: initwindow(S, S, "At the north pole!", 0, 0, true); while (true) { // 2-1. Draw five tilted snowmen: clearviewport( ); draw_snowman(S/5, S/3, S/17, M_PI/2 + delta); draw_snowman(S/5, 2*S/3, S/50, M_PI/4 + delta); draw_snowman(X3, int(0.64*S), S/6, -M_PI/2 + delta); draw_snowman(int(0.88*S), int(0.12*S), S/73, 3.14159 + delta); draw_snowman(int(0.66*S), int(0.1*S), S/39, 0 + delta); // 2-2. Swap and delay: swapbuffers( ); delay(1000/30); // One 30th of a second // 2-3. Computations for next frame: delta -= 0.1; } // 3. Finish: return EXIT_SUCCESS; } //---------------------------------------------------------- //---------------------------------------------------------- void draw_snowman(int x, int y, int radius, float theta) { int hradius = radius/2; int d = radius + hradius; int hx = int(x + d*cos(theta)); int hy = int(y - d*sin(theta)); circle(x, y, radius); // This draws the body circle(hx, hy, hradius); // This draws the head } //----------------------------------------------------------