//=========================================================================== // 1000 CANVAS RENDERED AGENTS // Version 4 - Higher Definition // 23 March 2010 // // This is the beginning of a new foundation. // Illustrating class construction, a great deal of code // has been added 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 // ====================================================== VARIABLES & CLASSES bool stop = true; bool move = false; bool anAgentHasBeenChosen = false; int flySpeed; // for throwing an agent double downX, downY; double upX, upY; double downUpX, downUpY; double timeUp, timeDown; double timeDownUp; int delay = 0; TColor back = 0X79BBD7; TColor fore = 0X4080FF; int i; int dist, chosen; TColor color = 0X4080FF; // this is a description of an abstract class called "agents" class agents { public: double x, y ; double direction; double dx, dy; 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->Pixels[x][y] = back; // check for center Form1->PaintBox1->Canvas->Polygon(points, 2); // Form1->PaintBox1->Canvas->Rectangle // (x - width / 2, y - height / 2, x + width / 2, y + height / 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) { Form1->PaintBox1->Canvas->Brush->Color = (dx + dy) * 4194303; } if (Form1->RadioGroupColor->ItemIndex == 2) { Form1->PaintBox1->Canvas->Brush->Color = dx * dy * 4194303; } if (Form1->RadioGroupColor->ItemIndex == 3) { Form1->PaintBox1->Canvas->Brush->Color = (x + y) * 63200; } Form1->PaintBox1->Canvas->Polygon(points, 2); //Form1->PaintBox1->Canvas->Pixels[x][y] = clBlack; check for center // Form1->PaintBox1->Canvas->Rectangle // (x - width / 2, y - height / 2, x + width / 2, y + height / 2); } void shrink (void) { // only works with Canvas->Rectangle erase(); height *= .9; width *= .9; draw(); } void grow (void) { // only works with Canvas->Rectangle erase(); height *= 1.1; width *= 1.1; draw(); } void move (void) { if (Form1->RadioGroupTrail->ItemIndex == 0) { erase(); } x += dx; y += dy; // BASIC if (Form1->RadioGroupAdvanced->ItemIndex == 0) { if (Form1->RadioGroupSpace->ItemIndex == 0) { // Bounce if (x > 895 || x < 5) { dx = -dx + flySpeed; } if (y > 895 || y < 5) { dy = -dy + flySpeed; } } else { // Wrap if (x > 895 ) x = 5; if (x < 5) x = 895; if (y > 895) y = 5; if (y < 5) y = 895; } } // ADVANCED if (Form1->RadioGroupAdvanced->ItemIndex == 1) { // Adjust normal behavior by increasing flySpeed if (Form1->RadioGroupSpace->ItemIndex == 0) { // Bounce if (x > 895 || x < 5) { dx = -dx + flySpeed; } if (y > 895 || y < 5) { dy = -dy + flySpeed; } } else { // Wrap if (x > 895 ) x = 5; if (x < 5) x = 895; if (y > 895) y = 5; if (y < 5) y = 895; } } // if (Form1->RadioGroupSpace->ItemIndex == 0 && // Form1->RadioGroupSpecial->ItemIndex == 0) { // if (x > 895 || x < 5) { // dx = -dx + flySpeed; // } // if (y > 895 || y < 5) { // dy = -dy + flySpeed; // } // } //// // if (Form1->RadioGroupSpace->ItemIndex == 0 && // Form1->RadioGroupSpecial->ItemIndex == 1) { // if (x > 895 || x < 5) { // dx = -dx + flySpeed; // } // if (y > 895 || y < 5) { // dy = -dy + flySpeed; // } // } if (Form1->RadioGroupSpace->ItemIndex == 0 && Form1->RadioGroupSpecial->ItemIndex == 2) { if (x > 895 || x < 5) { dx = -dx; } if (y > 895 || y < 5) { dy = -dy; } } else{ if (x > 895 ) x = 5; if (x < 5) x = 895; if (y > 895) y = 5; if (y < 5) y = 895; } draw(); } }; agents agent[POP]; // we now create an array "agent" of type "agents" // ================================================================ 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->TrackBarDelay->Position = Form1->TrackBarDelay->Max; } void step (void) { for (int i = 0; i < POP; i++) { agent[i].move(); } Sleep(delay); } void refresh (void) { 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) { initialize(); 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::TrackBarDelayChange(TObject *Sender) { delay = TrackBarDelay->Max - TrackBarDelay->Position; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarFlySpeedChange(TObject *Sender) { flySpeed = Form1->TrackBarFlySpeed->Position; } //---------------------------------------------------------------------------