//=========================================================================== // Kevin Fujii TRAFFIC FLOW - June 2007 //=========================================================================== // This is a two-dimensional traffic flow simulation. // Three sets of destination points are defined, which can be changed in the // simulation. The velocity of an agent is calculated by the distance between // the agent and the closest agent in its direction of travel and its mass. //=========================================================================== //--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "Unit1.h" // The following two lines are needed to enable the randomizer #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... DoubleBuffered = true; } //--------------------------------------------------------------------------- //=========================================================================== // GLOBAL VARIABLES //=========================================================================== // ------------------------------------------------- your program begins here int midiport = 0; HMIDIOUT device; union { public: unsigned long word; unsigned char data[4]; } message; int soundType = 0; int instrument; int note; TShape *shape[24] = {0}; // sets up an array of shapes class anAgent { public: double angle; double lastAngle; double deltaAngle; double velocity; double lastVelocity; double deltaVelocity; double angleAhead; double L1AngleAhead; double L2AngleAhead; double L3AngleAhead; double deltaAngleAhead; double x; double y; } agent[24]; double increment = 2.0; float deltaAngle; bool stop = true; bool wasStopped = false; double iterations; int i, next; int chosenAgent; int shapeDownX, shapeDownY; bool agentWasJustChosen = false; int delay = 2; float distx1[24], disty1[24], distx2[24], disty2[24], distx3[24], disty3[24]; float xstep[24], ystep[24], xstep2[24], ystep2[24], xstep3[24], ystep3[24]; int type[24], type2[24]; float xtime[24], ytime[24], xtime2[24], ytime2[24], xtime3[24], ytime3[24]; double deadline1 = 150, deadline2 = 300, deadline3 = 450; int x1signs[24]; int y1signs[24]; int x2signs[24]; int y2signs[24]; int x3signs[24]; int y3signs[24]; int late[24]; double counter1 = 0; double counter2 = 0; double counter3 = 0; double counter4 = 0; double counter5 = 0; double mass[24]; double masscount[5]; float dest1x = 300; float dest1y = 300; float dest2x1 = 100, dest2y1 = 100, dest2x2 = 400, dest2y2 = 100; float dest3x1 = 200, dest3y1 = 400, dest3x2 = 400, dest3y2 = 400, dest3x3 = 250, dest3y3 = 50; float planahead = 0; float speedlimitx = 10, speedlimity = 10; int which = 0; int madedeadline1 = 0, madedeadline2 = 0, madedeadline3 = 0; int destradius = 10; double loopediterations = 0; int calcchoice = 0; //=========================================================================== // FUNCTIONS //=========================================================================== //---------------------------------------------------------------- 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 < 24; i++) { agent[i].x = random(400) + 30; shape[i]->Left = agent[i].x - shape[i]->Width / 2; agent[i].y = random(400) + 30; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } } //--------------------------------------------------------------------- reset void reset (void) { iterations = 0; loopediterations = 0; madedeadline1 = 0; madedeadline2 = 0; madedeadline3 = 0; counter1 = 0; counter2 = 0; counter3 = 0; counter4 = 0; counter5 = 0; for (i = 0; i < 24; i++) { late[i] = 0; } Form1->EditDL1->Text = ""; Form1->EditDL2->Text = ""; Form1->EditDL3->Text = ""; Form1->EditIterations->Text = iterations; shuffle(); } //-------------------------------------------------------------------- circle void circle (void) { for (int i = 0; i < 24; i++) { agent[i].angle = i * (2 * M_PI / 24); agent[i].angleAhead = 2 * M_PI / 24; agent[i].L1AngleAhead = agent[i].angleAhead; agent[i].x = 200 + 180 * cos(agent[i].angle) + 30; agent[i].y = 200 + 180 * sin(agent[i].angle) + 30; shape[i]->Left = agent[i].x - shape[i]->Width / 2; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } } //---------------------------------------------------------- nearest neighbor int nearestNeighbor (int me) { int myNeighbor; double x, y, z; int dist = 10000; 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) { dist = z; myNeighbor = i; } } return dist; } double nearestNeighborSE (int me) { int myNeighbor; double x, y, z =10001; int dist = 10000; for (int i = 0; i < 24; i++) { if (i == me) continue; if (agent[i].x > agent[me].x && agent [i].y > agent[me].y) { x = agent[i].x - agent[me].x; y = agent[i].y - agent[me].y; z = sqrt(x * x + y * y); } if (z < dist) { dist = z; myNeighbor = i; } } return dist; } double nearestNeighborNE (int me) { int myNeighbor; double x, y, z =10001; int dist = 10000; for (int i = 0; i < 24; i++) { if (i == me) continue; if (agent[i].x > agent[me].x && agent [i].y <= agent[me].y) { x = agent[i].x - agent[me].x; y = agent[i].y - agent[me].y; z = sqrt(x * x + y * y); } if (z < dist) { dist = z; myNeighbor = i; } } return dist; } double nearestNeighborSW (int me) { int myNeighbor; double x, y, z =10001; int dist = 10000; for (int i = 0; i < 24; i++) { if (i == me) continue; if (agent[i].x <= agent[me].x && agent [i].y > agent[me].y) { x = agent[i].x - agent[me].x; y = agent[i].y - agent[me].y; z = sqrt(x * x + y * y); } if (z < dist) { dist = z; myNeighbor = i; } } return dist; } double nearestNeighborNW (int me) { int myNeighbor; double x, y, z =10001; int dist = 10000; for (int i = 0; i < 24; i++) { if (i == me) continue; if (agent[i].x <= agent[me].x && agent [i].y <= agent[me].y) { x = agent[i].x - agent[me].x; y = agent[i].y - agent[me].y; z = sqrt(x * x + y * y); } if (z < dist) { dist = z; myNeighbor = i; } } return dist; } //---------------------------------------------------------------------- step void step (void) { int directionint1[24]; int directionint2[24]; int directionint3[24]; iterations++; loopediterations++; if (loopediterations == deadline3 + 1) { loopediterations = 1; } if (loopediterations == deadline1 - 1) { madedeadline1 = 0; } if (loopediterations == deadline2 - 1) { madedeadline2 = 0; } if (loopediterations == deadline3 - 1) { madedeadline3 = 0; } Form1->EditIterations->Text = iterations; for (i = 0; i < 24; i++) { distx1[i] = abs(dest1x - agent[i].x); disty1[i] = abs(dest1y - agent[i].y); type[i] = i%2; type2[i] = i%3; if (type[i] == 0) { distx2[i] = abs(dest2x1 - agent[i].x); disty2[i] = abs(dest2y1 - agent[i].y); } if(type[i] == 1) { distx2[i] = abs(dest2x2 - agent[i].x); disty2[i] = abs(dest2y2 - agent[i].y); } if (type2[i] == 0) { distx3[i] = abs(dest3x1 - agent[i].x); disty3[i] = abs(dest3y1 - agent[i].y); } if (type2[i] == 1) { distx3[i] = abs(dest3x2 - agent[i].x); disty3[i] = abs(dest3y2 - agent[i].y); } if (type2[i] == 2) { distx3[i] = abs(dest3x3 - agent[i].x); disty3[i] = abs(dest3y3 - agent[i].y); } if (dest1x-agent[i].x > 0) { x1signs[i] = 1; } else {x1signs[i] = -1;} if (dest1y-agent[i].y > 0) { y1signs[i] = 1; } else {y1signs[i] = -1;} if (x1signs[i] == 1 && y1signs[i] == 1) { directionint1[i] = 1; //SE } if (x1signs[i] == -1 && y1signs[i] == 1) { directionint1[i] = 2; //SW } if (x1signs[i] == 1 && y1signs[i] == -1) { directionint1[i] = 3; //NE } if (x1signs[i] == -1 && y1signs[i] == -1) { directionint1[i] = 4; //NW } if ((dest2x1-agent[i].x > 0 && type[i] == 0) || (dest2x2-agent[i].x > 0 && type[i] == 1)) { x2signs[i] = 1; } else {x2signs[i] = -1;} if ((dest2y1-agent[i].y > 0 && type[i] == 0) || (dest2y2-agent[i].y > 0 && type[i] == 1)) { y2signs[i] = 1; } else {y2signs[i] = -1;} if (x2signs[i] == 1 && y2signs[i] == 1) { directionint2[i] = 1; //SE } if (x2signs[i] == -1 && y2signs[i] == 1) { directionint2[i] = 2; //SW } if (x2signs[i] == 1 && y2signs[i] == -1) { directionint2[i] = 3; //NE } if (x2signs[i] == -1 && y2signs[i] == -1) { directionint2[i] = 4; //NW } if ((dest3x1-agent[i].x > 0 && type2[i] == 0) || (dest3x2-agent[i].x > 0 && type2[i] == 1) || (dest3x3-agent[i].x > 0 && type2[i] == 2)) { x3signs[i] = 1; } else {x3signs[i] = -1;} if ((dest3y1-agent[i].y > 0 && type2[i] == 0) || (dest3y2-agent[i].y > 0 && type2[i] == 1) || (dest3y3-agent[i].y > 0 && type2[i] == 2)) { y3signs[i] = 1; } else {y3signs[i] = -1;} if (x3signs[i] == 1 && y3signs[i] == 1) { directionint3[i] = 1; //SE } if (x3signs[i] == -1 && y3signs[i] == 1) { directionint3[i] = 2; //SW } if (x3signs[i] == 1 && y3signs[i] == -1) { directionint3[i] = 3; //NE } if (x3signs[i] == -1 && y3signs[i] == -1) { directionint3[i] = 4; //NW } if (calcchoice == 0) { switch (directionint1[i]) { case 1: { xstep[i] = (nearestNeighborSE(i)) / 10 + (10/(mass[i] + 5)); ystep[i] = (nearestNeighborSE(i)) / 10 + (10/(mass[i] + 5)); break; } case 2: { xstep[i] = (nearestNeighborSW(i)) / 10 + (10/(mass[i] + 5)); ystep[i] = (nearestNeighborSW(i)) / 10 + (10/(mass[i] + 5)); break; } case 3: { xstep[i] = (nearestNeighborNE(i)) / 10 + (10/(mass[i] + 5)); ystep[i] = (nearestNeighborNE(i)) / 10 + (10/(mass[i] + 5)); break; } case 4: { xstep[i] = (nearestNeighborNW(i)) / 10 + (10/(mass[i] + 5)); ystep[i] = (nearestNeighborNW(i)) / 10 + (10/(mass[i] + 5)); break; } ; } switch (directionint2[i]) { case 1: { xstep2[i] = (nearestNeighborSE(i)) / 10 + (10/(mass[i] + 5)); ystep2[i] = (nearestNeighborSE(i)) / 10 + (10/(mass[i] + 5)); break; } case 2: { xstep2[i] = (nearestNeighborSW(i)) / 10 + (10/(mass[i] + 5)); ystep2[i] = (nearestNeighborSW(i)) / 10 + (10/(mass[i] + 5)); break; } case 3: { xstep2[i] = (nearestNeighborNE(i)) / 10 + (10/(mass[i] + 5)); ystep2[i] = (nearestNeighborNE(i)) / 10 + (10/(mass[i] + 5)); break; } case 4: { xstep2[i] = (nearestNeighborNW(i)) / 10 + (10/(mass[i] + 5)); ystep2[i] = (nearestNeighborNW(i)) / 10 + (10/(mass[i] + 5)); break; }; } switch (directionint3[i]) { case 1: { xstep3[i] = (nearestNeighborSE(i)) / 10 + (10/(mass[i] + 5)); ystep3[i] = (nearestNeighborSE(i)) / 10 + (10/(mass[i] + 5)); break; } case 2: { xstep3[i] = (nearestNeighborSW(i)) / 10 + (10/(mass[i] + 5)); ystep3[i] = (nearestNeighborSW(i)) / 10 + (10/(mass[i] + 5)); break; } case 3: { xstep3[i] = (nearestNeighborNE(i)) / 10 + (10/(mass[i] + 5)); ystep3[i] = (nearestNeighborNE(i)) / 10 + (10/(mass[i] + 5)); break; } case 4: { xstep3[i] = (nearestNeighborNW(i)) / 10 + (10/(mass[i] + 5)); ystep3[i] = (nearestNeighborNW(i)) / 10 + (10/(mass[i] + 5)); break; }; } } if (calcchoice == 1) { switch (directionint1[i]) { case 1: { xstep[i] = log(nearestNeighborSE(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep[i] = log(nearestNeighborSE(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } case 2: { xstep[i] = log(nearestNeighborSW(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep[i] = log(nearestNeighborSW(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } case 3: { xstep[i] = log(nearestNeighborNE(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep[i] = log(nearestNeighborNE(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } case 4: { xstep[i] = log(nearestNeighborNW(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep[i] = log(nearestNeighborNW(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } ; } switch (directionint2[i]) { case 1: { xstep2[i] = log(nearestNeighborSE(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep2[i] = log(nearestNeighborSE(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } case 2: { xstep2[i] = log(nearestNeighborSW(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep2[i] = log(nearestNeighborSW(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } case 3: { xstep2[i] = log(nearestNeighborNE(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep2[i] = log(nearestNeighborNE(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } case 4: { xstep2[i] = log(nearestNeighborNW(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep2[i] = log(nearestNeighborNW(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; }; } switch (directionint3[i]) { case 1: { xstep3[i] = log(nearestNeighborSE(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep3[i] = log(nearestNeighborSE(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } case 2: { xstep3[i] = log(nearestNeighborSW(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep3[i] = log(nearestNeighborSW(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } case 3: { xstep3[i] = log(nearestNeighborNE(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep3[i] = log(nearestNeighborNE(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; } case 4: { xstep3[i] = log(nearestNeighborNW(i) + 1) * 1.5 + (10/(mass[i] + 5)); ystep3[i] = log(nearestNeighborNW(i) + 1) * 1.5 + (10/(mass[i] + 5)); break; }; } } if (xstep[i] > speedlimitx) { xstep[i] = speedlimitx; } if (ystep[i] > speedlimity) { ystep[i] = speedlimity; } if (xstep2[i] > speedlimitx) { xstep2[i] = speedlimitx; } if (ystep2[i] > speedlimity) { ystep2[i] = speedlimity; } if (xstep3[i] > speedlimitx) { xstep3[i] = speedlimitx; } if (ystep3[i] > speedlimity) { ystep3[i] = speedlimity; } xtime[i] = distx1[i] / abs(xstep[i]) - (deadline1 - loopediterations); ytime[i] = disty1[i] / abs(ystep[i]) - (deadline1 - loopediterations); xtime2[i] = distx2[i] / abs(xstep2[i]) - (deadline2 - loopediterations); ytime2[i] = disty2[i] / abs(ystep2[i]) - (deadline2 - loopediterations); xtime3[i] = distx3[i] / abs(xstep3[i]) - (deadline3 - loopediterations); ytime3[i] = disty3[i] / abs(ystep3[i]) - (deadline3 - loopediterations); if (loopediterations < deadline1) { Form1->EditVelocity->Text = sqrt(xstep[chosenAgent]*xstep[chosenAgent] + ystep[chosenAgent]*ystep[chosenAgent]); } if (loopediterations >= deadline1 && loopediterations < deadline2) { Form1->EditVelocity->Text = sqrt(xstep2[chosenAgent]*xstep2[chosenAgent] + ystep2[chosenAgent]*ystep2[chosenAgent]); } if (loopediterations >= deadline2) { Form1->EditVelocity->Text = sqrt(xstep3[chosenAgent]*xstep3[chosenAgent] + ystep3[chosenAgent]*ystep3[chosenAgent]); } Form1->EditMass->Text = mass[chosenAgent]; Form1->EditNW->Text = nearestNeighborNW(chosenAgent); Form1->EditNE->Text = nearestNeighborNE(chosenAgent); Form1->EditSW->Text = nearestNeighborSW(chosenAgent); Form1->EditSE->Text = nearestNeighborSE(chosenAgent); if (agent[i].x < dest1x && loopediterations <= deadline1 && xtime[i] >= planahead) { agent[i].x +=xstep[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].x > dest1x && loopediterations <= deadline1 && xtime[i] >= planahead ) { agent[i].x -=xstep[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y < dest1y && loopediterations <= deadline1 && ytime[i] >= planahead ) { agent[i].y +=ystep[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (agent[i].y > dest1y && loopediterations <= deadline1 && ytime[i] >= planahead) { agent[i].y -=ystep[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } //second destination if (agent[i].x > dest2x1 && loopediterations > deadline1 && loopediterations <= deadline2 && type[i] == 0 && xtime2[i] >= planahead ) { agent[i].x -=xstep2[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y > dest2y1 && loopediterations > deadline1 && loopediterations <= deadline2 && type[i] == 0 && ytime2[i] >= planahead ) { agent[i].y -=ystep2[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (agent[i].x < dest2x1 && loopediterations > deadline1 && loopediterations <= deadline2 && type[i] == 0 && xtime2[i] >= planahead ) { agent[i].x +=xstep2[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y < dest2y1 && loopediterations > deadline1 && loopediterations <= deadline2 && type[i] == 0 && ytime2[i] >= planahead ) { agent[i].y +=ystep2[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (agent[i].x < dest2x2 && loopediterations > deadline1 && loopediterations <= deadline2 && type[i] == 1 && xtime2[i] >= planahead ) { agent[i].x +=xstep2[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y > dest2y2 && loopediterations > deadline1 && loopediterations <= deadline2 && type[i] == 1 && ytime2[i] >= planahead ) { agent[i].y -=ystep2[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (agent[i].x > dest2x2 && loopediterations > deadline1 && loopediterations <= deadline2 && type[i] == 1 && xtime2[i] >= planahead ) { agent[i].x -=xstep2[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y < dest2y2 && loopediterations > deadline1 && loopediterations <= deadline2 && type[i] == 1 && ytime2[i] >= planahead ) { agent[i].y +=ystep2[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } // third destination if (agent[i].x > dest3x1 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 0 && xtime3[i] >= planahead ) { agent[i].x -=xstep3[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y > dest3y1 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 0 && ytime3[i] >= planahead ) { agent[i].y -=ystep3[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (agent[i].x < dest3x1 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 0 && xtime3[i] >= planahead ) { agent[i].x +=xstep3[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y < dest3y1 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 0 && ytime3[i] >= planahead ) { agent[i].y +=ystep3[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (agent[i].x < dest3x2 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 1 && xtime3[i] >= planahead ) { agent[i].x +=xstep3[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y > dest3y2 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 1 && ytime3[i] >= planahead ) { agent[i].y -=ystep3[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (agent[i].x > dest3x2 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 1 && xtime3[i] >= planahead ) { agent[i].x -=xstep3[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y < dest3y2 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 1 && ytime3[i] >= planahead ) { agent[i].y +=ystep3[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (agent[i].x < dest3x3 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 2 && xtime3[i] >= planahead ) { agent[i].x +=xstep3[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y > dest3y3 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 2 && ytime3[i] >= planahead ) { agent[i].y -=ystep3[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (agent[i].x > dest3x3 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 2 && xtime3[i] >= planahead ) { agent[i].x -=xstep3[i]; shape[i]->Left = agent[i].x - shape[i]->Width / 2; } if (agent[i].y < dest3y3 && loopediterations > deadline2 && loopediterations <= deadline3 && type2[i] == 2 && ytime3[i] >= planahead ) { agent[i].y +=ystep3[i]; shape[i]->Top = agent[i].y - shape[i]->Height / 2; } if (loopediterations == deadline1) { if (agent[i].x < dest1x + destradius && agent[i].x > dest1x - destradius && agent[i].y < dest1y + destradius && agent[i].y > dest1y - destradius) { madedeadline1++; Form1->EditDL1->Text = madedeadline1; } else { late[i]++; Form1->EditLate->Text = late[i]; if (mass[i] == 1) { counter1++; Form1->Edit1->Text = 1 - counter1/(masscount[0]*iterations/deadline1); } if (mass[i] == 2) { counter2++; Form1->Edit2->Text = 1 - counter2/(masscount[1]*iterations/deadline1); } if (mass[i] == 3) { counter3++; Form1->Edit3->Text = 1 - counter3/(masscount[2]*iterations/deadline1); } if (mass[i] == 4) { counter4++; Form1->Edit4->Text = 1 - counter4/(masscount[3]*iterations/deadline1); } if (mass[i] == 5) { counter5++; Form1->Edit5->Text = 1 - counter5/(masscount[4]*iterations/deadline1); } } } if (loopediterations == deadline2) { if ((agent[i].x < dest2x1 + destradius && agent[i].x > dest2x1 - destradius && agent[i].y < dest2y1 + destradius && agent[i].y > dest2y1 - destradius && type[i] == 0) || (agent[i].x < dest2x2 + destradius && agent[i].x > dest2x2 - destradius && agent[i].y < dest2y2 + destradius && agent[i].y > dest2y2 - destradius && type[i] == 1)) { madedeadline2++; Form1->EditDL2->Text = madedeadline2; } else { late[i]++; Form1->EditLate->Text = late[i]; if (mass[i] == 1) { counter1++; Form1->Edit1->Text =1 - counter1/(masscount[0]*iterations/deadline1); } if (mass[i] == 2) { counter2++; Form1->Edit2->Text =1 - counter2/(masscount[1]*iterations/deadline1); } if (mass[i] == 3) { counter3++; Form1->Edit3->Text = 1 - counter3/(masscount[2]*iterations/deadline1); } if (mass[i] == 4) { counter4++; Form1->Edit4->Text = 1 - counter4/(masscount[3]*iterations/deadline1); } if (mass[i] == 5) { counter5++; Form1->Edit5->Text = 1 - counter5/(masscount[4]*iterations/deadline1); } } } if (loopediterations == deadline3) { if ((agent[i].x < dest3x1 + destradius && agent[i].x > dest3x1 - destradius && agent[i].y < dest3y1 + destradius && agent[i].y > dest3y1 - destradius && type2[i] == 0) || (agent[i].x < dest3x2 + destradius && agent[i].x > dest3x2 - destradius && agent[i].y < dest3y2 + destradius && agent[i].y > dest3y2 - destradius && type2[i] == 1) || (agent[i].x < dest3x3 + destradius && agent[i].x > dest3x3 - destradius && agent[i].y < dest3y3 + destradius && agent[i].y > dest3y3 - destradius && type2[i] == 2)) { madedeadline3++; Form1->EditDL3->Text = madedeadline3; } else { late[i]++; Form1->EditLate->Text = late[i]; if (mass[i] == 1) { counter1++; Form1->Edit1->Text =1 - counter1/(masscount[0]*iterations/deadline1); } if (mass[i] == 2) { counter2++; Form1->Edit2->Text =1 - counter2/(masscount[1]*iterations/deadline1); } if (mass[i] == 3) { counter3++; Form1->Edit3->Text =1 - counter3/(masscount[2]*iterations/deadline1); } if (mass[i] == 4) { counter4++; Form1->Edit4->Text =1 - counter4/(masscount[3]*iterations/deadline1); } if (mass[i] == 5) { counter5++; Form1->Edit5->Text =1 - counter5/(masscount[4]*iterations/deadline1); } } } } } //---------------------------------------------------------------------- Run void run (void) { stop = false; message.data[0] = 0xC0; message.data[1] = 6; message.data[2] = 100; message.data[3] = 0; while (stop == false) { step(); Sleep(50); Application->ProcessMessages(); } } //=========================================================================== // EVENT HANDLERS //=========================================================================== //------------------------------------------------------------ On Form Create void __fastcall TForm1::FormCreate(TObject *Sender) { randomize(); for (i = 0; i < 24; i++) { mass[i] = random(5) + 1; late[i] = 0; } for (i = 0; i < 24; i++) { if (mass[i] == 1) { masscount[0]++; } if (mass[i] == 2) { masscount[1]++; } if (mass[i] == 3) { masscount[2]++; } if (mass[i] == 4) { masscount[3]++; } if (mass[i] == 5) { masscount[4]++; } } // defines an array of shapes with properties and events for (int i = 0; i < 24; i++) { shape[i] = new TShape(this); shape[i]->Parent = Form1; shape[i]->Visible = true; shape[i]->OnMouseDown = ShapeMouseDown; shape[i]->OnMouseUp = ShapeMouseUp; shape[i]->OnMouseMove = ShapeMouseMove; shape[i]->Height = 10; shape[i]->Width = 10; shape[i]->Left = 0; shape[i]->Top = 0; shape[i]->Shape = stCircle; shape[i]->Tag = i; shape[i]->Pen->Width = 2; shape[i]->Pen->Color = static_cast(colorRamp(25, 24 - shape[i]->Tag)); shape[i]->Brush->Color = static_cast(colorRamp(25, shape[i]->Tag)); } shuffle(); //circle(); } //------------------------------------------------------- On Shape 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::ShapeMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { TShape *shape = dynamic_cast(Sender); // The following will be used for mouseMove and mouseUp events... // Captures the index number of the shape that was clicked chosenAgent = shape->Tag; // remembers is we were stopped or running wasStopped = stop; stop = true; if (Button == 0) { // Begin drag object // Remembers that an shape was chosen for mouseMove and mouseUp agentWasJustChosen = true; // Remembers where on the shape the mouse was downed shapeDownX = X; shapeDownY = Y; } else { // Show nearest neighbor Form1->Canvas->MoveTo(agent[chosenAgent].x, agent[chosenAgent].y); Form1->Canvas->LineTo(agent[nearestNeighbor(chosenAgent)].x, agent[nearestNeighbor(chosenAgent)].y); } } //------------------------------------------------------- On Shape 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::ShapeMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { TShape *shape = dynamic_cast(Sender); chosenAgent = shape->Tag; if (agentWasJustChosen) { // this drags the shape along... shape->Left = shape->Left + X - shapeDownX; shape->Top = shape->Top + Y - shapeDownY; agent[chosenAgent].x = shape->Left + shape->Width / 2;; agent[chosenAgent].y = shape->Top + shape->Height / 2;; } // When the mouse moves over an shape its values are displayed EditAgent->Text = shape->Tag; EditX->Text = int(agent[chosenAgent].x); EditY->Text = int(agent[chosenAgent].y); EditMass->Text = mass[chosenAgent]; EditNW->Text = nearestNeighborNW(chosenAgent); EditNE->Text = nearestNeighborNE(chosenAgent); EditSW->Text = nearestNeighborSW(chosenAgent); EditSE->Text = nearestNeighborSE(chosenAgent); EditLate->Text = late[chosenAgent]; } //--------------------------------------------------------- On Shape 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::ShapeMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { TShape *shape = dynamic_cast(Sender); // this is the end of the drag... agentWasJustChosen = false; EditAgent->Text = shape->Tag; // When the mouse moves over an shape its values are displayed EditX->Text = int(agent[chosenAgent].x); EditY->Text = int(agent[chosenAgent].y); EditVelocity->Text = agent[shape->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(); } //------------------------------------------------------------- circle button void __fastcall TForm1::ButtonCircleClick(TObject *Sender) { circle(); } //------------------------------------------------------ size change trackBar void __fastcall TForm1::TrackBarSizeChange(TObject *Sender) { for (int i = 0; i < 24; i++) { } } //----------------------------------------------------- sound type RadioGroup void __fastcall TForm1::RadioGroupDrivingBehaviorClick(TObject *Sender) { reset(); circle(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if (which == 1) { Form1->LabelOne->Left = X; Form1->LabelOne->Top = Y; dest1x = X; dest1y = Y; } if (which == 2) { Form1->LabelTwo1->Left = X; Form1->LabelTwo1->Top = Y; dest2x1 = X; dest2y1 = Y; } if (which == 3) { Form1->LabelTwo2->Left = X; Form1->LabelTwo2->Top = Y; dest2x2 = X; dest2y2 = Y; } if (which == 4) { Form1->LabelThree1->Left = X; Form1->LabelThree1->Top = Y; dest3x1 = X; dest3y1 = Y; } if (which == 5) { Form1->LabelThree2->Left = X; Form1->LabelThree2->Top = Y; dest3x2 = X; dest3y2 = Y; } if (which == 6) { Form1->LabelThree3->Left = X; Form1->LabelThree3->Top = Y; dest3x3 = X; dest3y3 = Y; } } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar1Change(TObject *Sender) { planahead = Form1->TrackBar1->Position / -.5; } //--------------------------------------------------------------------------- void __fastcall TForm1::LabelTwo1EndDrag(TObject *Sender, TObject *Target, int X, int Y) { dest2x1 = X; dest2y1 = Y; Form1->LabelTwo1->Left = X; Form1->LabelTwo1->Top = Y; } //--------------------------------------------------------------------------- void __fastcall TForm1::RadioGroup1Click(TObject *Sender) { switch (RadioGroup1->ItemIndex) { case 0: { which = 1; break; } case 1: { which = 2; break; } case 2: { which = 3; break; } case 3: { which = 4; break; } case 4: { which = 5; break; } case 5: { which = 6; break; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackbarRadiusChange(TObject *Sender) { destradius = Form1->TrackbarRadius->Position; } //--------------------------------------------------------------------------- void __fastcall TForm1::ComboBox1Change(TObject *Sender) { deadline1 = (Form1->ComboBox1->ItemIndex + 1) * 50; deadline2 = (Form1->ComboBox1->ItemIndex + 1) * 100; deadline3 = (Form1->ComboBox1->ItemIndex + 1) * 150; } //--------------------------------------------------------------------------- void __fastcall TForm1::ComboBox2Change(TObject *Sender) { calcchoice = Form1->ComboBox2->ItemIndex; } //---------------------------------------------------------------------------