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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| #include <cmath> #include <vector> #include <iostream> #include <graphics.h>
struct Vector2D { float x, y;
Vector2D() = default; Vector2D(float _x, float _y) : x(_x), y(_y) {}
Vector2D operator+(const Vector2D& other) const { return Vector2D(x + other.x, y + other.y); }
Vector2D operator-(const Vector2D& other) const { return Vector2D(x - other.x, y - other.y); }
Vector2D operator*(float scalar) const { return Vector2D(x * scalar, y * scalar); }
float length() const { return std::sqrt(x * x + y * y); }
void normalize() { float len = length(); x /= len; y /= len; } };
struct Boid { COLORREF color; Vector2D position; Vector2D velocity;
Vector2D cohesion(const std::vector<Boid>& boids) { Vector2D center_of_mass(0, 0); int total_neighbors = 0;
for (const Boid& b : boids) { float distance = (b.position - position).length();
if (distance > 0 && distance < neighbour_distance) { center_of_mass = center_of_mass + b.position; total_neighbors++; } }
if (total_neighbors > 0) { center_of_mass = center_of_mass * (1.0f / total_neighbors); return (center_of_mass - position); }
return Vector2D(0, 0); }
Vector2D separation(const std::vector<Boid>& boids) { Vector2D separation(0, 0);
for (const Boid& b : boids) { float distance = (b.position - position).length();
if (distance > 0 && distance < separation_distance) { Vector2D diff = position - b.position; separation = separation + diff * (1.0f / distance); } }
return separation; }
Vector2D alignment(const std::vector<Boid>& boids) { Vector2D avg_velocity(0, 0); int total_neighbors = 0;
for (const Boid& b : boids) { float distance = (b.position - position).length();
if (distance > 0 && distance < neighbour_distance) { avg_velocity = avg_velocity + b.velocity; total_neighbors++; } }
if (total_neighbors > 0) { avg_velocity = avg_velocity * (1.0f / total_neighbors); return avg_velocity - velocity; }
return Vector2D(0, 0); }
void update(const std::vector<Boid>& boids) { Vector2D v1 = cohesion(boids); Vector2D v2 = separation(boids); Vector2D v3 = alignment(boids);
v1 = v1 * cohesion_weight; v2 = v2 * separation_weight; v3 = v3 * align_weight;
velocity = velocity + v1 + v2 + v3;
float speed = velocity.length(); if (speed > max_speed) velocity = velocity * (max_speed / speed);
position = position + velocity;
if (position.x < 0) position.x = 0; if (position.x > 1280) position.x = 1280; if (position.y < 0) position.y = 0; if (position.y > 720) position.y = 720; }
float neighbour_distance = 100.0f; float separation_distance = 50.0f; float cohesion_weight = 1.0f; float separation_weight = 1.0f; float align_weight = 1.0f; float max_speed = 5.0f; };
int main() { initgraph(1280, 720, EW_SHOWCONSOLE); BeginBatchDraw();
std::vector<Boid> boids(500); for (Boid& b : boids) { b.position.x = (float)(rand() % 1280); b.position.y = (float)(rand() % 720); b.color = RGB(rand() % 255, rand() % 255, rand() % 255); }
while (true) { for (Boid& b : boids) b.update(boids);
cleardevice(); for (Boid& b : boids) { setfillcolor(b.color); fillcircle((int)b.position.x, (int)b.position.y, 10); } FlushBatchDraw();
Sleep(25); }
return 0; }
|