//=========================================================================== // FLOCKING BEHAVIOR WITH THE IDEA OF KINSHIP // An option has been added so that the agents are able to // reproduce, die by old age, and die by an enemy. //=========================================================================== // The Leonardo image is 1200 by 600 pixels. // The Male (blue), Female (pink), Baby (yellow) images are 60 by 61 pixels. // The Snake image is 79 x 58 pixels. //--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "Unit1.h" #include "Unit2.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; } //--------------------------------------------------------------------------- #define DEAD 0 #define ALIVE 1 //=========================================================================== // 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; //network linkage [from][to] int links[24][24]; TImage *image[48] = {0}; // sets up an array of images class anAgent { public: double velocity; double direction; double newDirection; double newDistance; double lastDistance; int age; int aging; int like[24]; int hate[24]; float x; float y; bool sex; bool flocking; int lastNN; int encounters[24]; } agent[48]; double aveDir, aveVelo, sumXs, sumYs; // average directions & velocities int sumVisibleNeighbors; // visible neighbors 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; int numMales = 0; int numFemales = 0; //=========================================================================== // 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)); } //------------------------------------------------------------------- shuffle void shuffle (void) { for (int i = 0; i < 48; i++) { if (i>24) { image[i]->Visible = FALSE; } else image[i]->Visible = TRUE; //int age = random(50)+ iterations/20; agent[i].age = random(50); agent[i].aging = agent[i].age; 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; if (i % 2 == 0) { agent[i].sex = true; image[i] -> Picture = Form1->ImageWoman -> Picture; } else { agent[i].sex = false; image[i] -> Picture = Form1 ->ImageMan -> Picture; } } } //--------------------------------------------------------------------- reset void reset (void) { Form1->Refresh(); iterations = 0; Form1->EditIterations->Text = iterations; shuffle(); } //-------------------------------------------------------------------- circle void circle (void) { Form1->Refresh(); for (int i = 0; i < 48; 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 int 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 < 48; 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 < 48; 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; //ATTEMPT TO LET BABY AGENTS AGE INTO MALE/FEMALE /* if (agent[i].aging <15) { image[i] ->Picture= Form1->ImageBaby->Picture; } else if (agent[i].aging>= 15) { if (i % 2 == 0) { agent[i].sex = true; image[i] -> Picture = Form1->ImageWoman -> Picture; } else { agent[i].sex = false; image[i] -> Picture = Form1 ->ImageMan -> Picture; } } */ increment = Form1->TrackBarIncrement->Position; for (i = 0; i < 48; i++) { agent[i].aging = agent[i].age + iterations/20; // 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 //////////////////////// /////////////////////////////////////////////////////////////////////// //agents ignore each other nearestNeighbor(i); if (Form1->RadioGroupFlockingBehavior->ItemIndex==0 && agent[i].lastNN !=myNeighbor) { links[i][nearestNeighbor(i)]++; agent[i].lastNN = myNeighbor; //puts last } // adopt nearest neighbor's direction (problematic with BOUNCE) if (Form1->RadioGroupFlockingBehavior->ItemIndex == 1) { agent[i].direction = agent[nearestNeighbor(i)].direction; } // adopt nearest neighbor's velocity if (Form1->RadioGroupFlockingBehavior->ItemIndex == 2) { agent[i].velocity = agent[nearestNeighbor(i)].velocity; } // increment by half nearest neighbor's velocity if (Form1->RadioGroupFlockingBehavior->ItemIndex==3) { agent[i].velocity += agent[nearestNeighbor(i)].velocity / 50; } // adopt nearest neighbor's direction and speed if (Form1->RadioGroupFlockingBehavior ->ItemIndex== 4) { 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 == 5) { 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 == 6) { 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 == 7) { 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; } } // If you are near an opposite sex, you will become that // opposite sex if (Form1->RadioGroupFlockingBehavior->ItemIndex == 8) { agent[i].direction = agent[nearestNeighbor(i)].direction; image[i]->Picture= image[nearestNeighbor(i)]->Picture; agent[i].sex= agent[nearestNeighbor(i)].sex; agent[i].encounters[nearestNeighbor(i)]++; } //Create a family. For every male/female interaction, a baby //will be formed if (Form1->RadioGroupFlockingBehavior->ItemIndex == 9) { 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; if (i>24){ image[i]->Picture = Form1->ImageBaby->Picture; image[i]->Visible = TRUE; agent[i].age = 0; if (agent[i].sex == true && (agent[i].age + iterations/20) >15) { //image[i]->Picture = Form1->ImageBaby->Picture; //image[i]->Visible = FALSE; image[i] -> Picture = Form1->ImageWoman -> Picture; //image[i] == ALIVE; //image[i]->Visible = TRUE; } else if (agent[i].sex == false && (agent[i].age +iterations/20) >15){//if (agent[i].age> 15 && agent[i].sex == true){ //image[i]->Picture = Form1->ImageBaby->Picture; //image[i]->Visible = FALSE; image[i] -> Picture = Form1 ->ImageMan -> Picture; //image[i] == ALIVE; //image[i]->Visible = TRUE; } } } agent[i].lastNN = myNeighbor; } } //Death by old age if (Form1->RadioGroupFlockingBehavior->ItemIndex == 10) { if(agent[i].aging >100 && agent[i].sex == true) { image[i] == DEAD; image[i]->Visible = FALSE; } else if(agent[i].aging >100 && agent[i].sex == false) { image[i] == DEAD; image[i]->Visible = FALSE; } } //Death by Enemy if (Form1->RadioGroupFlockingBehavior->ItemIndex == 11) { nearestNeighbor(i); if (i>=24 && i <36) { image[i]->Picture = Form1->ImageEnemy->Picture; image[i]->Visible = TRUE; } if (i % 3 == 1 && i <24) { if (image[i]->Picture != image[nearestNeighbor(i)]->Picture){ //age[i]->Picture = Form1->ImageEnemy->Picture; image[i] ->Visible = FALSE; } } else if (i % 3 == 2 && i < 24){ image[i] ->Visible = FALSE; } } //Cycle of Life and Death if (Form1->RadioGroupFlockingBehavior->ItemIndex == 12) { 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; if (i>24){ image[i]->Picture = Form1->ImageBaby->Picture; image[i]->Visible = TRUE; agent[i].age = 0; if (agent[i].sex == true && (agent[i].age + iterations/20) >=15) { //image[i]->Picture = Form1->ImageBaby->Picture; //image[i]->Visible = FALSE; image[i] -> Picture = Form1->ImageWoman -> Picture; //image[i] == ALIVE; //image[i]->Visible = TRUE; } else if (agent[i].sex == false && (agent[i].age +iterations/20) >=15){ //image[i]->Picture = Form1->ImageBaby->Picture; //image[i]->Visible = FALSE; image[i] -> Picture = Form1 ->ImageMan -> Picture; //image[i] == ALIVE; //image[i]->Visible = TRUE; } } if(agent[i].aging >100 && agent[i].sex == true) { image[i] == DEAD; image[i]->Visible = FALSE; } else if(agent[i].aging >100 && agent[i].sex == false) { image[i] == DEAD; image[i]->Visible = FALSE; } if (i<48) { if (agent[i].sex == true && (agent[i].age + iterations/20) >=15) { //image[i]->Picture = Form1->ImageBaby->Picture; //image[i]->Visible = FALSE; image[i] -> Picture = Form1->ImageWoman -> Picture; //image[i] == ALIVE; //image[i]->Visible = TRUE; } } 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); } } } } } //---------------------------------------------------------------------- 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 < 48; 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; EditAge->Text = agent[image->Tag].aging; 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); } //---------------------------------------------------------------------------