//=========================================================================== // CLASS OVERLOADED 2000 WANDERING SQUARES // 26 July 2009 // // As an illustration of class construction, probably too much code has // been added to the class declaration and definition. // Note: The rectangle method runs 10 times as fast as the ellipse method. //=========================================================================== #include #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; #define POP 2000 // ====================================================== VARIABLES & CLASSES bool stop = true; TColor back = 0X79BBD7; TColor fore = 0X4080FF; class shapes { public: double x, y ; double direction; double dx, dy; float height, width; void erase (void) { Form1->Canvas->Pen->Color = back; Form1->Canvas->Brush->Color = back; Form1->Canvas->Rectangle(x, y, x + width, y + height); } void draw (void) { Form1->Canvas->Pen->Color = clBlack; Form1->Canvas->Brush->Color = fore; Form1->Canvas->Rectangle(x, y, x + width, y + height); } void shrink (void) { erase(); height *= .9; width *= .9; draw(); } void grow (void) { erase(); height *= 1.1; width *= 1.1; draw(); } void move (void) { if (Form1->RadioGroupTrail->ItemIndex == 0) { Form1->Canvas->Pen->Color = back; Form1->Canvas->Brush->Color = back; Form1->Canvas->Rectangle(x, y, x + width, y + height); } x += dx; y += dy; if (x > 480 || x < 0) { dx = -dx; } if (y > 480 || y < 0) { dy = -dy; } draw(); } } shape[POP]; // ================================================================ FUNCTIONS void initialize (void) { for (int i = 0; i < POP; i++) { shape[i].erase(); shape[i].height = 10; shape[i].width = 10; shape[i].direction = double(i) / 3; shape[i].x = random(480); shape[i].y = random(480); shape[i].dy = double(random(100) - 50) / 25.00; shape[i].dx = double(random(100) - 50) / 25.00; Form1->Canvas->Pen->Color = clBlack; Form1->Canvas->Brush->Color = fore; Form1->Canvas->Rectangle(shape[i].x, shape[i].y, shape[i].x + shape[i].width, shape[i].y + shape[i].height); } } void step (void) { for (int i = 0; i < POP; i++) { shape[i].move(); } } // =========================================================== 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++) { shape[i].shrink(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonGrowClick(TObject *Sender) { for (int i = 0; i < POP; i++) { shape[i].grow(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormPaint(TObject *Sender) { for (int i = 0; i < POP; i++) { Form1->Canvas->Rectangle(shape[i].x, shape[i].y, shape[i].x + shape[i].width, shape[i].y + shape[i].height); } } //--------------------------------------------------------------------------- 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(); } //---------------------------------------------------------------------------