1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <cmath> // Constants const int WIDTH = 800; const int HEIGHT = 600; const int PARTICLE_COUNT = 100; const float STEP_SIZE = 1.0f; // Vector2 class for 2D vector operations class Vector2 { public: float x, y; Vector2(float x = 0, float y = 0) : x(x), y(y) {} Vector2 operator+(const Vector2& v) const { return Vector2(x + v.x, y + v.y); } void operator+=(const Vector2& v) { x += v.x; y += v.y; } void wrapAround() { if (x < 0) x += WIDTH; if (x >= WIDTH) x -= WIDTH; if (y < 0) y += HEIGHT; if (y >= HEIGHT) y -= HEIGHT; } }; // Particle class class Particle { public: Vector2 position; Particle(float x, float y) : position(x, y) {} void move() { int direction = rand() % 4; // Random direction: 0 = up, 1 = down, 2 = left, 3 = right switch (direction) { case 0: position.y -= STEP_SIZE; break; case 1: position.y += STEP_SIZE; break; case 2: position.x -= STEP_SIZE; break; case 3: position.x += STEP_SIZE; break; } position.wrapAround(); } }; // Main simulation function void simulate(std::vector<Particle>& particles) { for (auto& particle : particles) { particle.move(); } } int main() { srand(static_cast<unsigned>(time(0))); std::vector<Particle> particles; for (int i = 0; i < PARTICLE_COUNT; ++i) { particles.emplace_back(rand() % WIDTH, rand() % HEIGHT); } while (true) { simulate(particles); // Here you would typically add rendering code to visualize the particles } return 0; } |
4o mini