//=========================================================================== // FLOCKING POLYGONS - Version 22 // AutoRun with Multiple Timers // 5 November 2012 //=========================================================================== // // ========================================================================== // ====================================================== VARIABLES & CLASSES // ========================================================================== //------------------------ declaration of an abstract class called agentClass class agentClass { public: long double x, y, z ; long double dx, dy, dz; TColor color; int tag; int seeking; long double getDirection (void) long double getVelocity (void) void setDirection (long double dir) void setVelocity (long double vel) }; agentClass agent[POP]; // creates an array "agent" of type "agentClass" agentClass circles[4]; agentClass cursor; // ========================================================================== // ================================================================ FUNCTIONS // ========================================================================== // ========================================================= NEAREST NEIGHBOR int nneighbor (int me) { // calculates dist return nn; } // ======================================================= DISTANCE TO CURSOR double distanceToCursor(int i) { return dist; } // =================== SUM THE NEIGHBORS VECTORS WITHIN A RADIUS FROM SOMEONE void sumNeighborsVectorsFrom (int who) { // square, not Pythagorean, radius // calculates numberOfNeighbors // calculates sumXdistancesFromWho // calculates sumYdistancesFromWho // calculates sumZdistancesFromWho } // ======================================== ADJUST INFLUENCE BY INDEPENDENCE void adjustNeighborsVectorsByIndependence (void) { // calculates adjustedAveSumXdistances // calculates adjustedAveSumYdistances // calculates adjustedAveSumZdistances } // ================================================================ ADDS NOISE void addNoise (void) { // calculates agent[me].dx // calculates agent[me].dy } // ===================================================================== STEP void step (void) { for (me = 0; me < pop; me++) { //######################### beginning of rules ################## // ####################################################### RULE 1 if (rule == 1) { // adopt nearest neighbors direction and speed agent[me].dx = agent[nneighbor(me)].dx; agent[me].dy = agent[nneighbor(me)].dy; } // ####################################################### RULE 2 // adopt weighted average direction and speed of those // within a square radius if (rule == 2) { // calculates numberOfNeighbors and... sumNeighborsVectorsFrom(me); // also calculates numberOfNeighbors // adjusts vector sums by independence adjustNeighborsVectorsByIndependence(); // adds noise addNoise(); } // ####################################################### RULE 3 if (rule == 3) { // flock to cursor sumXdistancesFromWho = cursorX - agent[me].x; sumYdistancesFromWho = cursorY - agent[me].y; sumZdistancesFromWho = cursorZ - agent[me].z; numberOfNeighbors = 1; // adjusts vector sums by independence adjustNeighborsVectorsByIndependence(); // adds noise addNoise(); } // ####################################################### RULE 4 if (rule == 4) { // flee from cursor sumXdistancesFromWho = agent[me].x - cursorX; sumYdistancesFromWho = agent[me].y - cursorY; sumYdistancesFromWho = agent[me].z - cursorZ; numberOfNeighbors = 1; // adjusts vector sums by independence adjustNeighborsVectorsByIndependence(); // adds noise addNoise(); } // ####################################################### RULE 5 if (rule == 5) { // assimilate to nearest neighbor's color agent[me].color = agent[nneighbor(me)].color; } // ####################################################### RULE 6 if (rule == 6) { // seek assigned random individual sumXdistancesFromWho = agent[agent[me].seeking].x - agent[me].x; sumYdistancesFromWho = agent[agent[me].seeking].y - agent[me].y; sumZdistancesFromWho = agent[agent[me].seeking].z - agent[me].z; numberOfNeighbors = 1; // adjusts vector sums by independence adjustNeighborsVectorsByIndependence(); // adds noise addNoise(); } // ####################################################### RULE 7 if (rule == 7) { // follow in line, seek in order sumXdistancesFromWho = agent[mv(me+1)].x - agent[me].x; sumYdistancesFromWho = agent[mv(me+1)].y - agent[me].y; sumZdistancesFromWho = agent[mv(me+1)].z - agent[me].z; numberOfNeighbors = 1; // adjusts vector sums by independence adjustNeighborsVectorsByIndependence(); // adds noise addNoise(); } // ####################################################### RULE 8 if(rule == 8) { // Seek nearest neighbor sumXdistancesFromWho = agent[nneighbor(me)].x - agent[me].x; sumYdistancesFromWho = agent[nneighbor(me)].y - agent[me].y; sumZdistancesFromWho = agent[nneighbor(me)].z - agent[me].z; numberOfNeighbors = 1; // adjusts vector sums by independence adjustNeighborsVectorsByIndependence(); // adds noise addNoise(); } // ####################################################### RULE 9 if (rule == 9) { //flee nearest neighbor nneighbor(me); // calculates nn sumXdistancesFromWho = agent[me].x - agent[nn].x; sumYdistancesFromWho = agent[me].y - agent[nn].y; numberOfNeighbors = 1; // adjusts vector sums by independence adjustNeighborsVectorsByIndependence(); // adds noise addNoise(); } // ###################################################### RULE 10 if (rule == 10) { // Odd (.y and .x switched from seek cursor) sumXdistancesFromWho = cursorX - agent[me].y; sumYdistancesFromWho = cursorY - agent[me].x; sumZdistancesFromWho = cursorZ - agent[me].z; numberOfNeighbors = 1; // adjusts vector sums by independence adjustNeighborsVectorsByIndependence(); // adds noise addNoise(); } // ###################################################### RULE 11 if (rule == 11) { // Sarah Van Name's Four Corners if (agent[me].x < 840 && agent[me].y < 525) { sumXdistancesFromWho = 420 - agent[me].x; sumYdistancesFromWho = 262.5 - agent[me].y; sumZdistancesFromWho = 100 - agent[me].z; } if (agent[me].x < 840 && agent[me].y > 525) { sumXdistancesFromWho = 420 - agent[me].x; sumYdistancesFromWho = 787.5 - agent[me].y; sumZdistancesFromWho = 100 - agent[me].z; } if (agent[me].x > 840 && agent[me].y < 525) { sumXdistancesFromWho = 1260 - agent[me].x; sumYdistancesFromWho = 262.5 - agent[me].y; sumZdistancesFromWho = 100 - agent[me].z; } if (agent[me].x > 840 && agent[me].y > 525) { sumXdistancesFromWho = 1260 - agent[me].x; sumYdistancesFromWho = 787.5 - agent[me].y; sumZdistancesFromWho = 100 - agent[me].z; } dynaColor = colorRamp(125, 255); numberOfNeighbors = 1; // adjusts vector sums by independence adjustNeighborsVectorsByIndependence(); // adds noise addNoise(); } // ###################################################### RULE 12 if (rule == 12) { // Seek hierarchically sumXdistancesFromWho = agent[agent[me].seeking].x - agent[me].x; sumYdistancesFromWho = agent[agent[me].seeking].y - agent[me].y; sumZdistancesFromWho = agent[agent[me].seeking].z - agent[me].z; if (me == 0) { sumXdistancesFromWho = cursorX - agent[me].x; sumYdistancesFromWho = cursorY - agent[me].y; sumZdistancesFromWho = cursorZ - agent[me].z; } numberOfNeighbors = 1; // weight vectors by independence adjustNeighborsVectorsByIndependence(); // add noise addNoise(); } // ###################################################### RULE 13 if (rule == 13) { // Sarah Van Name's Lissajou Figure agent[mv(me + 1)].x = 840 + .45 * tan(me * 20) * 512; agent[mv(me + 1)].y = 525 + 0.3 * sin(me * 20) * 512; sumXdistancesFromWho = agent[mv(me + 1)].x - agent[me].x; sumYdistancesFromWho = agent[mv(me + 1)].y - agent[me].y; sumZdistancesFromWho = agent[mv(me + 1)].z - agent[me].z; numberOfNeighbors = 1; // weight vectors by independence adjustNeighborsVectorsByIndependence(); // add noise addNoise(); } // ###################################################### RULE 14 if(rule == 14) { // Flee Larger, Seek Smaller if(agent[nneighbor(me)].getVelocity() > agent[me].getVelocity()) { sumXdistancesFromWho = -1 * (agent[nneighbor(me)].x - agent[me].x); sumYdistancesFromWho = -1 * (agent[nneighbor(me)].y - agent[me].y); sumZdistancesFromWho = -1 * (agent[nneighbor(me)].z - agent[me].z); } else { sumXdistancesFromWho = agent[nneighbor(me)].x - agent[me].x; sumYdistancesFromWho = agent[nneighbor(me)].y - agent[me].y; sumZdistancesFromWho = agent[nneighbor(me)].z - agent[me].z; } numberOfNeighbors = 1; // weight vectors by independence adjustNeighborsVectorsByIndependence(); // add noise addNoise(); } // ###################################################### RULE 15 if (rule == 15) { // circle: seek radius from cursor if (distanceToCursor(me) < radius) { // flee sumXdistancesFromWho = agent[me].x - cursorX; sumYdistancesFromWho = agent[me].y - cursorY; } else { // flock sumXdistancesFromWho = cursorX - agent[me].x; sumYdistancesFromWho = cursorY - agent[me].y; } numberOfNeighbors = 1; // weight vectors by independence adjustNeighborsVectorsByIndependence(); // add noise addNoise(); } //############################### end of rules ################## //############################### end of rules ################## agent[me].move(); } THAT'S ALL FOLKS...