/* This program gives a basic introduction to "designs and mathematics" */ # include # include # include const float pi = 22.0f/7.0f; void DrawSineWave(void) { int loop; const int samples = 45; const float amplitude = 20.0f; const float wavelength = 250.0f; const float dTheta = 2.0f*pi/samples; float theta = -pi; glBegin(GL_LINE_STRIP); for(loop = 0; loop <= samples; loop++) { float x = (theta/pi)*wavelength*0.5f; float y = amplitude*sinf(theta); theta += dTheta; glVertex2f(x, y); } glEnd(); } void DrawEllipseAt(float x, float y, float a, float b) { const int samples = 10; const float dTheta = 2.0f*pi/samples; float theta = 0.0f; int loop; a *= 1.5f; b *= 1.2f; glBegin(GL_LINE_STRIP); for(loop = 0; loop <= samples; loop++) { float x1 = x + a*cosf(theta); float y1 = y + b*sinf(theta); glVertex2f(x1, y1); theta += dTheta; } glEnd(); } void DrawRing(float x, float y, float a, float b) { int loop; const int samples = 14; const float dTheta = 2.0f*pi/samples; float theta = 0.0f; for(loop = 0; loop <= samples; loop++) { float x1 = x + a * cosf(theta); float y1 = y + b * sinf(theta); DrawEllipseAt(x1, y1, a, b); theta += dTheta; } } void Draw4CirclesAt(float x, float y) { glColor3f(0.4f, 0.4f, 1.0f); DrawEllipseAt(x, y + 3.0f, 2.0f, 2.0f); DrawEllipseAt(x, y - 3.0f, 2.0f, 2.0f); DrawEllipseAt(x + 5.0f, y, 3.0f, 2.0f); DrawEllipseAt(x - 5.0f, y, 3.0f, 2.0f); glColor3f(1.0f, 0.1f, 0.1f); DrawEllipseAt(x, y + 3.0f, 3.0f, 3.0f); DrawEllipseAt(x, y - 3.0f, 3.0f, 3.0f); DrawEllipseAt(x + 5.0f, y, 4.0f, 3.0f); DrawEllipseAt(x - 5.0f, y, 4.0f, 3.0f); } void Draw(void) { // r g b a glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.9f, 0.9f, 0.0f); DrawSineWave(); glColor3f(0.0f, 1.0f, 0.0f); DrawRing(0.0f, 0.0f, 15.0f, 6.0f); DrawRing(35.0f, 15.0f, 6.0f, 4.0f); DrawRing(-35.0f, -15.0f, 6.0f, 4.0f); DrawRing(-60.0f, -20.0f, 4.0f, 2.0f); DrawRing(60.0f, 20.0f, 4.0f, 2.0f); Draw4CirclesAt(10.0f, 20.0f); Draw4CirclesAt(48.0f, 2.0f); Draw4CirclesAt(-10.0f, -20.0f); Draw4CirclesAt(-48.0f, -2.0f); glFlush(); } void OnWindowResize(int width, int height) { //See how the output changes if you change left and top int left = 0; int bottom = 0; glViewport(left, bottom, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-width/2.0, width/2.0, -height/2.0, height/2.0); } void OnKeyPress(unsigned char key, int x, int y) { # define ESC_Key 27 switch (key) { case ESC_Key: exit(0); break; } //printf("\nThe mouse is @..(%d, %d)\n", x, y); } int main(int argc, char** argv) { const int left = 0; const int top = 0; const int width = 512; const int height = 256; const char* windowTitle = "Hail Queen Mathematics!!!"; glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(width, height); glutInitWindowPosition(left, top); glutCreateWindow(windowTitle); glutDisplayFunc(Draw); glutReshapeFunc(OnWindowResize); glutKeyboardFunc(OnKeyPress); glutMainLoop(); return 0; } /* EXERCISE: Show the output to your girl friend and modify the design as she suggests */ // // MORAL of this tutorial // ~~~~~~~~~~~~~~~~~~~~~~~~ // If you are good at maths, you can play with curves ;) // (of course... PUN is always intended) //