//=========================================================================== // 1000 CANVAS RENDERED AGENTS // Version 5 - Higher Definition // 2 November 2010 // // This is the beginning of a new foundation. // Illustrating class construction, a great deal of code // has been moved to the class declaration and definition. // // Note: The Canvas->Rectangle method runs ABOUT 10 times faster // than the Canvas->Ellipse method and about 100 times faster // than moving TImages around the screen. // Use Canvas->Polyline or Canvas->Polygon to vary the visualization. // Trials runs 5-10 times faster than no trails. //=========================================================================== #include #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; #define POP 1000 // maximum population // ====================================================== VARIABLES & CLASSES bool stop = true; bool move = false; bool anAgentHasBeenChosen = false; // for throwing an agent double downX, downY; double upX, upY; double downUpX, downUpY; double timeUp, timeDown; double timeDownUp; double angle; double tangent; double sum_dX, sum_dY, ave_dX, ave_dY; double sum_abs_dX, sum_abs_dY; double ave_abs_dX, ave_abs_dY; double ave_dH, sum_dH, my_H; int delay = 0; TColor back = 0X79BBD7; TColor fore = 0X4080FF; int i; int dist, chosen; int pop = 100; // running population <= POP int lastPop = 100; int rule; int radius = 10; int num; int stubbornness = 25; TColor color = 0X4080FF; //--------------------------------------------------- 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 ; double direction; double dx, dy; double dyy; double dxx; TPoint points[6]; TColor color; float height, width; void erase (void) { points[0].x = x + dx * 10; points[0].y = y + dy * 10; points[1].x = x - dx * 10 + 4 * dy; points[1].y = y - dy * 10 - 4 * dx; points[2].x = x - dx * 10 - 4 * dy; points[2].y = y - dy * 10 + 4 * dx; Form1->PaintBox1->Canvas->Pen->Color = back; Form1->PaintBox1->Canvas->Brush->Color = back; Form1->PaintBox1->Canvas->Polygon(points, 2); } void draw (void) { points[0].x = x + dx * 10; points[0].y = y + dy * 10; points[1].x = x - dx * 10 + 4 * dy; points[1].y = y - dy * 10 - 4 * dx; points[2].x = x - dx * 10 - 4 * dy; points[2].y = y - dy * 10 + 4 * dx; Form1->PaintBox1->Canvas->Pen->Color = clBlack; if (Form1->RadioGroupColor->ItemIndex == 0) { // your color Form1->PaintBox1->Canvas->Brush->Color = color; } if (Form1->RadioGroupColor->ItemIndex == 1) { // by speed Form1->PaintBox1->Canvas->Brush->Color = colorRamp((abs(dx) + abs(dy) + 4) * 12.5, 100); } if (Form1->RadioGroupColor->ItemIndex == 2) { // by location Form1->PaintBox1->Canvas->Brush->Color = colorRamp(x + y, 1800); } Form1->PaintBox1->Canvas->Polygon(points, 2); } 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 (Form1->RadioGroupSpace->ItemIndex == 0) { // Bounce if (x > 885 || x < 15) { dx = -dx; } if (y > 885 || y < 15) { dy = -dy; } } else { // Wrap if (x > 895 ) x = 5; if (x < 5) x = 895; if (y > 895) y = 5; if (y < 5) y = 895; } draw(); } }; agentClass agent[POP]; // creates an array "agent" of type "agentClass" // ================================================================ FUNCTIONS void initialize (void) { for (int i = 0; i < POP; i++) { agent[i].erase(); agent[i].height = 10; agent[i].width = 10; agent[i].color = 0X4080FF; agent[i].direction = double(i) / 3; agent[i].x = random(890) + 5; // in a 900 x 900 world agent[i].y = 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].draw(); } Form1->TrackBarSpeed->Position = Form1->TrackBarSpeed->Max; } void reset (void) { Form1->PaintBox1->Refresh(); for (int i = 0; i < pop; i++) { agent[i].erase(); agent[i].height = 10; agent[i].width = 10; agent[i].color = 0X4080FF; agent[i].direction = double(i) / 3; agent[i].x = random(890) + 5; // in a 900 x 900 world agent[i].y = 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].draw(); } } int nneighbor (int me) { int is; int minDist = 1000; for (int i = 0; i < pop; i++) { if (i == me) continue; dist = (sqrt(pow((agent[i].x - agent[me].x), 2)) + sqrt(pow((agent[i].y - agent[me].y), 2))); if (dist < minDist) { minDist = dist; is = i; } } return is; } void averageInSquareRadius (int me) { num = 0; sum_dX = 0; sum_dY = 0; sum_dH = 0; sum_abs_dX = 0; sum_abs_dY = 0; for (int i = 0; i < pop; i++) { if (i == me) continue; sum_abs_dX = abs(agent[i].x - agent[me].x); sum_abs_dY = abs(agent[i].y - agent[me].y); if ((sum_abs_dX < radius) && (sum_abs_dY < radius)) { num++; //sum_dH += sqrt(pow(sum_abs_dX, 2) + pow(sum_abs_dY, 2)); sum_dX += agent[i].dx; sum_dY += agent[i].dy; //sum_abs_dX += abs(agent[i].dx); //sum_abs_dY += abs(agent[i].dy); } } } 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 if (rule == 2) { averageInSquareRadius(i); ave_dX = (stubbornness * agent[i].dx + sum_dX) / (num + stubbornness); ave_dY = (stubbornness * agent[i].dy + sum_dY) / (num + stubbornness); my_H = sqrt(pow(agent[i].dx, 2) + pow(agent[i].dy, 2)); ave_dH = (stubbornness * my_H + sum_dH) / (num + stubbornness); if (ave_dX == 0) ave_dX = .00001; if (ave_dY == 0) ave_dY = .00001; angle = atan2(ave_dY, ave_dX); agent[i].dx = my_H * cos(angle); //ave_dX * 1.001; agent[i].dy = my_H * sin(angle); //ave_dY * 1.001; } agent[i].move(); } Sleep(delay); } void refresh (void) { Form1->PaintBox1->Refresh(); for (int i = 0; i < pop; i++) { agent[i].draw(); } } // =========================================================== EVENT HANDLERS //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { DoubleBuffered = true; randomize(); initialize(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonShrinkClick(TObject *Sender) { for (int i = 0; i < pop; i++) { agent[i].shrink(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonGrowClick(TObject *Sender) { for (int i = 0; i < pop; i++) { agent[i].grow(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::RadioGroupTrailClick(TObject *Sender) { Form1->Refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonStepClick(TObject *Sender) { stop = true; step(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonRunClick(TObject *Sender) { stop = false; while (stop == false) { Application->ProcessMessages(); step(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonStopClick(TObject *Sender) { stop = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonResetClick(TObject *Sender) { reset(); Form1->Paint(); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { // a chosen agent is the nearest agent no farther away from its radius dist = 1000; for (i = 0; i < POP; i++) { if (abs(X - agent[i].x) + abs(Y - agent[i].y) < dist) { dist = abs(X - agent[i].x) + abs(Y - agent[i].y); chosen = i; } } if (dist < agent[chosen].width / 2) { anAgentHasBeenChosen = true; } else anAgentHasBeenChosen = false; // left click to throw if (Button == 0 && anAgentHasBeenChosen) { move = true; timeDown = Now(); downX = X; downY = Y; } // right click to color if (Button == 1 && anAgentHasBeenChosen) { move = false; agent[chosen].color = color; agent[chosen].draw(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { if (move == true && anAgentHasBeenChosen) { agent[chosen].erase(); agent[chosen].x = X; agent[chosen].y = Y; timeUp = Now(); agent[chosen].draw(); refresh(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if (move == true && anAgentHasBeenChosen) { timeUp = Now(); upX = X; upY = Y; downUpX = downX - upX; downUpY = downY - upY; timeDownUp = timeDown - timeUp; agent[chosen].dx = downUpX / timeDownUp / 5000000; agent[chosen].dy = downUpY / timeDownUp / 5000000; move = false; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormShow(TObject *Sender) { refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1Paint(TObject *Sender) { //refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonSelectColorClick(TObject *Sender) { if(Form1->ColorDialog1->Execute()) { color = ColorDialog1->Color; Form1->ShapeColor->Brush->Color = color; } } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarSpeedChange(TObject *Sender) { delay = TrackBarSpeed->Max - TrackBarSpeed->Position; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarPopulationChange(TObject *Sender) { lastPop = pop; pop = TrackBarPopulation->Position; Form1->EditPop->Text = pop; if (pop < lastPop) Form1->PaintBox1->Refresh(); reset(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormActivate(TObject *Sender) { refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::RadioGroupRulesClick(TObject *Sender) { rule = RadioGroupRules->ItemIndex; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarRadiusChange(TObject *Sender) { radius = TrackBarRadius->Position; Form1->EditRadius->Text = radius; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarStubbornnessChange(TObject *Sender) { stubbornness = TrackBarStubbornness->Position; Form1->EditStubbornness->Text = stubbornness; } //---------------------------------------------------------------------------