//=========================================================================== // FLOCKING - 1000 CANVAS RENDERED AGENTS // Version 11/3D - Multiple Timers // 25 November 2010 // // EVERYTHING RUNS MORE SLOWLY IN WINDOWS 7 // // This application automatically expands to the size of the display: // Form1->AutoSize = NOT false; // Form1->BorderStyle = bsNone; // Form1 and PaintBox1 sizes are set to MonitorHeight and MonitorWidth. // // Since the standard Windows controls do not appear: // Event handlers must be created to execute Form1->Close(). // A PopUp Menu is used to show and hide the controls. // // To make the application run with continuously varying parameters: // Nine timers control the positions of ten TrackBars. // The suspect population timer has been disabled.* // // *Access Violations occur unpredictably //=========================================================================== // optimize by not computing z when not required // need option to make agents smaller as a function of depth #include #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; #define POP 1000 // maximum population // still violation if 800 // ====================================================== VARIABLES & CLASSES int depth = 100; bool stop = true; bool move = false; bool anAgentHasBeenChosen = false; bool fixCursorXY = false; bool timers = false; bool anaglyph = false; // for throwing an agent double downX, downY; double upX, upY; double downUpX, downUpY; double timeUp, timeDown; double timeDownUp; double weightedAveDirection; double tangent; double sumXdistancesFromMe, sumYdistancesFromMe, sumZdistancesFromMe; double weightedAveSumXdistances, weightedAveSumYdistances, weightedAveSumZdistances; double xDistanceFromMe, yDistanceFromMe, zDistanceFromMe; double ave_abs_dX, ave_abs_dY; double weightedAveVelocity, sum_dH, my_H; double noiseX, noiseY; int variance; double directionVariance; int iterations; int delay = 0; int i; int dist, chosen; int pop = 500; // running population <= POP int lastPop = 100; int rule = 0; int radius = 20; int neighbors; int independence = 20; int cursorX, cursorY, cursorZ; int monitorWidth = 900; int monitorHeight = 900; TColor back = clBlack; TColor fore = clWhite; TColor userColor = clWhite; // user chosen color TColor dynaColor = clWhite; // dynamically chosen color TColor defaultColor = clWhite; //--------------------------------------------------- Color Ramp Version 2008 TColor colorRamp(int part, int whole) { if (whole == 0) whole++; // prevent divide by zero int pixelDistanceAlongEdges = (part * 1792) / whole; int red, green, blue; // Which edge of the color cube are we on? if (pixelDistanceAlongEdges < 256) { // from BLACK to BLUE red = 0; green = 0; blue = pixelDistanceAlongEdges; } else if (pixelDistanceAlongEdges < 512) { // from BLUE to CYAN red = 0; green = pixelDistanceAlongEdges - 256; blue = 255; } else if (pixelDistanceAlongEdges < 768) { // from CYAN to GREEN red = 0; green = 255; blue = 255 - (pixelDistanceAlongEdges - 512); } else if (pixelDistanceAlongEdges < 1024) { // from GREEN to YELLOW red = (pixelDistanceAlongEdges - 768); green = 255; blue = 0; } else if (pixelDistanceAlongEdges < 1280) { // from YELLOW to RED red = 255; green= 255-(pixelDistanceAlongEdges - 1024); blue = 0; } else if (pixelDistanceAlongEdges < 1536) { // from RED to MAGENTA red = 255; green= 0; blue = pixelDistanceAlongEdges - 1280; } else { // from MAGENTA to WHITE red = 255; green = pixelDistanceAlongEdges - 1537; blue = 255; } return static_cast(RGB(red, green, blue)); } // this is a description of an abstract class called "agents" class agentClass { public: double x, y, z ; double dx, dy, dz; double dxx, dyy, dzz; TPoint twoDPoints[3]; TPoint threeDPoints[3]; TColor color; float height, width; int seeking; void erase (void) { // size is dependent upon speed // twoDPoints[0].x = x + dx * 10; // twoDPoints[0].y = y + dy * 10; // twoDPoints[1].x = x - dx * 10 + 4 * dy; // twoDPoints[1].y = y - dy * 10 - 4 * dx; // twoDPoints[2].x = x - dx * 10 - 4 * dy; // twoDPoints[2].y = y - dy * 10 + 4 * dx; if (anaglyph) { // 3d Form1->PaintBox1->Canvas->Pen->Color = back; Form1->PaintBox1->Canvas->Brush->Color = back; Form1->PaintBox1->Canvas->Brush->Style = bsSolid; //bsClear; // Form1->PaintBox1->Canvas->Brush->Color = clBlack; // Form1->PaintBox1->Canvas->Brush->Style = bsCross; Form1->PaintBox1->Canvas->Polygon(twoDPoints, 2); Form1->PaintBox1->Canvas->Polygon(threeDPoints, 2); } else { // 2d Form1->PaintBox1->Canvas->Pen->Color = back; Form1->PaintBox1->Canvas->Brush->Color = back; Form1->PaintBox1->Canvas->Brush->Style = bsSolid; ////////////////////////////////////// Form1->PaintBox1->Canvas->Polygon(twoDPoints, 2); } } void draw (void) { // size is dependent upon speed twoDPoints[0].x = x + dx * 10; twoDPoints[0].y = y + dy * 10; twoDPoints[1].x = x - dx * 10 + 4 * dy; twoDPoints[1].y = y - dy * 10 - 4 * dx; twoDPoints[2].x = x - dx * 10 - 4 * dy; twoDPoints[2].y = y - dy * 10 + 4 * dx; if (!anaglyph) { // 2d if (Form1->RadioGroupColor->ItemIndex == 0) { // your color dynaColor = color; } if (Form1->RadioGroupColor->ItemIndex == 1) { // by speed dynaColor = colorRamp(velocity() * 300, 1000); } if (Form1->RadioGroupColor->ItemIndex == 2) { // by location dynaColor = colorRamp(x + y, monitorWidth * 1.8); } if (Form1->RadioGroupColor->ItemIndex == 3) { // by neighbors dynaColor = colorRamp(neighbors + 5, 30); } Form1->PaintBox1->Canvas->Brush->Color = dynaColor; Form1->PaintBox1->Canvas->Polygon(twoDPoints, 2); } else { // 3d threeDPoints[0].x = x + dx * 10 + (monitorHeight / 2 - z)/ depth; threeDPoints[0].y = y + dy * 10; threeDPoints[1].x = x - dx * 10 + 4 * dy + (monitorHeight / 2 - z) / depth; threeDPoints[1].y = y - dy * 10 - 4 * dx; threeDPoints[2].x = x - dx * 10 - 4 * dy + (monitorHeight / 2 - z) / depth; threeDPoints[2].y = y - dy * 10 + 4 * dx; Form1->PaintBox1->Canvas->Pen->Color = clRed; Form1->PaintBox1->Canvas->Brush->Style = bsClear; // Form1->PaintBox1->Canvas->Brush->Color = clRed; // Form1->PaintBox1->Canvas->Brush->Style = bsCross; Form1->PaintBox1->Canvas->Polygon(twoDPoints, 2); Application->ProcessMessages(); Form1->PaintBox1->Canvas->Pen->Color = clAqua; Form1->PaintBox1->Canvas->Brush->Style = bsClear; // Form1->PaintBox1->Canvas->Brush->Color = clAqua; // Form1->PaintBox1->Canvas->Brush->Style = bsCross; Form1->PaintBox1->Canvas->Polygon(threeDPoints, 2); } if (Form1->RadioGroupTrail->ItemIndex == 2) { // trails? Form1->PaintBox1->Canvas->Pen->Color = dynaColor; Application->ProcessMessages(); } else Form1->PaintBox1->Canvas->Pen->Color = clBlack; } void shrink (void) { erase(); // needs code for Polygon draw(); } void grow (void) { erase(); // needs code for Polygon draw(); } void move (void) { x += dx; y += dy; if (anaglyph) z += dz; if (Form1->RadioGroupSpace->ItemIndex == 0) { // Bounce if ((x > monitorWidth) || (x < 0)) { // was 885, 155 dx = -dx; } if ((y > monitorHeight) || (y < 0)) { dy = -dy; } if (anaglyph && (z > monitorHeight || z < 0)) { dz = -dz; } } else { // Wrap if (x > monitorWidth) x = 0; // was 895, 5 if (x < 0) x = monitorWidth; if (y > monitorHeight) y = 0; if (y < 0) y = monitorHeight; if (anaglyph && (z > monitorHeight)) z = 0; if (anaglyph && (z < 0)) z = monitorHeight; } draw(); } double velocity (void) { // is a function of dy, dx double temp = sqrt(pow(dx, 2) + pow(dy, 2)); //if (anaglyph) temp = sqrt(pow(temp, 2) + pow(dz, 2)); return temp; } double direction (void) { if (dx == 0) dx = .00001; return atan2(dy, dx); } }; agentClass agent[POP]; // creates an array "agent" of type "agentClass" // ================================================================ FUNCTIONS int mv (int i) { if (i < 0) i = pop; if (i < pop) i = 0; return i; } void reset (void) { //Form1->PaintBox1->Refresh(); Form1->PaintBox1->Canvas->Refresh(); // get depth for 3d if (anaglyph) depth = Form1->TrackBarDepth->Position; iterations = 0; for (int i = 0; i < pop; i++) { agent[i].erase(); agent[i].height = 10; agent[i].width = 10; agent[i].seeking = random(pop); //agent[i].color = defaultColor; //agent[i].direction = 0; //double(i) / 3; computed from dy, dx agent[i].x = random(monitorWidth) - 1; //random(890) + 5; // in a 900 x 900 world agent[i].y = random(monitorHeight) - 1; //random(890) + 5; // in a 900 x 900 world agent[i].z = random(monitorHeight) - 1; //random(890) + 5; // in a 900 x 900 world agent[i].dy = double(random(100) - 50) / 25.00; // -2 to +2 agent[i].dx = double(random(100) - 50) / 25.00; // -2 to +2 agent[i].dz = double(random(100) - 50) / 25.00; // -2 to +2 agent[i].draw(); if (rule == 5) { if (i % 4 == 0) agent[i].color = clAqua; if (i % 4 == 1) agent[i].color = clFuchsia; if (i % 4 == 2) agent[i].color = clYellow; if (i % 4 == 3) agent[i].color = clWhite; } else agent[i].color = clWhite; } } void initialize (void) { for (int i = 0; i < POP; i++) { agent[i].erase(); agent[i].height = 10; agent[i].width = 10; agent[i].color = defaultColor; //agent[i].direction = 0; // double(i) / 3; computed from dy, dx agent[i].x = random(monitorWidth) - 1; agent[i].y = random(monitorHeight) - 1; agent[i].z = random(monitorHeight) - 1; agent[i].dy = double(random(100) - 50) / 25.00; // -2 to +2 agent[i].dx = double(random(100) - 50) / 25.00; // -2 to +2 agent[i].dz = double(random(100) - 50) / 25.00; // -2 to +2 agent[i].draw(); } Form1->TrackBarSpeed->Position = Form1->TrackBarSpeed->Max; } int nneighbor (int me) { // suspect int nn; int minDist = 10000; for (int i = 0; i < pop; i++) { if (i == me) continue; // don't count yourself dist = sqrt( // true Pythagorean distance pow((agent[i].x - agent[me].x), 2) + pow((agent[i].y - agent[me].y), 2) ); if (anaglyph) { dist = sqrt( pow((agent[i].z - agent[me].z), 2) + pow(dist, 2) ); } // dist = abs(agent[i].x - agent[me].x); // square shortcut // if (dist > abs(agent[i].y - agent[me].y)) { // dist = abs(agent[i].y - agent[me].y);} // if (anaglyph && (dist > abs(agent[i].z - agent[me].z))) { // dist = abs(agent[i].z - agent[me].z);} if (dist < minDist) { minDist = dist; nn = i;} } return nn; } void averageInRadius (int me) { // square or cubic distance neighbors = 0; sumXdistancesFromMe = 0; sumYdistancesFromMe = 0; sumZdistancesFromMe = 0; sum_dH = 0; xDistanceFromMe = 0; yDistanceFromMe = 0; zDistanceFromMe = 0; for (int i = 0; i < pop; i++) { // look at everyone else if (i == me) continue; // but skip me xDistanceFromMe = abs(agent[i].x - agent[me].x); yDistanceFromMe = abs(agent[i].y - agent[me].y); zDistanceFromMe = abs(agent[i].z - agent[me].z); if ((xDistanceFromMe < radius) && (yDistanceFromMe < radius) && (zDistanceFromMe < radius)) { neighbors++; sumXdistancesFromMe += agent[i].dx; sumYdistancesFromMe += agent[i].dy; sumZdistancesFromMe += agent[i].dz; } } } void step (void) { for (int i = 0; i < pop; i++) { // draw trails or not if (Form1->RadioGroupTrail->ItemIndex == 0) { agent[i].erase(); } // adopt nearest neighbors direction and speed if (rule == 1) { agent[i].dx = agent[nneighbor(i)].dx; agent[i].dy = agent[nneighbor(i)].dy; } // adopt weighted average direction and speed of those // within a square radius // always a cube!!! if (rule == 2) { averageInRadius(i); // look around // add all x & y components weightedAveSumXdistances = (independence * agent[i].dx + sumXdistancesFromMe) / (neighbors + independence); weightedAveSumYdistances = (independence * agent[i].dy + sumYdistancesFromMe) / (neighbors + independence); weightedAveSumZdistances = (independence * agent[i].dz + sumZdistancesFromMe) / (neighbors + independence); if (weightedAveSumXdistances == 0) weightedAveSumXdistances = .00001; if (weightedAveSumYdistances == 0) weightedAveSumYdistances = .00001; if (weightedAveSumZdistances == 0) weightedAveSumZdistances = .00001; // compute noise directionVariance = 0.001 * ((random(variance + 1)) - 0.5 * variance); weightedAveDirection = atan2(weightedAveSumYdistances, weightedAveSumXdistances) + directionVariance; my_H = agent[i].velocity(); weightedAveVelocity = (independence * my_H + sum_dH) / (neighbors + independence); agent[i].dx = my_H * cos(weightedAveDirection); agent[i].dy = my_H * sin(weightedAveDirection); //agent[i].dz = my_H * sin(weightedAveDirection); } // flock to cursor (flee from cursor is less interesting) if (rule == 3) { sumXdistancesFromMe = cursorX - agent[i].x; sumYdistancesFromMe = cursorY - agent[i].y; sumZdistancesFromMe = cursorZ - agent[i].z; weightedAveSumXdistances = (independence * agent[i].dx + sumXdistancesFromMe) / (independence); weightedAveSumYdistances = (independence * agent[i].dy + sumYdistancesFromMe) / (independence); weightedAveSumZdistances = (independence * agent[i].dz + sumZdistancesFromMe) / (independence); if (weightedAveSumXdistances == 0) weightedAveSumXdistances = .00001; if (weightedAveSumYdistances == 0) weightedAveSumYdistances = .00001; if (weightedAveSumZdistances == 0) weightedAveSumZdistances = .00001; // compute noise directionVariance = 0.001 * ((random(variance + 1)) - 0.5 * variance); weightedAveDirection = atan2(weightedAveSumYdistances, weightedAveSumXdistances) + directionVariance; my_H = agent[i].velocity(); weightedAveVelocity = (independence * my_H + sum_dH) / (neighbors + independence); agent[i].dx = my_H * cos(weightedAveDirection); agent[i].dy = my_H * sin(weightedAveDirection); } // flee from cursor if (rule == 4) { sumXdistancesFromMe = agent[i].x - cursorX; sumYdistancesFromMe = agent[i].y - cursorY; weightedAveSumXdistances = (independence * agent[i].dx + sumXdistancesFromMe) / (independence); weightedAveSumYdistances = (independence * agent[i].dy + sumYdistancesFromMe) / (independence); if (weightedAveSumXdistances == 0) weightedAveSumXdistances = .00001; if (weightedAveSumYdistances == 0) weightedAveSumYdistances = .00001; // compute noise directionVariance = 0.001 * ((random(variance + 1)) - 0.5 * variance); weightedAveDirection = atan2(weightedAveSumYdistances, weightedAveSumXdistances) + directionVariance; my_H = agent[i].velocity(); weightedAveVelocity = (independence * my_H + sum_dH) / (neighbors + independence); agent[i].dx = my_H * cos(weightedAveDirection); agent[i].dy = my_H * sin(weightedAveDirection); } // assimilate if (rule == 5) { // agent[i].dx = agent[nneighbor(i)].dx; // agent[i].dy = agent[nneighbor(i)].dy; if (agent[i].color == clAqua) agent[nneighbor(i)].color = clAqua; if (agent[i].color == clFuchsia) agent[nneighbor(i)].color = clFuchsia; if (agent[i].color == clYellow) agent[nneighbor(i)].color = clYellow; if (agent[i].color == clWhite) agent[nneighbor(i)].color = clWhite; } // seek random other if (rule == 6) { sumXdistancesFromMe = agent[agent[i].seeking].x - agent[i].x; sumYdistancesFromMe = agent[agent[i].seeking].y - agent[i].y; sumZdistancesFromMe = agent[agent[i].seeking].z - agent[i].z; weightedAveSumXdistances = (independence * agent[i].dx + sumXdistancesFromMe) / (independence); weightedAveSumYdistances = (independence * agent[i].dy + sumYdistancesFromMe) / (independence); weightedAveSumZdistances = (independence * agent[i].dz + sumZdistancesFromMe) / (independence); if (weightedAveSumXdistances == 0) weightedAveSumXdistances = .00001; if (weightedAveSumYdistances == 0) weightedAveSumYdistances = .00001; if (weightedAveSumZdistances == 0) weightedAveSumZdistances = .00001; // compute noise directionVariance = 0.001 * ((random(variance + 1)) - 0.5 * variance); weightedAveDirection = atan2(weightedAveSumYdistances, weightedAveSumXdistances) + directionVariance; my_H = agent[i].velocity(); weightedAveVelocity = (independence * my_H + sum_dH) / (neighbors + independence); agent[i].dx = my_H * cos(weightedAveDirection); agent[i].dy = my_H * sin(weightedAveDirection); } // follow in line, seek in order if (rule == 7) { sumXdistancesFromMe = agent[mv(i+1)].x - agent[i].x; sumYdistancesFromMe = agent[mv(i+1)].y - agent[i].y; sumZdistancesFromMe = agent[mv(i+1)].z - agent[i].z; weightedAveSumXdistances = (independence * agent[i].dx + sumXdistancesFromMe) / (independence); weightedAveSumYdistances = (independence * agent[i].dy + sumYdistancesFromMe) / (independence); weightedAveSumZdistances = (independence * agent[i].dz + sumZdistancesFromMe) / (independence); if (weightedAveSumXdistances == 0) weightedAveSumXdistances = .00001; if (weightedAveSumYdistances == 0) weightedAveSumYdistances = .00001; if (weightedAveSumZdistances == 0) weightedAveSumZdistances = .00001; // compute noise directionVariance = 0.001 * ((random(variance + 1)) - 0.5 * variance); weightedAveDirection = atan2(weightedAveSumYdistances, weightedAveSumXdistances) + directionVariance; my_H = agent[i].velocity(); weightedAveVelocity = (independence * my_H + sum_dH) / (neighbors + independence); agent[i].dx = my_H * cos(weightedAveDirection); agent[i].dy = my_H * sin(weightedAveDirection); } agent[i].move(); } Sleep(delay); } void refresh (void) { Form1->PaintBox1->Refresh(); for (int i = 0; i < pop; i++) { agent[i].draw(); } } void run (void) { stop = false; Form1->EditStart->Text = Time(); while (stop == false) { Application->ProcessMessages(); // get depth for 3d if (anaglyph) depth = Form1->TrackBarDepth->Position; iterations++; step(); } } // ========================================================================== // =========================================================== EVENT HANDLERS // ========================================================================== //---------------------------------------------------------- FORM CONSTRUCTOR __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { DoubleBuffered = true; // MakeFullyVisible(); // See also Screen->Height, Width // See also Monitor->Height, Width monitorHeight = Monitor->Height; Form1->Height = monitorHeight; Form1->PaintBox1->Height = monitorHeight; monitorWidth = Monitor->Width; Form1->Width = monitorWidth; Form1->PaintBox1->Width = monitorWidth; randomize(); initialize(); Form1->PaintBox1->Canvas->Pen->Width = 1; } //----------------------------------------------------------------- SHOW FORM void __fastcall TForm1::FormShow(TObject *Sender) { refresh(); } //------------------------------------------------------------- ACTIVATE FORM void __fastcall TForm1::FormActivate(TObject *Sender) { refresh(); } //---------------------------------------------------------------- RUN BUTTON void __fastcall TForm1::ButtonRunClick(TObject *Sender) { run(); } //--------------------------------------------------------------- STEP BUTTON void __fastcall TForm1::ButtonStepClick(TObject *Sender) { stop = true; step(); } //--------------------------------------------------------------- STOP BUTTON void __fastcall TForm1::ButtonStopClick(TObject *Sender) { stop = true; } //-------------------------------------------------------------- RESET BUTTON void __fastcall TForm1::ButtonResetClick(TObject *Sender) { reset(); Form1->Refresh(); } //------------------------------------------------------------- CLOSE BUTTON void __fastcall TForm1::ButtonCloseClick(TObject *Sender) { Form1->Close(); } //------------------------------------------------------------- SHRINK BUTTON void __fastcall TForm1::ButtonShrinkClick(TObject *Sender) { for (int i = 0; i < pop; i++) { agent[i].shrink(); } } //--------------------------------------------------------------- GROW BUTTON void __fastcall TForm1::ButtonGrowClick(TObject *Sender) { for (int i = 0; i < pop; i++) { agent[i].grow(); } } //--------------------------------------------------------- TRAIL RADIO GROUP void __fastcall TForm1::RadioGroupTrailClick(TObject *Sender) { Form1->Refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if (Button == 0) { // set, unset, cursor X and Y fixCursorXY = !fixCursorXY; // clicks alternate between fixed & moving if (fixCursorXY == true) { cursorX = X; cursorY = Y; cursorZ = monitorHeight / 2; } } // Button 1 is reserved for the Popup Menu if (Button == 2) { // allow user to color an agent // a chosen agent is the nearest agent no farther away from its radius int shortestDistance = 1000; for (i = 0; i < pop; i++) { int distance = abs(X - agent[i].x) + abs(Y - agent[i].y); if (distance < shortestDistance) { shortestDistance = distance; chosen = i; } } if( shortestDistance < agent[chosen].width / 2) { agent[chosen].color = userColor; agent[chosen].draw(); } } } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { if (fixCursorXY == false) { cursorX = X; // useful for some rules cursorY = Y; } } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonSelectColorClick(TObject *Sender) { if(Form1->ColorDialog1->Execute()) { userColor = ColorDialog1->Color; Form1->ShapeColor->Brush->Color = userColor; } } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarSpeedChange(TObject *Sender) { delay = TrackBarSpeed->Max - TrackBarSpeed->Position; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarPopulationChange(TObject *Sender) { lastPop = pop; // remember previous population pop = TrackBarPopulation->Position; Form1->EditPop->Text = pop; if (pop < lastPop) // if new pop smaller, erase excess agents Form1->PaintBox1->Refresh(); reset(); } //--------------------------------------------------------------------------- void __fastcall TForm1::RadioGroupRulesClick(TObject *Sender) { rule = RadioGroupRules->ItemIndex; for (int i = 0; i < pop; i++) { if (rule == 5) { if (i % 4 == 0) agent[i].color = clAqua; if (i % 4 == 1) agent[i].color = clFuchsia; if (i % 4 == 2) agent[i].color = clYellow; if (i % 4 == 3) agent[i].color = clWhite; } else agent[i].color = clWhite; } } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarRadiusChange(TObject *Sender) { radius = TrackBarRadius->Position; Form1->EditRadius->Text = radius; Form1->ShapeRed->Height = radius; Form1->ShapeRed->Width = radius; Form1->ShapeRed->Left = (850 + (monitorHeight / 2 + radius) / depth); Form1->ShapeCyan->Left = (850 + (monitorHeight / 2 - radius) / depth); //(monitorHeight / 2 - z) / depth Form1->ShapeCyan->Height = radius; Form1->ShapeCyan->Width = radius; step(); } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarIndependenceChange(TObject *Sender) { independence = TrackBarIndependence->Position; Form1->EditStubbornness->Text = independence; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarVarianceChange(TObject *Sender) { variance = TrackBarVariance->Position; Form1->EditVariance->Text = variance; } //---------------------------------------------- ON / OFF RADIO GROUP TIMERS void __fastcall TForm1::RadioGroupTimersClick(TObject *Sender) { timers = !timers; cursorX = random(monitorWidth - 10); cursorY = random(monitorHeight - 10); if (timers == true) { Form1->TimerColor->Enabled = true; Form1->TimerRules->Enabled = true; Form1->TimerRadius->Enabled = true; Form1->TimerIndependence->Enabled = true; Form1->TimerVariance->Enabled = true; Form1->TimerReset->Enabled = true; Form1->TimerTrails->Enabled = true; Form1->TimerCursor->Enabled = true; Form1->TimerPopulation->Enabled = true; Form1->TimerOneSecond->Enabled = true; } else { Form1->TimerColor->Enabled = false; Form1->TimerRules->Enabled = false; Form1->TimerRadius->Enabled = false; Form1->TimerIndependence->Enabled = false; Form1->TimerVariance->Enabled = false; Form1->TimerReset->Enabled = false; Form1->TimerTrails->Enabled = false; Form1->TimerCursor->Enabled = false; Form1->TimerPopulation->Enabled = false; Form1->TimerOneSecond->Enabled = false; } } //--------------------------------------------------------------- COLOR TIMER void __fastcall TForm1::TimerColorTimer(TObject *Sender) { Form1->RadioGroupColor->ItemIndex = random(3) + 1; } //--------------------------------------------------------------- RULES TIMER void __fastcall TForm1::TimerRulesTimer(TObject *Sender) { Form1->RadioGroupRules->ItemIndex = random(3) + 1; } //-------------------------------------------------------------- RADIUS TIMER void __fastcall TForm1::TimerRadiusTimer(TObject *Sender) { Form1->TrackBarRadius->Position = random(100); } //-------------------------------------------------------- INDEPENDENCE TIMER void __fastcall TForm1::TimerIndependenceTimer(TObject *Sender) { Form1->TrackBarIndependence->Position = random(2000) + 1; } //------------------------------------------------------------ VARIANCE TIMER void __fastcall TForm1::TimerVarianceTimer(TObject *Sender) { Form1->TrackBarVariance->Position = random(200); } //--------------------------------------------------------------- RESET TIMER void __fastcall TForm1::TimerResetTimer(TObject *Sender) { reset(); } //------------------------------------------------------------- TRAILS TIMER void __fastcall TForm1::TimerTrailsTimer(TObject *Sender) { Form1->RadioGroupTrail->ItemIndex = random(3); } //------------------------------------------------------------- CURSOR TIMER void __fastcall TForm1::TimerCursorTimer(TObject *Sender) { cursorX = random(monitorWidth - 20) + 10; cursorY = random(monitorHeight - 20) + 10; } //--------------------------------------------------------- POPULATION TIMER void __fastcall TForm1::TimerPopulationTimer(TObject *Sender) { Form1->TrackBarPopulation->Position = 100 + random(800); // Access Violation if pop < 100 } //--------------------------------------------------------- ONE SECOND TIMER void __fastcall TForm1::TimerOneSecondTimer(TObject *Sender) { Form1->EditEnd->Text = Time(); } //--------------------------------------------------------------------------- //----------------------------------------------------------- EXIT POPUP MENU void __fastcall TForm1::Exit1Click(TObject *Sender) { run(); } //----------------------------------------------------------- EXIT POPUP MENU void __fastcall TForm1::Run1Click(TObject *Sender) { stop = true; step(); } //----------------------------------------------------------- EXIT POPUP MENU void __fastcall TForm1::Step1Click(TObject *Sender) { stop = true; } //----------------------------------------------------------- EXIT POPUP MENU void __fastcall TForm1::Exit2Click(TObject *Sender) { Form1->Close(); } //----------------------------------------------------------- EXIT POPUP MENU void __fastcall TForm1::Visible1Click(TObject *Sender) { Form1->GroupBoxControls->Visible = true; } //----------------------------------------------------------- EXIT POPUP MENU void __fastcall TForm1::Hidden1Click(TObject *Sender) { Form1->GroupBoxControls->Visible = false; } //--------------------------------------------------------------------------- void __fastcall TForm1::RadioGroupColorClick(TObject *Sender) { for (int i = 0; i < pop; i++) { agent[i].erase(); } if (RadioGroupColor->ItemIndex == 4) anaglyph = true; else anaglyph = false; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarDepthChange(TObject *Sender) { depth = TrackBarDepth->Position; Form1->ShapeRed->Height = radius; Form1->ShapeRed->Width = radius; Form1->ShapeRed->Left = (850 + (monitorHeight / 2 + radius) / depth); Form1->ShapeCyan->Left = (850 + (monitorHeight / 2 - radius) / depth); //(monitorHeight / 2 - z) / depth Form1->ShapeCyan->Height = radius; Form1->ShapeCyan->Width = radius; step(); } //--------------------------------------------------------------------------- void __fastcall TForm1::RadioGroupPenWidthClick(TObject *Sender) { if (RadioGroupPenWidth->ItemIndex == 0) Form1->PaintBox1->Canvas->Pen->Width = 1; if (RadioGroupPenWidth->ItemIndex == 1) Form1->PaintBox1->Canvas->Pen->Width = 2; if (RadioGroupPenWidth->ItemIndex == 2) Form1->PaintBox1->Canvas->Pen->Width = 3; } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonEqualizeSpeedClick(TObject *Sender) { for (int i = 0; i < POP; i++) { agent[i].dy = double(random(10) - 5) / 2.00; // -2 to +2 agent[i].dx = double(random(10) - 5) / 2.00; // -2 to +2 agent[i].dz = double(random(10) - 5) / 2.00; // -2 to +2 } } //--------------------------------------------------------------------------- void __fastcall TForm1::Reset1Click(TObject *Sender) { reset(); Form1->Refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ClearScreen1Click(TObject *Sender) { Form1->Refresh(); } //---------------------------------------------------------------------------