Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <bevgrafmath2017.h>
  3.  
  4. int mainCircleRad = 30, dotsRad = 5, dotsCount = 10;
  5. float circleSpeed = 0.5f;
  6. vec2 dotsPoses[10];
  7.  
  8. float width = 800, height = 600;
  9. GLint novX = 1, novY=1;
  10. GLint halfSize = 50;
  11. vec2 center = { width/2, height/2 };
  12.  
  13.  
  14.  
  15.  
  16. void init()
  17. {
  18.     glClearColor(1.0, 1.0, 1.0, 0.0);
  19.     glMatrixMode(GL_PROJECTION);
  20.     gluOrtho2D(0.0, width, 0.0, height);
  21.     glShadeModel(GL_FLAT);
  22. }
  23.  
  24. void Circle(vec2 center,float r, bool fill = false) {
  25.    
  26.     int segments = 200;
  27.    
  28.     glLineWidth(2);
  29.     glColor3ub(0, 0, 0);
  30.  
  31.     if(fill)
  32.         glBegin(GL_POLYGON);
  33.     else
  34.         glBegin(GL_LINE_LOOP);
  35.  
  36.     for (float t = 0; t <two_pi(); t += (pi() / 180))
  37.     {
  38.         float x = r * cosf(t) + center[0];
  39.         float y = r * sinf(t) + center[1];
  40.  
  41.         glVertex2f(x, y);
  42.     }
  43.  
  44.  
  45.     glEnd();
  46.  
  47.     glFlush();
  48. }
  49.  
  50.  
  51. void GeneratePoses()
  52. {
  53.     for (int i = 0; i < dotsCount; i++) {
  54.        
  55.         dotsPoses[i].x = rand();
  56.         while (dotsPoses[i].x  < dotsRad || dotsPoses[i].x  > width- dotsRad)
  57.             dotsPoses[i].x = rand();
  58.  
  59.         dotsPoses[i].y = rand();
  60.         while (dotsPoses[i].y  < dotsRad || dotsPoses[i].y  > height- dotsRad)
  61.             dotsPoses[i].y = rand();
  62.     }
  63. }
  64.  
  65. bool lock = false;
  66.  
  67. void display()
  68. {
  69.     glClear(GL_COLOR_BUFFER_BIT);
  70.     glColor3f(0.0, 0.0, 1.0);
  71.     Circle(center, mainCircleRad);
  72.    
  73.     if (!lock)
  74.     {
  75.         GeneratePoses();
  76.         lock = true;
  77.     }
  78.  
  79.     for (int c = 0; c < dotsCount; c++)
  80.     {
  81.         if (dist(dotsPoses[c], center) < mainCircleRad)
  82.         {
  83.             dotsPoses[c] = vec2{ -100,-100 };
  84.             mainCircleRad += 5;
  85.             continue;
  86.         }
  87.         Circle(dotsPoses[c], dotsRad, true);
  88.     }
  89.     glutSwapBuffers();
  90. }
  91.  
  92. void update(int n)
  93. {
  94.     center[0] += novX * circleSpeed;
  95.     center[1] += novY * circleSpeed;
  96.    
  97.     if (center[0] + mainCircleRad > width || center[0] - mainCircleRad < 0)
  98.     {
  99.         novX *= -1;
  100.         center[0] += novX * 5;
  101.     }
  102.  
  103.     if (center[1] + mainCircleRad > height || center[1] - mainCircleRad < 0)
  104.     {
  105.         novY *= -1;
  106.         center[1] += novY * 5;
  107.     }
  108.  
  109.     glutPostRedisplay();
  110.  
  111.     glutTimerFunc(5, update, 0);
  112.  
  113. }
  114.  
  115. void keyboard(unsigned char key, int x, int y)
  116. {
  117.     switch (key) {
  118.     case 27:
  119.         exit(0);
  120.         break;
  121.     case 'w':
  122.         novX *= -1;
  123.         break;
  124.     case 'd':
  125.         novY *= -1;
  126.         break;
  127.     }
  128. }
  129.  
  130. int main(int argc, char** argv)
  131. {
  132.     glutInit(&argc, argv);
  133.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  134.     glutInitWindowSize(width, height);
  135.     glutInitWindowPosition(100, 100);
  136.     glutCreateWindow(argv[0]);
  137.     init();
  138.     glutDisplayFunc(display);
  139.     glutKeyboardFunc(keyboard);
  140.     glutTimerFunc(5, update, 0);
  141.     glutMainLoop();
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement