//=========================================================================== // CANVAS AGENTS WITH SPATIAL PROPERTIES //=========================================================================== #include #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //=========================================================================== // VARIABLES //=========================================================================== #define BACKGROUND clBtnFace #define PEN clBlack #define BRUSH clYellow float ave; int from; class vehicles { public: TPoint star[6]; double x; double y; int tag; double distance[10]; void draw (void) { star[0].x = x + 5; star[0].y = y - 22; star[1].x = x - 15; star[1].y = y + 28; star[2].x = x + 35; star[2].y = y - 2; star[3].x = x - 25; star[3].y = y - 2; star[4].x = x + 25; star[4].y = y + 28; star[5].x = x + 5; star[5].y = y - 26; Form1->Canvas->Pen->Color = PEN; Form1->Canvas->Brush->Color = BRUSH; Form1->Canvas->Polygon(star,5); Form1->Canvas->Font->Color = PEN; Form1->Canvas->Brush->Style = bsClear; Form1->Canvas->TextOut(x, y, tag); } void erase (void) { star[0].x = x + 5; star[0].y = y - 22; star[1].x = x - 15; star[1].y = y + 28; star[2].x = x + 35; star[2].y = y - 2; star[3].x = x - 25; star[3].y = y - 2; star[4].x = x + 25; star[4].y = y + 28; star[5].x = x + 5; star[5].y = y - 26; Form1->Canvas->Pen->Color = BACKGROUND; Form1->Canvas->Brush->Color = BACKGROUND; Form1->Canvas->Polygon(star,5); Form1->Canvas->Font->Color = BACKGROUND; Form1->Canvas->Brush->Style = bsClear; Form1->Canvas->TextOut(x, y, tag); } } agent[10]; //=========================================================================== // FUNCTIONS //=========================================================================== //--------------------------------------------------------- Compute Distances void computeDistances (void) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { agent[i].distance[j] = sqrt( pow(agent[i].x - agent[j].x, 2) + pow(agent[i].y - agent[j].y, 2)) ; } } } //------------------------------------------------------------ Draw Distances void drawDistances (int from) { for (int to = 0; to < 10; to++) { Form1->Canvas->MoveTo(agent[from].x, agent[from].y); Form1->Canvas->LineTo(agent[to].x, agent[to].y); Form1->Canvas->Brush->Style = bsSolid; Form1->Canvas->TextOut( agent[to].x + 30, agent[to].y + 30, agent[from].distance[to] ); } } //=========================================================================== // EVENT HANDLERS //=========================================================================== //--------------------------------------------------------- Initialize Button void __fastcall TForm1::ButtonInitializeClick(TObject *Sender) { for (int i = 0; i < 10; i++) { agent[i].x = random(400); agent[i].y = random(400); agent[i].tag = i; } } //--------------------------------------------------------------- Draw Button void __fastcall TForm1::ButtonDrawClick(TObject *Sender) { for (int i = 0; i < 10; i++) { agent[i].draw(); } } //-------------------------------------------------------------- Erase Button void __fastcall TForm1::ButtonEraseClick(TObject *Sender) { for (int i = 0; i < 10; i++) { agent[i].erase(); } } //---------------------------------------------------------- Distances Button void __fastcall TForm1::ButtonDistancesClick(TObject *Sender) { computeDistances(); drawDistances(from); } //-------------------------------------------------------------- Clear Button void __fastcall TForm1::ButtonClearClick(TObject *Sender) { Refresh(); } //---------------------------------------------------------------------------