/// <summary> /// Get the coordinates to draw the traffic lights /// </summary> /// <param name="lane">Traffic Lane</param> /// <param name="state">Current state of the traffic light</param> /// <returns>Point with the correct coordinates</returns> private Point getCoordinatesForTrafficLight(TrafficLane l, bool state) { Crossing c = l.Parent; bool isVertical = false; int x = 0; int y = 0; if (c is Crossing_A) { x = LIGHT_STRUCT_X_SPOTS_CROSSING_A[l.ID - 4] + 1; y = LIGHT_STRUCT_Y_SPOTS_CROSSING_A[l.ID - 4] + 1; switch (l.ID) { case 4: case 5: case 8: case 9: isVertical = true; break; default: isVertical = false; break; } } else { x = LIGHT_STRUCT_X_SPOTS_CROSSING_B[l.ID - 4] + 1; y = LIGHT_STRUCT_Y_SPOTS_CROSSING_B[l.ID - 4] + 1; switch (l.ID) { case 4: case 7: isVertical = true; break; default: isVertical = false; break; } } if (state) { if (isVertical) { y += TRAFFIC_LIGHT_BOX_HEIGHT * 2; } else { x += TRAFFIC_LIGHT_BOX_WIDTH * 2; } } return(new Point(x, y)); }
/// <summary> /// Adds a crossing to the list of crossing /// </summary> /// <param name="Crossing"></param> /// <returns></returns> public bool AddCrossing(Crossing Crossing) { if (Crossings.Count < 12) { Crossings.Add(Crossing); Saved = false; return(true); } MessageBox.Show("Remove a crossing to be able to add another one. You can have only 12 crossings at the same time.", "Crossing limit reached"); return(false); }
/// <summary> /// Gets the properties of the individual crossing /// </summary> /// <param name="id"></param> /// <param name="nCars"></param> /// <param name="time"></param> /// <param name="style"></param> public void getProperties(int id, ref int nCars, ref int time, ref string style) { Crossing cr = Crossings.Find(x => x.CrossingId == (id)); nCars = cr.NumCars; time = cr.Time.Seconds; if (cr.GetType() == typeof(Crossing_B)) { Crossing_B cr1 = (Crossing_B)cr; //nPed = cr1.NumPeds; style = cr1.style; } }
/// <summary> /// Constructor of the traffic lane /// </summary> /// <param name="iD"></param> /// <param name="toFromCross"></param> /// <param name="direction"></param> /// <param name="trafficLight"></param> /// <param name="connLanes"></param> /// <param name="parent"></param> public TrafficLane(int iD, bool toFromCross, Direction direction, Light trafficLight, List <TrafficLane> connLanes, Crossing parent) : base(iD) { this.ID = iD; this.ToFromCross = toFromCross; this.Direction = direction; this.Lanes = connLanes; this.parent = parent; this.TrafficLight = trafficLight; Cars = new List <Car>(); this.initPoints(); }
/// <summary> /// Edits the properties of the crossing /// eg. number of cars for that crossing, time the light is green, etc. /// </summary> /// <param name="id"></param> /// <param name="numCars"></param> /// <param name="time"></param> /// <param name="style"></param> public void EditCrossing(int id, int numCars, int time, string style) { Crossing cr = Crossings.Find(x => x.CrossingId == (id)); if (cr.GetType() == typeof(Crossing_A)) { cr.NumCars = numCars; cr.Time = new TimeSpan(0, 0, time); } else { Crossing_B cr1 = (Crossing_B)cr; cr1.NumCars = numCars; cr1.Time = new TimeSpan(0, 0, time); cr1.style = style; cr1.CreatePedestrians(); } }
private void CrossingType(Crossing crossing) { string sPattern = @"[0-9]+"; foreach (PictureBox pic in ControlList) { Match m = Regex.Match(pic.Name, sPattern); int number = Convert.ToInt32(m.Value); if (crossing.CrossingId == number) { if (crossing.GetType() == typeof(Crossing_A)) { pic.Image = ProCP.Properties.Resources.Crossing_a; } else if (crossing.GetType() == typeof(Crossing_B)) { pic.Image = ProCP.Properties.Resources.Crossing_b; } } } }
/// <summary> /// Constructor of the traffic lane /// </summary> /// <param name="iD"></param> /// <param name="toFromCross"></param> /// <param name="direction"></param> /// <param name="trafficLight"></param> /// <param name="connLanes"></param> /// <param name="parent"></param> public TrafficLane(int iD, bool toFromCross, Direction direction, Light trafficLight, List<TrafficLane> connLanes, Crossing parent) : base(iD) { this.ID = iD; this.ToFromCross = toFromCross; this.Direction = direction; this.Lanes = connLanes; this.parent = parent; this.TrafficLight = trafficLight; Cars = new List<Car>(); this.initPoints(); }
/// <summary> /// the tick event for the timer thread /// </summary> /// <param name="sender"></param> /// <param name="args"></param> /// <param name="c"></param> private void TickEvent(object sender, EventArgs args, Crossing c) { SwitchAll(c); }
/// <summary> /// changes the lights to the next iteration /// </summary> /// <param name="c"></param> public void SwitchAll(Crossing c) { TotalNumberofSwitches++; if (c.GetType() == typeof(Crossing_A)) { /* #region A Crossing * * foreach (TrafficLane l in c.Lanes) * { * if (l.TrafficLight != null) * { * if (l.Direction == (Direction)(c.Turn - 1)) * { * l.TrafficLight.State = true; * } * else * { * l.TrafficLight.State = false; * } * } * c.Turn = ((c.Turn + 1) % 4); * * if (c.Turn == 0) * c.Turn = 1; * } * #endregion */ #region A Crossing switch (c.Turn) { case 1: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.NORTH) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn++; break; case 2: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.EAST) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn++; break; case 3: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.SOUTH) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn++; break; case 4: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.WEST) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn = 1; break; } #endregion } else { #region B Crossing Crossing_B cb = (Crossing_B)c; switch (c.Turn) { case 1: cb.ATimer.Interval = 1000 * cb.Time.Seconds; foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.NORTH || l.Direction == Direction.SOUTH) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } foreach (PedestrianLane pl in cb.pLanes) { if (pl.PLight != null) { pl.PLight.State = false; } } c.Turn++; break; case 2: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.EAST) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn++; break; case 3: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.WEST) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } bool sensor = false; cb.CreatePedestrians(); foreach (PedestrianLane pl in cb.pLanes) { if (pl.PLight.Sensor) { sensor = true; } } if (sensor) { c.Turn++; } else { c.Turn = 1; } break; case 4: if (cb.style == "Quiet") { cb.ATimer.Interval = (int)(1000 * (double)cb.Time.Seconds * 0.25); } else { cb.ATimer.Interval = (int)(1000 * (double)cb.Time.Seconds * 0.75); } foreach (PedestrianLane pl in cb.pLanes) { if (pl.PLight != null) { pl.PLight.State = true; } } foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { l.TrafficLight.State = false; } } c.Turn = 1; break; } #endregion } }
/// <summary> /// changes the lights to the next iteration /// </summary> /// <param name="c"></param> public void SwitchAll(Crossing c) { TotalNumberofSwitches++; if (c.GetType() == typeof(Crossing_A)) { /* #region A Crossing foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == (Direction)(c.Turn - 1)) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } c.Turn = ((c.Turn + 1) % 4); if (c.Turn == 0) c.Turn = 1; } #endregion */ #region A Crossing switch (c.Turn) { case 1: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.NORTH) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn++; break; case 2: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.EAST) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn++; break; case 3: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.SOUTH) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn++; break; case 4: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.WEST) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn = 1; break; } #endregion } else { #region B Crossing Crossing_B cb = (Crossing_B)c; switch (c.Turn) { case 1: cb.ATimer.Interval = 1000 * cb.Time.Seconds; foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.NORTH || l.Direction == Direction.SOUTH) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } foreach (PedestrianLane pl in cb.pLanes) { if (pl.PLight != null) { pl.PLight.State = false; } } c.Turn++; break; case 2: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.EAST) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } c.Turn++; break; case 3: foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { if (l.Direction == Direction.WEST) { l.TrafficLight.State = true; } else { l.TrafficLight.State = false; } } } bool sensor = false; cb.CreatePedestrians(); foreach (PedestrianLane pl in cb.pLanes) { if (pl.PLight.Sensor) { sensor = true; } } if (sensor) { c.Turn++; } else { c.Turn = 1; } break; case 4: if (cb.style == "Quiet") { cb.ATimer.Interval = (int)(1000* (double)cb.Time.Seconds * 0.25); } else { cb.ATimer.Interval = (int)(1000* (double)cb.Time.Seconds * 0.75); } foreach (PedestrianLane pl in cb.pLanes) { if (pl.PLight != null) { pl.PLight.State = true; } } foreach (TrafficLane l in c.Lanes) { if (l.TrafficLight != null) { l.TrafficLight.State = false; } } c.Turn = 1; break; } #endregion } }
/// <summary> /// Adds a crossing to the list of crossing /// </summary> /// <param name="Crossing"></param> /// <returns></returns> public bool AddCrossing(Crossing Crossing) { if (Crossings.Count < 12) { Crossings.Add(Crossing); Saved = false; return true; } MessageBox.Show("Remove a crossing to be able to add another one. You can have only 12 crossings at the same time.", "Crossing limit reached"); return false; }
//public void enableNum() //{ // if (isLocked && !eraseFlag) // { // if (surrounded) // { // numericCars.Enabled = false; // } // if (!surrounded) // { // numericCars.Enabled = true; // } // this.numericTrafficTime.Enabled = true; // //this.cBPedTraffic.Enabled = true; // this.btnFinishCrossing.Enabled = true; // this.btnRemove.Enabled = false; // } // if (isLocked && eraseFlag) // { // this.numericCars.Enabled = false; // this.numericTrafficTime.Enabled = false; // this.cBPedTraffic.Enabled = false; // this.btnFinishCrossing.Enabled = false; // this.btnRemove.Enabled = false; // selectedID = 0; // } // if (!eraseFlag && !isLocked) // { // this.numericCars.Enabled = false; // this.numericTrafficTime.Enabled = false; // this.cBPedTraffic.Enabled = false; // this.btnFinishCrossing.Enabled = false; // this.btnRemove.Enabled = true; // } // if (!isLocked && eraseFlag) // { // this.numericCars.Enabled = false; // this.numericTrafficTime.Enabled = false; // this.cBPedTraffic.Enabled = false; // this.btnFinishCrossing.Enabled = false; // this.btnRemove.Enabled = false; // selectedID = 0; // } // if (crossLocked) // { // this.numericCars.Enabled = false; // this.numericTrafficTime.Enabled = false; // this.cBPedTraffic.Enabled = false; // } // if (isLocked) // { // btnPlay.Enabled = true; // } // if (!play) // { // btnLock.Enabled = true; // btnToggleLight.Enabled = false; // } // if (play) // { // btnLock.Enabled = false; // btnToggleLight.Enabled = true; // } //} #endregion private void pictureBoxOnPaint(object sender, PaintEventArgs e) { PictureBox self = (PictureBox)sender; Artist painter = new Artist(e); Crossing crossing = Simulation.getCrossing(GetNumberOfPicturebox(self)); if (null != selectedPicBox) { if (self.Equals(selectedPicBox)) { painter.paintSelectionOutline(); crossLocked = false; this.btnFinishCrossing.Text = "Lock Crossing"; //enableNum(); } if (eraseFlag) { eraseFlag = false; this.btnFinishCrossing.Text = "Unlock Crossing"; //enableNum(); self.Invalidate(); } } #region Drawing Stuff // Drawing cars and lights, yay List <Car> tempCars = new List <Car>(); foreach (Crossing item in Simulation.Crossings) { if (!item.Equals(crossing)) { continue; } if (item is Crossing_B) { foreach (PedestrianLane p in ((Crossing_B)item).pLanes) { if (p.PLight != null) { painter.drawPedestrianLight(p, p.PLight.State); if (p.PLight.State) { painter.drawPedestrians(p, ((Crossing_B)item).style); } } } } foreach (TrafficLane j in item.Lanes) { if (j.TrafficLight != null) { painter.drawTrafficLight(j, j.TrafficLight.State); } foreach (Car c in j.Cars) { painter.drawCar(c, j.Direction); } } } #endregion #region Debug // enable/disable in the beginning of the class if (this.cBDebugPoint.Checked) { // Dots debug int picBoxNum = GetNumberOfPicturebox(self); bool test = Simulation.CrossingExist(picBoxNum); if (Simulation.CrossingExist(picBoxNum)) { crossing = Simulation.getCrossing(picBoxNum); List <TrafficLane> lanes = new List <TrafficLane>(crossing.Lanes); foreach (TrafficLane t in lanes) { foreach (Point p in t.Points) { e.Graphics.DrawEllipse(new Pen(Color.Red, 1), p.X, p.Y, 1, 1); } } if (this.cBDebugCars.Checked) { if (crossing is Crossing_A) { // Car debug #region Vertical Crossings // Lane 4 e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 77, 1, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 77, 16, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 77, 31, 10, 13); // Lane 5 e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 104, 1, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 104, 16, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 104, 31, 10, 13); // Lane 0 e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 134, 1, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 134, 16, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 134, 31, 10, 13); // Lane 2 e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 77, 111, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 77, 126, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 77, 141, 10, 13); // Lane 9 e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 107, 111, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 107, 126, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 107, 141, 10, 13); // Lane 8 e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 134, 111, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 134, 126, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 134, 141, 10, 13); #endregion #region Horizontal Crossings // Lane 6 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 157, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 172, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 187, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 202, 54, 13, 10); // Lane 7 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 157, 74, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 172, 74, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 187, 74, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 202, 74, 13, 10); // Lane 1 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 157, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 172, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 187, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 202, 94, 13, 10); // Lane 3 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 5, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 20, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 35, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 50, 54, 13, 10); // Lane 11 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 5, 74, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 20, 74, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 35, 74, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 50, 74, 13, 10); // Lane 10 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 5, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 20, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 35, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 50, 94, 13, 10); #endregion } if (crossing is Crossing_B) { // Car debug #region Vertical Crossings // Lane 4 e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 85, 1, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 85, 16, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 85, 31, 10, 13); // Lane 0 e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 128, 1, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 128, 16, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 128, 31, 10, 13); // Lane 2 e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 85, 113, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 85, 128, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 85, 143, 10, 13); // Lane 7 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 128, 113, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 128, 128, 10, 13); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 128, 143, 10, 13); #endregion #region Horizontal Crossings // Lane 6 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 157, 73, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 172, 73, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 187, 73, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 202, 73, 13, 10); // Lane 8 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 5, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 20, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 35, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 50, 94, 13, 10); // Lane 5 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 157, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 172, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 187, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 202, 54, 13, 10); // Lane 9 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 5, 74, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 20, 74, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 35, 74, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 50, 74, 13, 10); // Lane 1 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 157, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 172, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 187, 94, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 202, 94, 13, 10); // Lane 3 e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 5, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Black, 1), 20, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), 35, 54, 13, 10); e.Graphics.DrawRectangle(new Pen(Color.Orange, 1), 50, 54, 13, 10); #endregion } } } } #endregion }