Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <vector>
- using namespace std;
- const float fps = 0.01;
- struct P {
- float x;
- float y;
- float vx;
- float vy;
- float m;
- };
- vector<P> p;
- void display() {
- glClear(GL_COLOR_BUFFER_BIT);
- for (vector<P>::iterator i = p.begin(); i != p.end(); ++i) {
- if (i->m <= 2) {
- glPointSize(2);
- glBegin(GL_POINTS);
- glVertex2f(i->x, i->y);
- glEnd();
- } else {
- glBegin(GL_LINES);
- for (int a = 0; a < 36; ++a) {
- float x = i->m * cos(a * 3.14 / 18);
- float y = i->m * sin(a * 3.14 / 18);
- glVertex2f(i->x + x, i->y + y);
- x = i->m * cos((a + 1) * 3.14 / 18);
- y = i->m * sin((a + 1) * 3.14 / 18);
- glVertex2f(i->x + x, i->y + y);
- }
- glEnd();
- }
- }
- glutSwapBuffers();
- }
- void timer(int = 0) {
- for (vector<P>::iterator i = p.begin(); i != p.end(); ++i)
- for (vector<P>::iterator j = p.begin(); j != p.end(); ++j)
- if (i != j) {
- float d = sqrt((i->x - j->x) * (i->x - j->x) +
- (i->y - j->y) * (i->y - j->y));
- if (d < i->m + j->m) {
- float f = 50 * (i->m + j->m - d);
- i->vx += f * (i->x - j->x) / d / i->m * fps;
- i->vy += f * (i->y - j->y) / d / i->m * fps;
- j->vx -= f * (i->x - j->x) / d / j->m * fps;
- j->vy -= f * (i->y - j->y) / d / j->m * fps;
- }
- }
- for (vector<P>::iterator i = p.begin(); i != p.end(); ++i) {
- i->x += i->vx * fps;
- i->y += i->vy * fps;
- if (i->x < 0)
- i->x += 480;
- if (i->y < 0)
- i->y += 480;
- if (i->x > 480)
- i->x -= 480;
- if (i->y > 480)
- i->y -= 480;
- }
- display();
- glutTimerFunc(10, timer, 0);
- }
- int main(int argc, char **argv) {
- P p0 = {480 / 2, 480 / 2, 0, 0, 40};
- p.push_back(p0);
- for (int i = 0; i < 100; ++i) {
- P p0 = {rand() % 480, rand() % 480, rand() % 100000 / 500.0 - 100,
- rand() % 100000 / 500.0 - 100, 4};
- p.push_back(p0);
- }
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(480, 480);
- glutInitWindowPosition(20, 1050 - 480 - 20);
- glutCreateWindow("Молекулы");
- glClearColor(0, 0, 0, 1.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 480, 480, 0, -1, 1);
- glutDisplayFunc(display);
- timer();
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement