//=========================================================================== // FLOCKING - March 2007 - Version 9 // An option has been added so that the agent will adopt the // average behavior of its neighbors within a given radius. //=========================================================================== // The Leonardo image is 612 by 612 pixels. // The Voyager images are 100 by 44 pixels. //--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "Unit1.h" #include "stdlib.h" #include //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // Double buffering eliminates some screen flicker but takes 30% longer. DoubleBuffered = true; } //--------------------------------------------------------------------------- //=========================================================================== // GLOBAL VARIABLES //=========================================================================== // ------------------------------------------------- your program begins here // this sets up graphics functions to handle the import of a bitmap image Graphics::TBitmap* b = new Graphics::TBitmap(); RGBTRIPLE* t; // these arrays capture the Red, Green and Blue values of a bitmap image // the .bmp image is used as a geographic map of additional rules int bmpRed[612][612]; int bmpGreen[612][612]; int bmpBlue[612][612]; // this sets up the MIDI functionality int midiport = 0; HMIDIOUT device; union { public: unsigned long word; unsigned char data[4]; } message; int soundType = 0; int instrument; int note; TImage *image[24] = {0}; // sets up an array of images class anAgent { public: double velocity; double direction; double newDirection; double newDistance; double lastDistance; float x; float y; bool sex; bool flocking; int lastNN; int gender; int first; int second; int third; int currentCompatibility; } agent[24]; double aveDir, aveVelo, sumXs, sumYs; // average directions & velocities int sumVisibleNeighbors; // visible neighbors int compatibilityFromTo[24][24]; int increment = 5; double x, y, z; bool stop = true; bool wasStopped = false; bool showNNs = false; bool collisions = false; int iterations; int i; int myNeighbor; int dist; int radius = 30; int chosenAgent; int imageDownX, imageDownY; bool agentWasJustChosen = false; //=========================================================================== // FUNCTIONS //=========================================================================== //-------------------------------------------------- FILL ARRAYS FROM BITMAP // This fills the Red, Green and Blue arrays from the bitmap. // It converts the color values (0-255) to preferences (0-7). // The conversion divides the color values by 35. // With a gray-scale image the Red, Green and Blue values are identical. void bmpToArray(Graphics::TBitmap* bm) { for (int y = 0; y < 612; y++) { t = (RGBTRIPLE*)bm->ScanLine[y]; for (int x = 0; x < 612; x++) { bmpRed[x][y] = t->rgbtRed; // used for preference 0-4 bmpGreen[x][y] = t->rgbtGreen; // used for distance 0-3 bmpBlue[x][y] = t->rgbtBlue; // used for neighborhood 0-2 t++; } } } //------------------------------------------------------------- RENDER BITMAP // This renders the imported bitmap to the screen. // It does not change the values in any of the arrays. // All it does is confirm that the image has been acquired. void renderBMP(Graphics::TBitmap* bm) { for (int y = 0; y < 612; y++) { t = (RGBTRIPLE*)bm->ScanLine[y]; for (int x = 0; x < 612; x++) { Form1->Canvas->Pixels[x][y]= static_cast(RGB(t->rgbtRed, t->rgbtGreen, t->rgbtBlue)); t++; } } } //---------------------------------------------------------------- color ramp int colorRamp(int range, int value) { int pixelDistanceAlongPath = (value * 1792) / range; int red, green, blue; // Which edge of the color cube are we on? if (pixelDistanceAlongPath < 256) { // Edge 1 from BLACK to BLUE red=0; green=0; blue=pixelDistanceAlongPath; } else if (pixelDistanceAlongPath < 512) { // Edge 2 from BLUE to CYAN red =0; green=pixelDistanceAlongPath-256; blue=255; } else if (pixelDistanceAlongPath < 768) { // Edge 3 from CYAN to GREEN red =0; green =255; blue= 255-(pixelDistanceAlongPath-512); } else if (pixelDistanceAlongPath < 1024) { // Edge 4 from GREEN to YELLOW red= (pixelDistanceAlongPath-768); green =255; blue =0; } else if (pixelDistanceAlongPath <1280) { // Edge 5 from YELLOW to RED red =255; green=255-(pixelDistanceAlongPath-1024); blue =0; } else if (pixelDistanceAlongPath < 1536) { // Edge 6 from RED to MAGENTA red =255; green=0; blue=pixelDistanceAlongPath -1280; } else { // Edge 7 from MAGENTA to WHITE red =255; green=pixelDistanceAlongPath-1536; blue =255; } return (RGB(red, green, blue)); } //--------------------------------------------------------------initialization void shuffle (void) { for (int i = 0; i < 24; i++) { agent[i].x = random(560) + 30; image[i]->Left = agent[i].x - image[i]->Width / 2; agent[i].y = random(510) + 50; image[i]->Top = agent[i].y - image[i]->Height / 2; agent[i].direction = (float(random(1000)) / 500) * M_PI; agent[i].velocity = float(random(1000)) / 1500 + .1; agent[i].flocking = false; agent[i].currentCompatibility = 0; agent[i].gender = random(2); agent[i].first = random(4); agent[i].second = random(4); while(agent[i].second == agent[i].first) agent[i].second = random(4); agent[i].third = random(4); while(agent[i].third == agent[i].second || agent[i].third == agent[i].first) agent[i].third = random(4); if (i % 2 == 0) { agent[i].sex = true; } else agent[i].sex = false; // compute compatibility for (int j = 0; j < 24; j++) { if (agent[i].sex == agent[j].sex) { // same sex compatibilityFromTo[i][j] = -1; compatibilityFromTo[j][i] = -1; } else { // opposite sex compatibilityFromTo[i][j] = 0; compatibilityFromTo[j][i] = 0; if (agent[i].first == agent[j].first) { compatibilityFromTo[i][j] = 1; compatibilityFromTo[j][i] = 1; if (agent[i].second == agent[j].second) { compatibilityFromTo[i][j] = 2; compatibilityFromTo[j][i] = 2; if (agent[i].third == agent[j].third) { compatibilityFromTo[i][j] = 3; compatibilityFromTo[j][i] = 3; } } } } } } } //--------------------------------------------------------------------- reset void reset (void) { Form1->Refresh(); iterations = 0; Form1->EditIterations->Text = iterations; shuffle(); } //-------------------------------------------------------------------- circle void circle (void) { Form1->Refresh(); for (int i = 0; i < 24; i++) { agent[i].x = 274 + 250 * cos(i * (2 * M_PI / 24)) + 30; agent[i].y = 274 + 250 * sin(i * (2 * M_PI / 24)) + 30; image[i]->Left = agent[i].x - image[i]->Width / 2; image[i]->Top = agent[i].y - image[i]->Height / 2; } } //----------------------------------- returns a direction to a specific agent double directionFromTo (int from, int to) { x = agent[to].x - agent[from].x; y = agent[to].y - agent[from].y; return atan(y / x); } //---------------------- finds average Velo and Dir of neighbor with a radius void averageOfNeighbors (int me) { aveDir = 0; // directions aveVelo = 0; // velocities sumXs = 0; // x component sumYs = 0; // y component sumVisibleNeighbors = 0; // number of visible neighbors for (int i = 0; i < 24; i++) { if (i == me) continue; x = agent[i].x - agent[me].x; y = agent[i].y - agent[me].y; z = sqrt(x * x + y * y); if (z < radius) { // average all visible neighbors sumVisibleNeighbors++; sumYs += agent[i].velocity * sin(agent[i].direction); sumXs += agent[i].velocity * cos(agent[i].direction); } } if ((sumYs != 0) && (sumXs != 0) && (sumVisibleNeighbors > 0)) { aveDir = atan(sumYs / sumXs); aveVelo = sqrt(sumXs * sumXs + sumYs * sumYs) / sumVisibleNeighbors; } } //----------------------------------------------- returns my nearest neighbor int nearestNeighbor (int me) { myNeighbor = 0; // my nearest neighbor dist = 10000; // shortest distance for (int i = 0; i < 24; i++) { if (i == me) continue; x = agent[i].x - agent[me].x; y = agent[i].y - agent[me].y; z = sqrt(x * x + y * y); if (z < dist) { // select nearest neighbor dist = z; myNeighbor = i; } } return myNeighbor; } //-------------------------------------------------- show my nearest neighbor void showNearestNeighbor (int me) { Form1->Canvas->Pen->Color = clRed; Form1->Canvas->MoveTo(agent[me].x, agent[me].y); Form1->Canvas->LineTo(agent[nearestNeighbor(me)].x, agent[nearestNeighbor(me)].y); /* if (agent[me].sex == true) { // from F Form1->Canvas->MoveTo(agent[me].x - 7, agent[me].y - 17); } else { Form1->Canvas->MoveTo(agent[me].x, // from M agent[me].y - 22); } if (agent[nearestNeighbor(me)].sex == true) { // to F Form1->Canvas->LineTo(agent[nearestNeighbor(me)].x - 7, agent[nearestNeighbor(me)].y - 17); } else { // to M Form1->Canvas->LineTo(agent[nearestNeighbor(me)].x, agent[nearestNeighbor(me)].y - 22); } */ } //---------------------------------------------------- show perceptual circle void showPerceptualCircle (int me) { Form1->Canvas->Brush->Style = bsClear; Form1->Canvas->Ellipse(agent[me].x - radius, agent[me].y - radius, agent[me].x + radius, agent[me].y + radius); } //---------------------------------------------------------------------- step void step (void) { //Form1->Refresh(); iterations++; Form1->EditIterations->Text = iterations; increment = Form1->TrackBarIncrement->Position; for (int i = 0; i < 24; i++) { // calculate agent's new position based on velocity and direction agent[i].x += agent[i].velocity * cos(agent[i].direction) * increment; agent[i].y += agent[i].velocity * sin(agent[i].direction) * increment; // move the visualization on the screen accordingly image[i]->Left = agent[i].x - image[i]->Width / 2; image[i]->Top = agent[i].y - image[i]->Height / 2; /////////////////////////////////////////////////////////////////////// ///////////////////// FLOCKING BEHAVIORS BELOW //////////////////////// /////////////////////////////////////////////////////////////////////// // adopt nearest neighbor's direction (problematic with BOUNCE) if (Form1->RadioGroupFlockingBehavior->ItemIndex == 1) { agent[i].direction = agent[nearestNeighbor(i)].direction; } // increment direction by 1/50 your nearest neighbor's if (Form1->RadioGroupFlockingBehavior->ItemIndex == 2) { agent[i].direction += agent[nearestNeighbor(i)].direction / 30; if (agent[i].direction > 2 * M_PI) { // modulo divide agent[i].direction = agent[i].direction - 2 * M_PI; } } // decrement direction by 1/100 your nearest neighbor's if (Form1->RadioGroupFlockingBehavior->ItemIndex == 3) { agent[i].direction -= agent[nearestNeighbor(i)].direction / 100; if (agent[i].direction > 2 * M_PI) { // modulo divide agent[i].direction = agent[i].direction - 2 * M_PI; } } // adopt nearest neighbor's velocity if (Form1->RadioGroupFlockingBehavior->ItemIndex == 4) { agent[i].velocity = agent[nearestNeighbor(i)].velocity; } // increment by half nearest neighbor's velocity if (Form1->RadioGroupFlockingBehavior->ItemIndex == 5) { agent[i].velocity += agent[nearestNeighbor(i)].velocity / 50; } // adopt nearest neighbor's direction and speed if (Form1->RadioGroupFlockingBehavior->ItemIndex == 6) { agent[i].velocity = agent[nearestNeighbor(i)].velocity; agent[i].direction = agent[nearestNeighbor(i)].direction; } // STRAIGHT COUPLES // if same sex: both reverse direction and take random velocities // if opposite sex: both take average direction and NN velocity if (Form1->RadioGroupFlockingBehavior->ItemIndex == 7) { nearestNeighbor(i); if ((dist < radius) && (agent[i].lastNN != myNeighbor)) { if (agent[i].sex == agent[myNeighbor].sex) { // reverse agent[i].direction += M_PI; if (agent[i].direction > 2 * M_PI) { agent[i].direction = agent[i].direction - 2 * M_PI; } agent[myNeighbor].direction += M_PI; if (agent[myNeighbor].direction > 2 * M_PI) { agent[myNeighbor].direction = agent[myNeighbor].direction - 2 * M_PI; } agent[i].velocity = float(random(1000)) / 1500 + .1; agent[myNeighbor].velocity = float(random(1000)) / 1500 + .1; } else { // adopt aveDir = (agent[i].direction + agent[myNeighbor].direction) / 2; agent[i].direction = aveDir; agent[myNeighbor].direction = aveDir; agent[i].velocity = agent[myNeighbor].velocity; } agent[i].lastNN = myNeighbor; } } // GAY/LES COUPLES // if opposite sex: both reverse direction and take random velocities // if same sex: both take average direction and NN velocity if (Form1->RadioGroupFlockingBehavior->ItemIndex == 8) { nearestNeighbor(i); if ((dist < radius) && (agent[i].lastNN != myNeighbor)) { if (agent[i].sex != agent[myNeighbor].sex) { // reverse agent[i].direction += M_PI; if (agent[i].direction > 2 * M_PI) { agent[i].direction = agent[i].direction - 2 * M_PI; } agent[myNeighbor].direction += M_PI; if (agent[myNeighbor].direction > 2 * M_PI) { agent[myNeighbor].direction = agent[myNeighbor].direction - 2 * M_PI; } agent[i].velocity = float(random(1000)) / 1500 + .1; agent[myNeighbor].velocity = float(random(1000)) / 1500 + .1; } else { // adopt aveDir = (agent[i].direction + agent[myNeighbor].direction) / 2; agent[i].direction = aveDir; agent[myNeighbor].direction = aveDir; agent[i].velocity = agent[myNeighbor].velocity; } agent[i].lastNN = myNeighbor; } } // Your direction and velocity are the average of those // you can see within a given radius... if (Form1->RadioGroupFlockingBehavior->ItemIndex == 9) { averageOfNeighbors(i); if (aveVelo != 0 && aveDir != 0 && sumVisibleNeighbors > 1) { agent[i].velocity = aveVelo; agent[i].direction = aveDir; agent[i].flocking = true; } else if (agent[i].flocking == TRUE) { agent[i].direction = (float(random(1000)) / 500) * M_PI; agent[i].velocity = float(random(1000)) / 1500 + .1; agent[i].flocking = false; } } //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ // LESLIE PERKINS - a perfect match // STRAIGHT COUPLES // if same sex: both reverse direction and take random velocities // if opposite sex: both take average direction and NN velocity if (Form1->RadioGroupFlockingBehavior->ItemIndex == 10) { nearestNeighbor(i); if ((dist < radius) && (agent[i].lastNN != myNeighbor)) { if (agent[i].sex == agent[myNeighbor].sex) { // no match /*agent[i].direction += M_PI; if (agent[i].direction > 2 * M_PI) { agent[i].direction = agent[i].direction - 2 * M_PI; } agent[myNeighbor].direction += M_PI; if (agent[myNeighbor].direction > 2 * M_PI) { agent[myNeighbor].direction = agent[myNeighbor].direction - 2 * M_PI; } agent[i].velocity = float(random(1000)) / 1500 + .1; agent[myNeighbor].velocity = float(random(1000)) / 1500 + .1;*/ } else { // match if (compatibilityFromTo[i][myNeighbor] > agent[i].currentCompatibility) { // this is asymmetrical aveDir = (agent[i].direction + agent[myNeighbor].direction) / 2; agent[i].direction = aveDir; agent[myNeighbor].direction = aveDir; agent[i].velocity = agent[myNeighbor].velocity; agent[i].currentCompatibility = compatibilityFromTo[i][myNeighbor]; } } agent[i].lastNN = myNeighbor; } } //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ /////////////////////////////////////////////////////////////////////// ///////////////////// FLOCKING BEHAVIORS ABOVE /////////////////////// /////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// BOUNCE if (Form1->RadioGroupBoundary->ItemIndex == 0) { if (agent[i].x > 560) { agent[i].direction = M_PI - agent[i].direction; if (soundType == 1) Beep(196, 50); if (soundType == 2) { PlaySound ("C:\\WINDOWS\\Media\\Windows XP Balloon.wav", "", SND_ASYNC); } if (soundType == 3) { PlaySound ("C:\\Program Files\\Windows NT\\Pinball\\SOUND181.wav", "", SND_ASYNC); } if (soundType == 4) { message.data[0] = 0x90; message.data[1] = 55; message.data[2] = 100; message.data[3] = 0; midiOutShortMsg(device, message.word); } } if (agent[i].y > 560) { agent[i].direction = 2 * M_PI - agent[i].direction; if (soundType == 1) Beep(261, 50); if (soundType == 2) { PlaySound ("C:\\WINDOWS\\Media\\Windows XP pop-up blocked.wav", "", SND_ASYNC); } if (soundType == 3) { PlaySound ("C:\\Program Files\\Windows NT\\Pinball\\SOUND243.wav", "", SND_ASYNC); } if (soundType == 4) { message.data[0] = 0x90; message.data[1] = 60; message.data[2] = 100; message.data[3] = 0; midiOutShortMsg(device, message.word); } } if (agent[i].x < 30) { agent[i].direction = M_PI - agent[i].direction; if (soundType == 1) Beep(330, 50); if (soundType == 2) { PlaySound ("C:\\WINDOWS\\Media\\Windows XP Balloon.wav", "", SND_ASYNC); } if (soundType == 3) { PlaySound ("C:\\Program Files\\Windows NT\\Pinball\\SOUND713.wav", "", SND_ASYNC); } if (soundType == 4) { message.data[0] = 0x90; message.data[1] = 64; message.data[2] = 100; message.data[3] = 0; midiOutShortMsg(device, message.word); } } if (agent[i].y < 50) { agent[i].direction = 2 * M_PI - agent[i].direction; if (soundType == 1) Beep(392, 50); if (soundType == 2) { PlaySound ("C:\\WINDOWS\\Media\\Windows XP pop-up blocked.wav", "", SND_ASYNC); } if (soundType == 3) { PlaySound ("C:\\Program Files\\Windows NT\\Pinball\\SOUND735.wav", "", SND_ASYNC); } if (soundType == 4) { message.data[0] = 0x90; message.data[1] = 67; message.data[2] = 100; message.data[3] = 0; midiOutShortMsg(device, message.word); } } // modular divide direction by one rotation if (agent[i].direction > 2 * M_PI) { agent[i].direction = agent[i].direction - 2 * M_PI; } if (agent[i].direction < 0) { agent[i].direction = agent[i].direction + 2 * M_PI; } } ///////////////////////////////////////////////////////////////// WRAP else { if (agent[i].x > 590) { agent[i].x = 30; image[i]->Left = agent[i].x - image[i]->Width / 2; if (soundType == 1) Beep(196, 50); if (soundType == 2) { PlaySound ("C:\\WINDOWS\\Media\\Windows XP Balloon.wav", "", SND_ASYNC); } if (soundType == 3) { PlaySound ("C:\\Program Files\\Windows NT\\Pinball\\SOUND181.wav", "", SND_ASYNC); } if (soundType == 4) { message.data[0] = 0x90; message.data[1] = 55; message.data[2] = 100; message.data[3] = 0; midiOutShortMsg(device, message.word); } } if (agent[i].y > 560) { agent[i].y = 50; image[i]->Top = agent[i].y - image[i]->Height / 2; if (soundType == 1) Beep(330, 50); if (soundType == 2) { PlaySound ("C:\\WINDOWS\\Media\\Windows XP Balloon.wav", "", SND_ASYNC); } if (soundType == 3) { PlaySound ("C:\\Program Files\\Windows NT\\Pinball\\SOUND713.wav", "", SND_ASYNC); } if (soundType == 4) { message.data[0] = 0x90; message.data[1] = 64; message.data[2] = 100; message.data[3] = 0; midiOutShortMsg(device, message.word); } } if (agent[i].x < 30) { agent[i].x = 590; image[i]->Left = agent[i].x - image[i]->Width / 2; if (soundType == 1) Beep(261, 50); if (soundType == 2) { PlaySound ("C:\\WINDOWS\\Media\\Windows XP pop-up blocked.wav", "", SND_ASYNC); } if (soundType == 3) { PlaySound ("C:\\Program Files\\Windows NT\\Pinball\\SOUND243.wav", "", SND_ASYNC); } if (soundType == 4) { message.data[0] = 0x90; message.data[1] = 60; message.data[2] = 100; message.data[3] = 0; midiOutShortMsg(device, message.word); } } if (agent[i].y < 50) { agent[i].y = 560; image[i]->Top = agent[i].y - image[i]->Height / 2; if (soundType == 1) Beep(392, 50); if (soundType == 2) { PlaySound ("C:\\WINDOWS\\Media\\Windows XP pop-up blocked.wav", "", SND_ASYNC); } if (soundType == 3) { PlaySound ("C:\\Program Files\\Windows NT\\Pinball\\SOUND735.wav", "", SND_ASYNC); } if (soundType == 4) { message.data[0] = 0x90; message.data[1] = 67; message.data[2] = 100; message.data[3] = 0; midiOutShortMsg(device, message.word); } } // end last boundary check } // end wrap } // end loop } // end step //---------------------------------------------------------------------- Run void run (void) { Form1->Refresh(); stop = false; message.data[0] = 0xC0; message.data[1] = 6; message.data[2] = 100; message.data[3] = 0; midiOutShortMsg(device, message.word); while (stop == false) { step(); Application->ProcessMessages(); } if (showNNs) { for (int i = 0; i < 24; i++) { showNearestNeighbor(i); } } showNNs = false; } //=========================================================================== // EVENT HANDLERS //=========================================================================== //------------------------------------------------------------ On Form Create void __fastcall TForm1::FormCreate(TObject *Sender) { midiOutOpen(&device, midiport, 0, 0, CALLBACK_NULL); randomize(); // defines an array of images with properties and events for (int i = 0; i < 24; i++) { image[i] = new TImage(this); image[i]->Parent = Form1; image[i]->Visible = true; image[i]->OnMouseDown = ImageMouseDown; image[i]->OnMouseUp = ImageMouseUp; image[i]->OnMouseMove = ImageMouseMove; //image[i]->Height = 100; //image[i]->Width = 50; image[i]->AutoSize= true; image[i]->Left = 0; image[i]->Top = 0; image[i]->Visible = true; if (i % 2 == 0) { image[i]->Picture = Form1->ImageWoman->Picture; } else image[i]->Picture = Form1->ImageMan->Picture; image[i]->Transparent = true; image[i]->Tag = i; } shuffle(); } //------------------------------------------------------- On image Mouse Down // An exception to the rule which says "Let Borland write event handlers." // This one you must type in yourself in addition to a reference to it // in Unit1.h (look at the source code in that unit). void __fastcall TForm1::ImageMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { TImage *image = dynamic_cast(Sender); // The following will be used for mouseMove and mouseUp events... // Captures the index number of the image that was clicked chosenAgent = image->Tag; // remembers is we were stopped or running wasStopped = stop; stop = true; if (Button == 0) { // Begin drag object // Remembers that an image was chosen for mouseMove and mouseUp agentWasJustChosen = true; // Remembers where on the image the mouse was downed imageDownX = X; imageDownY = Y; } else { // Show nearest neighbor showNearestNeighbor(chosenAgent); showPerceptualCircle(chosenAgent); } } //------------------------------------------------------- On image Mouse Move // An exception to the rule which says "Let Borland write event handlers." // This one you must type in yourself in addition to a reference to it // in Unit1.h (look at the source code in that unit). void __fastcall TForm1::ImageMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { TImage *image = dynamic_cast(Sender); chosenAgent = image->Tag; if (agentWasJustChosen) { // this drags the image along... image->Left = image->Left + X - imageDownX; image->Top = image->Top + Y - imageDownY; agent[chosenAgent].x = image->Left + image->Width / 2;; agent[chosenAgent].y = image->Top + image->Height / 2;; } // When the mouse moves over an image its values are displayed EditAgent->Text = image->Tag; EditX->Text = int(agent[chosenAgent].x); EditY->Text = int(agent[chosenAgent].y); EditDirection->Text = agent[image->Tag].direction; EditVelocity->Text = agent[image->Tag].velocity; EditFirst->Text = agent[image->Tag].first; EditSecond->Text = agent[image->Tag].second; EditThird->Text = agent[image->Tag].third; EditCurrentCompatibility->Text = agent[image->Tag].currentCompatibility; if (agent[image->Tag].sex) { EditSex->Text= "F"; } else EditSex->Text= "M"; EditNN->Text = nearestNeighbor(image->Tag); } //--------------------------------------------------------- On image Mouse Up // An exception to the rule which says "Let Borland write event handlers." // This one you must type in yourself in addition to a reference to it // in Unit1.h (look at the source code in that unit). void __fastcall TForm1::ImageMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { TImage *image = dynamic_cast(Sender); // this is the end of the drag... agentWasJustChosen = false; EditAgent->Text = image->Tag; // When the mouse moves over an image its values are displayed EditX->Text = int(agent[chosenAgent].x); EditY->Text = int(agent[chosenAgent].y); EditDirection->Text = agent[image->Tag].direction; EditVelocity->Text = agent[image->Tag].velocity; // if we were running before the drag, then run... Form1->Refresh(); if (!wasStopped) { run(); } } //---------------------------------------------------------------- run button void __fastcall TForm1::ButtonRunClick(TObject *Sender) { run(); } //--------------------------------------------------------------- step button void __fastcall TForm1::ButtonStepClick(TObject *Sender) { stop = true; step(); } //--------------------------------------------------------------- stop button void __fastcall TForm1::ButtonStopClick(TObject *Sender) { stop = true; } //---------------------------------------------------------- randomize button void __fastcall TForm1::ButtonResetClick(TObject *Sender) { reset(); } //------------------------------------------------------------- circle button void __fastcall TForm1::ButtonCircleClick(TObject *Sender) { circle(); } //------------------------------------------------------ sound type RadioGroup void __fastcall TForm1::RadioGroupSonificationClick(TObject *Sender) { soundType = RadioGroupSonification->ItemIndex; } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonNNsClick(TObject *Sender) { if (stop) { for (int i = 0; i < 24; i++) { showNearestNeighbor(i); } } else { showNNs = true; stop = true; } } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBarRadiusChange(TObject *Sender) { Form1->Refresh(); radius = TrackBarRadius->Position; Form1->EditRadius->Text = radius; if (stop == true) { for (int i = 0; i < 24; i++) { showPerceptualCircle(i); } } } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonOpenBMPClick(TObject *Sender) { if (OpenBitMapDialog->Execute() ) b->LoadFromFile(OpenBitMapDialog->FileName); bmpToArray(b); renderBMP(b); } //---------------------------------------------------------------------------