Advertisement
d_skat

Untitled

Apr 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <cmath>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. const float fps = 0.01;
  7.  
  8. struct P {
  9.     float x;
  10.     float y;
  11.     float vx;
  12.     float vy;
  13.     float m;
  14. };
  15.  
  16. vector<P> p;
  17.  
  18. void display() {
  19.     glClear(GL_COLOR_BUFFER_BIT);
  20.     for (vector<P>::iterator i = p.begin(); i != p.end(); ++i) {
  21.         if (i->m <= 2) {
  22.             glPointSize(2);
  23.             glBegin(GL_POINTS);
  24.             glVertex2f(i->x, i->y);
  25.             glEnd();
  26.         } else {
  27.             glBegin(GL_LINES);
  28.             for (int a = 0; a < 36; ++a) {
  29.                 float x = i->m * cos(a * 3.14 / 18);
  30.                 float y = i->m * sin(a * 3.14 / 18);
  31.                 glVertex2f(i->x + x, i->y + y);
  32.                 x = i->m * cos((a + 1) * 3.14 / 18);
  33.                 y = i->m * sin((a + 1) * 3.14 / 18);
  34.                 glVertex2f(i->x + x, i->y + y);
  35.             }
  36.             glEnd();
  37.         }
  38.     }
  39.     glutSwapBuffers();
  40. }
  41.  
  42. void timer(int = 0) {
  43.     for (vector<P>::iterator i = p.begin(); i != p.end(); ++i)
  44.         for (vector<P>::iterator j = p.begin(); j != p.end(); ++j)
  45.             if (i != j) {
  46.                 float d = sqrt((i->x - j->x) * (i->x - j->x) +
  47.                                (i->y - j->y) * (i->y - j->y));
  48.                 if (d < i->m + j->m) {
  49.                     float f = 50 * (i->m + j->m - d);
  50.                     i->vx += f * (i->x - j->x) / d / i->m * fps;
  51.                     i->vy += f * (i->y - j->y) / d / i->m * fps;
  52.                     j->vx -= f * (i->x - j->x) / d / j->m * fps;
  53.                     j->vy -= f * (i->y - j->y) / d / j->m * fps;
  54.                 }
  55.             }
  56.     for (vector<P>::iterator i = p.begin(); i != p.end(); ++i) {
  57.         i->x += i->vx * fps;
  58.         i->y += i->vy * fps;
  59.         if (i->x < 0)
  60.             i->x += 480;
  61.         if (i->y < 0)
  62.             i->y += 480;
  63.         if (i->x > 480)
  64.             i->x -= 480;
  65.         if (i->y > 480)
  66.             i->y -= 480;
  67.     }
  68.     display();
  69.     glutTimerFunc(10, timer, 0);
  70. }
  71.  
  72. int main(int argc, char **argv) {
  73.     P p0 = {480 / 2, 480 / 2, 0, 0, 40};
  74.     p.push_back(p0);
  75.     for (int i = 0; i < 100; ++i) {
  76.         P p0 = {rand() % 480, rand() % 480, rand() % 100000 / 500.0 - 100,
  77.                 rand() % 100000 / 500.0 - 100, 4};
  78.         p.push_back(p0);
  79.     }
  80.     glutInit(&argc, argv);
  81.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  82.     glutInitWindowSize(480, 480);
  83.     glutInitWindowPosition(20, 1050 - 480 - 20);
  84.     glutCreateWindow("Молекулы");
  85.     glClearColor(0, 0, 0, 1.0);
  86.     glMatrixMode(GL_PROJECTION);
  87.     glLoadIdentity();
  88.     glOrtho(0, 480, 480, 0, -1, 1);
  89.     glutDisplayFunc(display);
  90.     timer();
  91.     glutMainLoop();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement