// multi agent with ring connection // 041009 // ah int agent_number = 10; Agent[] agents; float theta_unit; int r = 100; int x0, y0; void setup() { // initialize screen size (500, 500); background(255); // set global various theta_unit = TWO_PI / agent_number; x0 = 250; y0 = 250; // create agents agents = new Agent[agent_number]; for (int i = 0; i < agent_number; i++) { // agents[i] = new Agent(theta_unit * i, int(random(10, 200))); agents[i] = new Agent(random(1) * TWO_PI, int(random(10, 200))); } for (int i = 0; i < agent_number; i++) { agents[i].setPreviousAgent(agents[previous(i)]); agents[i].setNextAgent(agents[next(i)]); } // set initial status agents[0].setStatus(true); agents[3].setStatus(true); } // main loop void loop() { background(255); for (int i = 0; i < agent_number; i++) { agents[i].move(); agents[i].drawBox(); agents[i].drawConnection(); agents[i].communicateWithPrevious(); } } // global function // get next number int next(int current) { int num; if (current == (agent_number - 1)) { num = 0; } else { num = current + 1; } return num; } // get previous number int previous(int current) { int num; if (current == 0) { num = agent_number - 1; } else { num = current - 1; } return num; } // class definition class Agent { float theta; Agent previous_agent, next_agent; boolean status; int r; Agent(float theta, int r) { this.theta = theta; this.r = r; status = false; } void setPreviousAgent(Agent a) { previous_agent = a; } void setNextAgent(Agent a) { next_agent = a; } void setStatus(boolean s) { status = s; } boolean getStatus() { return status; } float getTheta() { return theta; } int getR() { return r; } void drawBox() { stroke(180, 150, 0); if (status == true) { fill(200, 0, 0); } else { fill(200, 200, 0); } rect (x0 + r * sin(theta), y0 + r * cos(theta) * -1, 10, 10); } // tell left agent my status void communicateWithPrevious() { if (getStatus()) { previous_agent.setStatus(true); setStatus(false); // println(theta); } } void drawConnection() { stroke(200); line( x0 + r * sin(theta) + 5, y0 + r * cos(theta) * -1 + 5, x0 + previous_agent.getR() * sin(previous_agent.getTheta()) + 5, y0 + previous_agent.getR() * cos(previous_agent.getTheta()) * -1 + 5); } void move() { r += int(random(-1, 1)); theta += random(-.01, .01); } }