//=========================================================================== // 2000 WANDERING SQUARES // Version 2 - High Definition // 21 October 2009 // // 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 rectangle method runs 10 times faster than the ellipse method. // The Canvas->Rectangle method runs 100 times faster than moving TImages. // 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 2000 // ====================================================== VARIABLES & CLASSES bool stop = true; TColor back = 0X79BBD7; TColor fore = 0X4080FF; int i; int dist, nearest; TColor color = 0X4080FF; // this is a description of an abstract class of shapes class shapes { public: double x, y ; double direction; double dx, dy; TColor color; float height, width; void erase (void) { Form1->PaintBox1->Canvas->Pen->Color = back; Form1->PaintBox1->Canvas->Brush->Color = back; Form1->PaintBox1->Canvas->Rectangle (x - 5, y - 5, x - 5 + width, y - 5 + height); } void draw (void) { Form1->PaintBox1->Canvas->Pen->Color = clBlack; Form1->PaintBox1->Canvas->Brush->Color = color; Form1->PaintBox1->Canvas->Rectangle (x - 5, y - 5, x - 5 + width, y - 5 + 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->PaintBox1->Canvas->Pen->Color = back; Form1->PaintBox1->Canvas->Brush->Color = back; Form1->PaintBox1->Canvas->Rectangle (x - 5, y - 5, x - 5 + width, y - 5 + height); } x += dx; y += dy; if (x > 695 || x < 5) { dx = -dx; } if (y > 695 || y < 5) { dy = -dy; } draw(); } } shape[POP]; // we now create an array of shapes-type objects // ================================================================ FUNCTIONS void initialize (void) { for (int i = 0; i < POP; i++) { shape[i].erase(); shape[i].height = 10; shape[i].width = 10; shape[i].color = 0X4080FF; shape[i].direction = double(i) / 3; shape[i].x = random(690) + 5; // in a 700 x 700 world shape[i].y = random(690) + 5; // in a 700 x 700 world shape[i].dy = double(random(100) - 50) / 25.00; shape[i].dx = double(random(100) - 50) / 25.00; Form1->PaintBox1->Canvas->Pen->Color = clBlack; Form1->PaintBox1->Canvas->Brush->Color = fore; Form1->PaintBox1->Canvas->Rectangle (shape[i].x - 5, shape[i].y - 5, shape[i].x - 5 + shape[i].width, shape[i].y - 5 + shape[i].height); } } void step (void) { for (int i = 0; i < POP; i++) { shape[i].move(); } } void refresh (void) { for (int i = 0; i < POP; i++) { Form1->PaintBox1->Canvas->Pen->Color = clBlack; Form1->PaintBox1->Canvas->Brush->Color = shape[i].color; Form1->PaintBox1->Canvas->Rectangle (shape[i].x - 5, shape[i].y - 5, shape[i].x - 5 + shape[i].width, shape[i].y - 5 + shape[i].height); } } // =========================================================== 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::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) { stop = true; dist = 1000; for (i = 0; i < POP; i++) { if (abs(X - shape[i].x) + abs(Y - shape[i].y) < dist) { dist = abs(X - shape[i].x) + abs(Y - shape[i].y); nearest = i; } } shape[nearest].color = color; Form1->PaintBox1->Canvas->Pen->Color = clBlack; Form1->PaintBox1->Canvas->Brush->Color = color; Form1->PaintBox1->Canvas->Rectangle( shape[nearest].x - 5, shape[nearest].y - 5, shape[nearest].x - 5 + shape[nearest].width, shape[nearest].y - 5 + shape[nearest].height); } //--------------------------------------------------------------------------- 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; } } //---------------------------------------------------------------------------