public void simulate(Graphics G) { double tempdistance; double bias; // 'for direction int side; //G->Clear(Color::Green); drawnetwork(G); for (int j = 0; j < cars.Count; j++) { if (simtime == global.LeaveForHomeTime) { plancarroute(j); } cars[j].move(G); for (int k = 0; k < cars.Count; k++) { //car breaking code if (k != j && cars[j].roadon == cars[k].roadon && cars[j].lane == cars[k].lane && cars[j].destination01 == cars[k].destination01) { tempdistance = utilities.distance(cars[j].location, cars[j].roadon.points[cars[j].destination01]) - utilities.distance(cars[k].location, cars[k].roadon.points[cars[k].destination01]); if ((tempdistance < (cars[j].breakdistance + global.CarLength * 2)) && (tempdistance >= 0)) { if (cars[j].lane > 0 && cars[j].ontrajectory == false && cars[j].changinglanes == false) { cars[j].lanechange(-1); } if (cars[j].changinglanes == false) { if (cars[j].mode != carmode.Breaking) { cars[j].distancecontroller.init(cars[j].breakdistance, tempdistance, cars[j].speed); } if ((tempdistance < global.CarLength * 2) && (cars[j].speed < 1.0 / 3600.0)) { cars[j].speed = 0; } else { cars[j].speed = cars[j].distancecontroller.update(cars[j].breakdistance + global.CarLength * 2, tempdistance); } //'If cars(j).speed < 0 Then cars(j).speed = 0 cars[j].mode = carmode.Breaking; } } else if (cars[j].mode != carmode.Normal) { cars[j].speedcontroller.init(cars[j].preferredspeed, cars[j].speed, cars[j].speed); cars[j].mode = carmode.Normal; } } } //traffic light handling code for (int k = 0; k < trafficlights.Count; k++) { if (trafficlights[k].roadon == cars[j].roadon) { if (cars[j].destination01 == trafficlights[k].pointnotpointedto) { tempdistance = utilities.distance(cars[j].location, cars[j].roadon.points[cars[j].destination01]) - utilities.distance(trafficlights[k].location, cars[j].roadon.points[cars[j].destination01]); if (tempdistance < (cars[j].breakdistance)) { if (trafficlights[k].colour != trafficlightcolour.Green) { if (cars[j].mode != carmode.Breaking) { cars[j].distancecontroller.init(cars[j].breakdistance, tempdistance, cars[j].speed); } if (tempdistance <= 0) { cars[j].speed = 0; } else { cars[j].speed = cars[j].distancecontroller.update(cars[j].breakdistance, tempdistance); } cars[j].mode = carmode.Breaking; } } if ((trafficlights[k].colour == trafficlightcolour.Green) && (cars[j].mode != carmode.Normal)) { cars[j].speedcontroller.init(cars[j].preferredspeed, cars[j].speed, cars[j].speed); cars[j].mode = carmode.Normal; } } } } //stop sign handling code for (int k = 0; k < stopsigns.Count; k++) { if (stopsigns[k].roadon == cars[j].roadon) { if (cars[j].destination01 == stopsigns[k].pointnotpointedto) { tempdistance = utilities.distance(cars[j].location, cars[j].roadon.points[cars[j].destination01]) - utilities.distance(stopsigns[k].location, cars[j].roadon.points[cars[j].destination01]); if (tempdistance < (cars[j].breakdistance)) { if (cars[j].mode != carmode.Breaking) { cars[j].distancecontroller.init(cars[j].breakdistance, tempdistance, cars[j].speed); } if (tempdistance <= 0) { cars[j].speed = 0; } else { cars[j].speed = cars[j].distancecontroller.update(cars[j].breakdistance, tempdistance); } cars[j].mode = carmode.Breaking; } if (safetodrive(j) && cars[j].mode != carmode.Normal) { cars[j].speedcontroller.init(cars[j].preferredspeed, cars[j].speed, cars[j].speed); cars[j].mode = carmode.Normal; } } } } //code executed once the car has reached its destination if (cars[j].atdestination) { cars[j].readlane(); if (cars[j].changinglanes) { cars[j].stoplanechange(); } else if (cars[j].ontrajectory == false) { if (cars[j].decisiont == decisiontype.BuildingToBuilding) { if (cars[j].currentstepinroute < 0) { cars[j].currentstepinroute++; calcmobilepointendofroad(cars[j].roadon, cars[j].destination01, cars[j].roadon.nlanes - 1); cars[j].newdestination(mobilepoint, cars[j].roadon, cars[j].destination01); } else if (cars[j].currentstepinroute < cars[j].route.Count - 1) { cars[j].currentstepinroute++; cars[j].newroadn = cars[j].route[cars[j].currentstepinroute]; } if (cars[j].currentstepinroute == cars[j].route.Count) { cars[j].currentstepinroute++; cars[j].newdestination(cars[j].ultimatedest, cars[j].roadon, cars[j].destination01); } } else { //calcnewroadn(j); //index of car sent. if (cars[j].cartobedeleted) { carstobedeleted = true; } } if (cars[j].decisiont != decisiontype.BuildingToBuilding || (cars[j].decisiont == decisiontype.BuildingToBuilding && cars[j].currentstepinroute <= cars[j].route.Count - 1 && cars[j].currentstepinroute > 0)) { calcmobilepointstartofroad(cars[j], roads[cars[j].newroadn], roads[cars[j].newroadn].nlanes - 1); if (cars[j].roadon.points[cars[j].destination01] == roads[cars[j].newroadn].points[0]) { cars[j].newdestination(mobilepoint, roads[cars[j].newroadn], 1); } else { cars[j].newdestination(mobilepoint, roads[cars[j].newroadn], 0); } cars[j].ontrajectory = true; } } else if (cars[j].ontrajectory == true) //there could be a bug here... was '=' in c++ { if (cars[j].decisiont == decisiontype.BuildingToBuilding && cars[j].currentstepinroute == cars[j].route.Count - 1) { cars[j].currentstepinroute++; mobilepoint.x = cars[j].penultimatedest.x; mobilepoint.y = cars[j].penultimatedest.y; } else { calcmobilepointendofroad(cars[j].roadon, cars[j].destination01, cars[j].roadon.nlanes - 1); } cars[j].newdestination(mobilepoint, cars[j].roadon, cars[j].destination01); cars[j].ontrajectory = false; calcnewroadn(j); //index of car sent. } } } //delete cars that are to be deleted if (carstobedeleted) { for (int j = 0; j < cars.Count; j++) { if (cars[j].cartobedeleted) { cars.RemoveAt(j); } } carstobedeleted = false; } //update trafficlights for (int j = 0; j < trafficlights.Count; j++) { trafficlights[j].update(); } //update carinjectors for (int j = 0; j < carinjectors.Count; j++) { /*car(double aspeed, double adt, point^ alocation, * point^ adestination, road^ aroadon, int adestination01, * double abreakacceleration);*/ carinjectors[j].update(); if (carinjectors[j].injectnow) { point startpoint = new point(0, 0); startpoint.copyfrom(carinjectors[j].carstartlocation); calcmobilepointendofroad(carinjectors[j].roadon, carinjectors[j].cardestination01, carinjectors[j].carlane); cars.Add(new car(global.CarSpeed + utilities.randnormv(global.CarSpeed3Sigma, r), global.SampleT, startpoint, mobilepoint, carinjectors[j].roadon, carinjectors[j].cardestination01, global.CarBreakAcceleration)); // sim.cars.Add(new car(global.CarSpeed + utilities.randnormv(global.CarSpeed3Sigma, sim.r), //global.SampleT, // new point(0, 0), sim.roads[0].points[1], sim.roads[0], 1, global.CarBreakAcceleration)); } } simtime.tick(); }
public void newdestinationlane(point adestination, road aroadon, int adestination01, int adestinationlane) { }
public static double distance(point p1, point p2) { return(Math.Sqrt(Math.Pow(p1.x - p2.x, 2) + Math.Pow(p1.y - p2.y, 2))); }