//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; // Set number of agents int number = 100; // Set probe int probe = 0; // Set up table of 100 agents and their positions: float agentx[100], agenty[100]; // Identify who they are attracted to: int attractee[100]; // Identify who they will avoid: int avoidee[100]; // Identify movement toward attractee: float attracteex, attracteey; // Identify movement toward avoidee: float avoideex, avoideey; // Level of attraction and avoidance: float attraction = .05; float avoidance = .01; // Run - Stop Status: bool stop = true; // Track Status: bool track = false; // Probe Status: bool showProbe = false; // Scale for plot: float s = 1; // Interval for Sleep: int interval = 50; //---------------------------------------------------------------------- STEP void step(void) { for (int i = 0; i < number; i++) { // Unplot old probe // Unplot old position Form1->PaintBox1->Canvas->Brush->Color = clWhite; Form1->PaintBox1->Canvas->Pen->Color = clWhite; if (track) Form1->PaintBox1->Canvas->Pen->Color = clSilver; Form1->PaintBox1->Canvas-> Ellipse(agentx[i]/s+250*(1-1/s)-1, agenty[i]/s+250*(1-1/s)-1, agentx[i]/s+250*(1-1/s)+1, agenty[i]/s+250*(1-1/s)+1); //Form1->PaintBox1->Canvas->Pixels[agentx[i]][agenty[i]] = clWhite; // Calculate move toward attractee attracteex = agentx[i]-agentx[attractee[i]]; attracteey = agenty[i]-agenty[attractee[i]]; // Calculate move away from avoidee avoideex = agentx[i]-agentx[avoidee[i]]; avoideey = agenty[i]-agenty[avoidee[i]]; // Calculate composite move agentx[i] = agentx[i] + (avoidance * avoideex) - (attraction * attracteex); agenty[i] = agenty[i] + (avoidance * avoideey) - (attraction * attracteey); // Plot new probe if (showProbe && probe == i) { Form1->PaintBox1->Canvas->Pen->Color = clLime; Form1->PaintBox1->Canvas-> MoveTo(agentx[i]/s+250*(1-1/s), agenty[i]/s+250*(1-1/s)); Form1->PaintBox1->Canvas->LineTo(agentx[attractee[i]]/s+250*(1-1/s), agenty[attractee[i]]/s+250*(1-1/s)); Form1->PaintBox1->Canvas->Pen->Color = clFuchsia; Form1->PaintBox1->Canvas->MoveTo(agentx[i]/s+250*(1-1/s), agenty[i]/s+250*(1-1/s)); Form1->PaintBox1->Canvas->LineTo(agentx[avoidee[i]]/s+250*(1-1/s), agenty[avoidee[i]]/s+250*(1-1/s)); } // Plot new position Form1->PaintBox1->Canvas->Brush->Color = clBlack; Form1->PaintBox1->Canvas->Pen->Color = clBlack; Form1->PaintBox1->Canvas-> Ellipse(agentx[i]/s+250*(1-1/s)-1, agenty[i]/s+250*(1-1/s)-1, agentx[i]/s+250*(1-1/s)+1, agenty[i]/s+250*(1-1/s)+1); //Form1->PaintBox1->Canvas->Pixels[agentx[i]][agenty[i]] = clBlack; } } //--------------------------------------------------------------------- RUN void run(void) { while (!stop) { step(); Sleep(interval); Application->ProcessMessages(); } } //----------------------------------------------------------------- RANDOMIZE void randomize(void) { for (int i = 0; i < number; i++) { // Randomize position // We divide by 100 so the calculation is a smaller scale than the plot agentx[i] = (float(random(50000)))/100; agenty[i] = (float(random(50000)))/100; // Randomize interacting partner attractee[i] = random(100); avoidee[i] = random(100); } Form1->PaintBox1->Refresh(); } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //-------------------------------------------------------------- BUTTON CLEAR void __fastcall TForm1::ButtonClearClick(TObject *Sender) { Form1->PaintBox1->Refresh(); } //---------------------------------------------------------- BUTTON RANDOMIZE void __fastcall TForm1::ButtonRandomizeClick(TObject *Sender) { randomize(); } //--------------------------------------------------------------- BUTTON PLOT void __fastcall TForm1::ButtonPlotClick(TObject *Sender) { for (int i = 0; i < number; i++) { Form1->PaintBox1->Canvas->Brush->Color = clBlack; Form1->PaintBox1->Canvas->Pen->Color = clBlack; Form1->PaintBox1->Canvas-> Ellipse(agentx[i]/s+250*(1-1/s)-1, agenty[i]/s+250*(1-1/s)-1, agentx[i]/s+250*(1-1/s)+1, agenty[i]/s+250*(1-1/s)+1); //Form1->PaintBox1->Canvas->Pixels[agentx[i]][agenty[i]] = clBlack; } } //--------------------------------------------------------------- BUTTON STEP void __fastcall TForm1::ButtonStepClick(TObject *Sender) { step(); } //---------------------------------------------------------------- BUTTON RUN void __fastcall TForm1::ButtonRunClick(TObject *Sender) { stop = !stop; if (stop) { ButtonRun->Caption = "Run"; PanelRunStop->Color = 0x005256FE; } if (!stop) { ButtonRun->Caption = "Stop"; PanelRunStop->Color = 0x0049CF57; } run(); } //------------------------------------------------------------ TRACKBAR SPEED void __fastcall TForm1::TrackBarSpeedChange(TObject *Sender) { interval = TrackBarSpeed->Position; } //------------------------------------------------------------- TRACKBAR ZOOM void __fastcall TForm1::TrackBarZoomOutChange(TObject *Sender) { s = pow(2, TrackBarZoomOut->Position); Form1->PaintBox1->Refresh(); } //-------------------------------------------------------------- BUTTON TRACK void __fastcall TForm1::ButtonTrackClick(TObject *Sender) { track = !track; if (track) ButtonTrack->Caption = "Hide Tracks"; if (!track) ButtonTrack->Caption = "Show Tracks"; } //------------------------------------------------------- TRACKBAR ATTRACTION void __fastcall TForm1::TrackBarAttractionChange(TObject *Sender) { attraction = float(TrackBarAttraction->Position) / 100; } //-------------------------------------------------------- TRACKBAR AVOIDANCE void __fastcall TForm1::TrackBarAvoidanceChange(TObject *Sender) { avoidance = float(TrackBarAvoidance->Position) / 100; } //-------------------------------------------------------------- BUTTON RESET void __fastcall TForm1::ButtonResetClick(TObject *Sender) { randomize(); attraction = .05; TrackBarAttraction->Position = 5; avoidance = .01; TrackBarAvoidance->Position = 1; track = false; ButtonTrack->Caption = "Show Tracks"; s = 1; TrackBarZoomOut->Position = 0; interval = 50; TrackBarSpeed->Position = 50; probe = false; ButtonProbe->Caption = "Show Probe"; Form1->PaintBox1->Refresh(); Application->ProcessMessages(); } //------------------------------------------------------------ TRACKBAR PROBE void __fastcall TForm1::TrackBarProbeChange(TObject *Sender) { probe = TrackBarProbe->Position -1; } //-------------------------------------------------------------- BUTTON PROBE void __fastcall TForm1::ButtonProbeClick(TObject *Sender) { showProbe = !showProbe; if (showProbe) ButtonProbe->Caption = "Hide Probe"; if (!showProbe) ButtonProbe->Caption = "Show Probe"; } //---------------------------------------------------------------------------