Advertisement
HugoBallee

circles.cpp

Feb 28th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. // File triangles.cpp
  2. // Hugo Gallée
  3. // Sat Feb 25 13:02:15 CET 2012
  4.  
  5. #include <GL/glut.h>
  6. #include <math.h>
  7.  
  8. #define _USE_MATH_DEFINES
  9.  
  10. void circle(float x, float y, float radius, int n) {
  11.   float teta;
  12.   glBegin (GL_LINE_LOOP);
  13.     glColor3f (1.0, 0.0, 0.0);
  14.     for (int i = 0; i < n; i++) {
  15.       teta = i * 2 * M_PI / n;
  16.       glVertex3f (radius * sin(teta) + x, radius * cos(teta) + y, 0.0);
  17.     }
  18.   glEnd();
  19. }
  20.  
  21. void init (void) {
  22.   // select clearing color
  23.   glClearColor (1.0, 1.0, 1.0, 0.0);
  24.  
  25.   // initialize viewing values
  26.   glMatrixMode(GL_PROJECTION);
  27.   glLoadIdentity();
  28.   glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
  29. }
  30.  
  31. void display(void) {
  32.   // clear all pixels
  33.   glClear (GL_COLOR_BUFFER_BIT);
  34.  
  35.   int nbSegments = 40;
  36.   float teta = 1.0;
  37.   float scale = 1.2;
  38.  
  39.   float radius = 0.4;
  40.   float x = 0.5;
  41.   float y = 0.5;
  42.   while (radius > 0.02) {
  43.     circle(x, y, radius, nbSegments);
  44.     x += (radius - radius / scale) * sin(teta);
  45.     y += (radius - radius / scale) * cos(teta);
  46.     radius /= scale;
  47.     circle(x, y, radius, nbSegments);
  48.     teta += 0.5;
  49.   }
  50.  
  51.   // Swap the buffers to show the oneon which we writed
  52.   glutSwapBuffers();
  53. }
  54.  
  55. /*
  56.  * Declare initial window size, position, and display mode
  57.  * (double buffer and RGB).  Open window with "hello"
  58.  * in its title bar.  Call initialization routines.
  59.  * Register callback function to display graphics.
  60.  * Enter main loop and process events.
  61.  */
  62. int main(int argc, char** argv) {
  63.   glutInit(&argc, argv);
  64.   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  65.   glutInitWindowSize (500, 500);
  66.   glutInitWindowPosition (100, 100);
  67.   glutCreateWindow ("circles");
  68.   init ();
  69.   glutDisplayFunc(display);
  70.   glutMainLoop();
  71.   return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement