// Class that represents a bee. Has a position, velocity, and random color. class Bee { // Costructor. Assigns position, velocity is 0, color is random Bee(float x, float y) { _x = x; _y = y; _fx = 0; _fy = 0; _r = random(0,255); _g = random(0,255); _b = random(0,255); } // Draw the bee. void draw() { float hw = 10; float dx = _fx; float dy = _fy; float l = sqrt(dx*dx+dy*dy); if (l == 0) { l = 1; } dx = dx/l; dy = dy/l; float h = l * 500; if (h > 255) { h = 255; } float b = 100*constrain(10*l,0,2); fill(h, 255, b); beginShape(TRIANGLES); vertex(_x+hw*dx*1.2,_y+hw*dy*1.2); vertex(_x-hw*dy/3,_y+hw*dx/3); vertex(_x+hw*dy/3,_y-hw*dx/3); endShape(); } // Initialize the force to 0. Call once at the start of each timestep. void start_step() { _fx = 0; _fy = 0; } // Add a gravitational force vector. Call once for each planet after // calling start_step. void add_force(float x, float y) { float dx = (_x - x) / 1000; float dy = (_y - y) / 1000; float g = .001; float l = sqrt(dx*dx + dy*dy); float f = 0; if (l > 0) { dx = dx / l; dy = dy / l; f = g / (l*l); f = -constrain(f, 0, 2 ); } _fx = dx * f; _fy = dy * f; } float _x; float _y; float _fx; float _fy; float _r; float _g; float _b; }; Bee[] bees = new Bee[20*20]; int num_bees = 20*20; boolean first_frame = true; // true means we have to clear boolean always_clear = true; // true means we should clear at the start of every frame // Create a random set of bees void setup() { size(400,400); for (int r=0; r<20; ++r) { for (int c=0; c<20; ++c) { bees[c*20+r] = new Bee(400*r/20,400*c/20); } } noStroke(); colorMode(HSB); } // Called once per frame void draw() { if (first_frame || always_clear) { background(255,0,0); first_frame = false; } // Loop over the bees. Move them and draw them. for (int i=0; i