public StraightTrack(CheckPoint pStart, CheckPoint pEnd, float width) { this.pStart = pStart; this.pEnd = pEnd; this.cars = new ArrayList(); this.carsBuf = new ArrayList(); float x0, x1, y0, y1; x0 = pStart.x; y0 = pStart.y; x1 = pEnd.x; y1 = pEnd.y; this.length = (float)Math.Sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); this.minDistCoeff = 1; this.width = width; pStart.outTracks.Add(this); pEnd.inTracks.Add(this); }
static void ThreadProc(Object o) { //try { ThreadProcParams tpp = o as ThreadProcParams; Form1 f = tpp.form; float lambdaAudi, lambdaBus, lambdaTruck; lambdaAudi = tpp.lambdaAudi; lambdaBus = tpp.lambdaBus; lambdaTruck = tpp.lambdaTruck; int sleepTime = tpp.sleepTime; bool visualisation = tpp.visualisation; Bitmap b = new Bitmap(600, 600); Graphics g = Graphics.FromImage(b); f.pictureBox1.Image = b; float x = 175, y = 175, w = 30, xMax = 600, yMax = 600; float epsilon = 0.001f; // Контрольные точки // Дорога от площади Революции CheckPoint cpRevBackStart1 = new CheckPoint(x + 3.5f * w, 0); CheckPoint cpRevBackTrafLight1 = new CheckPoint(x + 3.5f * w, y + epsilon); CheckPoint cpRevForwStart1 = new CheckPoint(x + 4.5f * w, 0); CheckPoint cpRevForwTrafLight1 = new CheckPoint(x + 4.5f * w, y); CheckPoint cpRevForwStart2 = new CheckPoint(x + 5.5f * w, 0); CheckPoint cpRevForwTrafLight2 = new CheckPoint(x + 5.5f * w, y); CheckPoint cpRevForwStart3 = new CheckPoint(x + 6.5f * w, 0); CheckPoint cpRevForwTrafLight3 = new CheckPoint(x + 6.5f * w, y); CheckPoint cpRevForwMiddle = new CheckPoint(x + 4.5f * w, y + 3 * w); CheckPoint cpRevForwLeft1 = new CheckPoint(x + 2 * w + 2.5f * w * (float)Math.Cos(0.7f), y + 3 * w + 2.5f * w * (float)Math.Sin(0.7f)); CheckPoint cpRevForwLeft2 = new CheckPoint(x + 2 * w, y + 5.5f * w); // Дорога от стадиона CheckPoint cpStaForwStart1 = new CheckPoint(x + 3.5f * w, yMax); CheckPoint cpStaForwTrafLight1 = new CheckPoint(x + 3.5f * w, y + 10 * w); CheckPoint cpStaBackStart1 = new CheckPoint(x + 4.5f * w, yMax); CheckPoint cpStaBackTrafLight1 = new CheckPoint(x + 4.5f * w, y + 10 * w); CheckPoint cpStaBackStart2 = new CheckPoint(x + 5.5f * w, yMax); CheckPoint cpStaBackTrafLight2 = new CheckPoint(x + 5.5f * w, y + 10 * w); CheckPoint cpStaBackStart3 = new CheckPoint(x + 6.5f * w, yMax); CheckPoint cpStaBackTrafLight3 = new CheckPoint(x + 6.5f * w, y + 10 * w); // Дорога от Фрунзе CheckPoint cpFruBackStart1 = new CheckPoint(0, y + 6.5f * w); CheckPoint cpFruBackTrafLight1 = new CheckPoint(x, y + 6.5f * w); CheckPoint cpFruBackStart2 = new CheckPoint(0, y + 5.5f * w); CheckPoint cpFruBackTrafLight2 = new CheckPoint(x, y + 5.5f * w); CheckPoint cpFruForwStart1 = new CheckPoint(0, y + 4.5f * w); CheckPoint cpFruForwTrafLight1 = new CheckPoint(x, y + 4.5f * w); CheckPoint cpFruForwStart2 = new CheckPoint(0, y + 3.5f * w); CheckPoint cpFruForwTrafLight2 = new CheckPoint(x, y + 3.5f * w); CheckPoint cpFruForwMiddle = new CheckPoint(x + 2 * w, y + 3 * w); // Дорога от театра CheckPoint cpTheForwStart1 = new CheckPoint(xMax, y + 7.5f * w); CheckPoint cpTheForwTrafLight1 = new CheckPoint(x + 9 * w, y + 7.5f * w); CheckPoint cpTheForwStart2 = new CheckPoint(xMax, y + 6.5f * w); CheckPoint cpTheForwTrafLight2 = new CheckPoint(x + 9 * w, y + 6.5f * w); CheckPoint cpTheForwStart3 = new CheckPoint(xMax, y + 5.5f * w); CheckPoint cpTheForwTrafLight3 = new CheckPoint(x + 9 * w, y + 5.5f * w); CheckPoint cpTheBackStart1 = new CheckPoint(xMax, y + 4.5f * w); CheckPoint cpTheBackTrafLight1 = new CheckPoint(x + 9 * w, y + 4.5f * w); CheckPoint cpTheBackStart2 = new CheckPoint(xMax, y + 3.5f * w); CheckPoint cpTheBackTrafLight2 = new CheckPoint(x + 9 * w, y + 3.5f * w); CheckPoint cpTheBackStart3 = new CheckPoint(xMax, y + 2.5f * w); CheckPoint cpTheBackTrafLight3 = new CheckPoint(x + 9 * w, y + 2.5f * w); // Точки для поворота // Площадь Революции - театр float sqrt2 = (float)Math.Sqrt(2); PointF pRevThe2 = new PointF(x + 9 * w - 3.5f * w / sqrt2, y + 3.5f * w / sqrt2); PointF pRevThe3 = new PointF(x + 9 * w - 2.5f * w / sqrt2, y + 2.5f * w / sqrt2); // Площадь Революции - Фрунзе PointF pRevFru1 = new PointF(x + 2 * w + 2.5f * w * (float)Math.Cos(0.1f), y + 3 * w + 2.5f * w * (float)Math.Sin(0.1f)); PointF pRevFru2 = new PointF(x + 2 * w + 2.5f * w / sqrt2, y + 3 * w + 2.5f * w / sqrt2); // Театр - стадион PointF pTheSta2 = new PointF(x + 9 * w - 3.5f * w / sqrt2, y + 10 * w - 3.5f * w / sqrt2); PointF pTheSta3 = new PointF(x + 9 * w - 2.5f * w / sqrt2, y + 10 * w - 2.5f * w / sqrt2); // Стадион - Фрунзе PointF pStaFru1 = new PointF(x + 3.5f * w / sqrt2, y + 10 * w - 3.5f * w / sqrt2); PointF pStaFru2 = new PointF(x + 4.5f * w / sqrt2, y + 10.1f * w - 4.5f * w / sqrt2); // Фрунзе - театр PointF pFruThe1 = new PointF(x + 1 * w, y + 3.3f * w); PointF pFruThe2 = new PointF(x + 4 * w, y + 2.6f * w); // Фрунзе - площадь Революции PointF pFruRev1 = new PointF(x + 3.5f * w / sqrt2, y + 3.5f * w / sqrt2); // Треки // Дорога от площади Революции float minDistCoeff1 = 0.8f, minDistCoeff2 = 0.7f; Track tRevSta11 = new StraightTrack(cpRevForwTrafLight1, cpRevForwMiddle, w); Track tRevSta12 = new StraightTrack(cpRevForwMiddle, cpStaBackTrafLight1, w); Track tRevSta2 = new StraightTrack(cpRevForwTrafLight2, cpStaBackTrafLight2, w); Track tRevSta3 = new StraightTrack(cpRevForwTrafLight3, cpStaBackTrafLight3, w); Track tRevThe2 = new ArchTrack(cpRevForwTrafLight2, cpTheBackTrafLight2, pRevThe2, minDistCoeff1, w); Track tRevThe3 = new ArchTrack(cpRevForwTrafLight3, cpTheBackTrafLight3, pRevThe3, minDistCoeff1, w); Track tRevFru21 = new ArchTrack(cpRevForwMiddle, cpRevForwLeft1, pRevFru1, minDistCoeff2, w); Track tRevFru22 = new ArchTrack(cpRevForwLeft1, cpRevForwLeft2, pRevFru2, minDistCoeff2, w); Track tRevFru23 = new StraightTrack(cpRevForwLeft2, cpFruBackTrafLight2, w); Track tRevBack1 = new StraightTrack(cpRevBackTrafLight1, cpRevBackStart1, w); Track tRevForw1 = new StraightTrack(cpRevForwStart1, cpRevForwTrafLight1, w); Track tRevForw2 = new StraightTrack(cpRevForwStart2, cpRevForwTrafLight2, w); Track tRevForw3 = new StraightTrack(cpRevForwStart3, cpRevForwTrafLight3, w); // Дорога от стадиона Track tStaRev1 = new StraightTrack(cpStaForwTrafLight1, cpRevBackTrafLight1, w); Track tStaFru1 = new ArchTrack(cpStaForwTrafLight1, cpFruBackTrafLight1, pStaFru1, minDistCoeff1, w); Track tStaFru2 = new ArchTrack(cpStaBackTrafLight2, cpFruBackTrafLight2, pStaFru2, minDistCoeff1, w); Track tStaForw1 = new StraightTrack(cpStaForwStart1, cpStaForwTrafLight1, w); Track tStaBack1 = new StraightTrack(cpStaBackTrafLight1, cpStaBackStart1, w); Track tStaBack2 = new StraightTrack(cpStaBackTrafLight2, cpStaBackStart2, w); Track tStaBack3 = new StraightTrack(cpStaBackTrafLight3, cpStaBackStart3, w); // Дорога от Фрунзе Track tFruThe1 = new StraightTrack(cpFruForwTrafLight1, cpTheBackTrafLight1, w); Track tFruThe2 = new StraightTrack(cpFruForwTrafLight2, cpTheBackTrafLight2, w); Track tFruThe31 = new ArchTrack(cpFruForwTrafLight2, cpFruForwMiddle, pFruThe1, minDistCoeff2, w); Track tFruThe32 = new ArchTrack(cpFruForwMiddle, cpTheBackTrafLight3, pFruThe2, minDistCoeff2, w); Track tFruRev1 = new ArchTrack(cpFruForwTrafLight2, cpRevBackTrafLight1, pFruRev1, minDistCoeff1, w); Track tFruBack1 = new StraightTrack(cpFruBackTrafLight1, cpFruBackStart1, w); Track tFruBack2 = new StraightTrack(cpFruBackTrafLight2, cpFruBackStart2, w); Track tFruForw1 = new StraightTrack(cpFruForwStart1, cpFruForwTrafLight1, w); Track tFruForw2 = new StraightTrack(cpFruForwStart2, cpFruForwTrafLight2, w); // Дорога от театра Track tTheFru2 = new StraightTrack(cpTheForwTrafLight2, cpFruBackTrafLight1, w); Track tTheFru3 = new StraightTrack(cpTheForwTrafLight3, cpFruBackTrafLight2, w); Track tTheSta1 = new ArchTrack(cpTheForwTrafLight1, cpStaBackTrafLight3, pTheSta3, minDistCoeff1, w); Track tTheSta2 = new ArchTrack(cpTheForwTrafLight2, cpStaBackTrafLight2, pTheSta2, minDistCoeff1, w); Track tTheForw1 = new StraightTrack(cpTheForwStart1, cpTheForwTrafLight1, w); Track tTheForw2 = new StraightTrack(cpTheForwStart2, cpTheForwTrafLight2, w); Track tTheForw3 = new StraightTrack(cpTheForwStart3, cpTheForwTrafLight3, w); Track tTheBack1 = new StraightTrack(cpTheBackTrafLight1, cpTheBackStart1, w); Track tTheBack2 = new StraightTrack(cpTheBackTrafLight2, cpTheBackStart2, w); Track tTheBack3 = new StraightTrack(cpTheBackTrafLight3, cpTheBackStart3, w); // Имена треков tRevForw1.name = "Первая полоса (пл. Революции)"; tRevForw2.name = "Вторая полоса (пл. Революции)"; tRevForw3.name = "Третья полоса (пл. Революции)"; tStaForw1.name = "Первая полоса (стадион)"; tFruForw1.name = "Первая полоса (ул. Фрунзе)"; tFruForw2.name = "Вторая полоса (ул. Фрунзе)"; tTheForw1.name = "Первая полоса (театр)"; tTheForw2.name = "Вторая полоса (театр)"; tTheForw3.name = "Третья полоса (театр)"; // Машины Bitmap bmpAudi, bmpBus, bmpTruck; bmpAudi = Properties.Resources.AudiSmall; bmpBus = Properties.Resources.BusSmall; bmpTruck = Properties.Resources.TruckSmall; bmpAudi.MakeTransparent(Color.Magenta); bmpBus.MakeTransparent(Color.Magenta); bmpTruck.MakeTransparent(Color.Magenta); Car cAudi = new Car(bmpAudi, 4, 0.5f, -1.4f, 5.5f); Car cBus = new Car(bmpBus, 4, 0.4f, -1.3f, 5); Car cTruck = new Car(bmpTruck, 4, 0.3f, -1.2f, 4); // Дорога от площади Революции Path pathRev1Sta1 = new Path(new Track[] { tRevForw1, tRevSta11, tRevSta12, tStaBack1 }, new Generator[] { new ExpGenerator(lambdaAudi * 1.2f, cAudi, 5), new ExpGenerator(lambdaBus * 0.7f, cBus, 7), new ExpGenerator(lambdaTruck * 0.5f, cTruck, 6) }); Path pathRev1Fru2 = new Path(new Track[] { tRevForw1, tRevSta11, tRevFru21, tRevFru22, tRevFru23, tFruBack2 }, new Generator[] { new ExpGenerator(lambdaBus * 1.7f, cBus, 7) }); Path pathRev2Sta2 = new Path(new Track[] { tRevForw2, tRevSta2, tStaBack2 }, new Generator[] { new ExpGenerator(lambdaAudi * 1.0f, cAudi, 84), new ExpGenerator(lambdaBus * 0.8f, cBus, 63), new ExpGenerator(lambdaTruck * 1.1f, cTruck, 4) }); Path pathRev3Sta3 = new Path(new Track[] { tRevForw3, tRevSta3, tStaBack3 }, new Generator[] { }); Path pathRev2The2 = new Path(new Track[] { tRevForw2, tRevThe2, tTheBack2 }, new Generator[] { new ExpGenerator(lambdaAudi * 0.4f, cAudi, 52), new ExpGenerator(lambdaBus * 1.2f, cBus, 33), new ExpGenerator(lambdaTruck * 0.7f, cTruck, 26) }); Path pathRev3The3 = new Path(new Track[] { tRevForw3, tRevThe3, tTheBack3 }, new Generator[] { new ExpGenerator(lambdaAudi * 0.9f, cAudi, 22), new ExpGenerator(lambdaBus * 1.0f, cBus, 90), new ExpGenerator(lambdaTruck * 1.1f, cTruck, 5) }); // Дорога от стадиона Path pathSta1Rev1 = new Path(new Track[] { tStaForw1, tStaRev1, tRevBack1 }, new Generator[] { new ExpGenerator(lambdaBus * 1.2f, cBus, 190) }); Path pathSta1Fru1 = new Path(new Track[] { tStaForw1, tStaFru1, tFruBack1 }, new Generator[] { new ExpGenerator(lambdaBus * 0.2f, cBus, 190) }); Path pathSta2Fru2 = new Path(new Track[] { tStaFru2 }, new Generator[] { }); // Дорога от Фрунзе Path pathFru1The1 = new Path(new Track[] { tFruForw1, tFruThe1, tTheBack1 }, new Generator[] { new ExpGenerator(lambdaAudi * 0.8f, cAudi, 122), new ExpGenerator(lambdaBus * 1.1f, cBus, 44), new ExpGenerator(lambdaTruck * 0.5f, cTruck, 52) }); Path pathFru2The2 = new Path(new Track[] { tFruForw2, tFruThe2, tTheBack2 }, new Generator[] { new ExpGenerator(lambdaAudi * 1.2f, cAudi, 122), new ExpGenerator(lambdaBus * 0.1f, cBus, 40), new ExpGenerator(lambdaTruck * 0.6f, cTruck, 15) }); Path pathFru2The3 = new Path(new Track[] { tFruForw2, tFruThe31, tFruThe32, tTheBack3 }, new Generator[] { new ExpGenerator(lambdaBus * 1.0f, cBus, 40) }); Path pathFru2Rev1 = new Path(new Track[] { tFruForw2, tFruRev1, tRevBack1 }, new Generator[] { new ExpGenerator(lambdaBus * 0.1f, cBus, 40) }); // Дорога от театра Path pathThe1Sta3 = new Path(new Track[] { tTheForw1, tTheSta1, tStaBack3 }, new Generator[] { new ExpGenerator(lambdaAudi * 1.1f, cAudi, 72) }); Path pathThe2Fru1 = new Path(new Track[] { tTheForw2, tTheFru2, tFruBack1 }, new Generator[] { new ExpGenerator(lambdaAudi * 1.3f, cAudi, 62), new ExpGenerator(lambdaBus * 0.9f, cBus, 79), new ExpGenerator(lambdaTruck * 0.9f, cTruck, 55) }); Path pathThe3Fru2 = new Path(new Track[] { tTheForw3, tTheFru3, tFruBack2 }, new Generator[] { new ExpGenerator(lambdaAudi * 0.9f, cAudi, 222), new ExpGenerator(lambdaBus * 1.0f, cBus, 91), new ExpGenerator(lambdaTruck * 0.8f, cTruck, 35) }); Path pathThe2Sta2 = new Path(new Track[] { tTheForw2, tTheSta2, tStaBack2 }, new Generator[] { new ExpGenerator(lambdaAudi * 0.6f, cAudi, 72) }); Director d = new Director(new Path[] { pathRev1Sta1, pathRev1Fru2, pathRev2Sta2, pathRev2The2, pathRev3The3, pathRev3Sta3, pathSta1Rev1, pathSta1Fru1, pathSta2Fru2, pathFru1The1, pathFru2The2, pathFru2The3, pathFru2Rev1, pathThe1Sta3, pathThe2Sta2, pathThe2Fru1, pathThe3Fru2 }); g.TranslateTransform(0, b.Height); g.ScaleTransform(1, -1); float timeTrafLigRevSta = 200, timeTrafLigFruThe = 200, counterRevSta, counterFruThe; float distSafe = 6 * w; bool openedRevSta = false, openedFruThe = true; cpStaForwTrafLight1.status = cpRevForwTrafLight1.status = cpRevForwTrafLight2.status = cpRevForwTrafLight3.status = CheckPointStatus.Stop; counterRevSta = timeTrafLigRevSta; counterFruThe = timeTrafLigFruThe; f.pictureBox1.Invalidate(); Bitmap bGreen = Properties.Resources.Green; Bitmap bRed = Properties.Resources.Red; bGreen.MakeTransparent(Color.Magenta); bRed.MakeTransparent(Color.Magenta); g.Clear(Color.LightGray); d.DrawTracks(g); for (ulong t = 0; t < 1000000; t++) { d.DoIteration(1); if (visualisation) { g.Clear(Color.LightGray); d.DrawTracks(g); d.DrawCars(g); RectangleF destRectSta = new RectangleF(x + 2 * w, y + 10 * w, w / 2, w / 2); RectangleF destRectRev = new RectangleF(x + 7.5f * w, y, w / 2, w / 2); RectangleF destRectFru = new RectangleF(x, y + 2 * w, w / 2, w / 2); RectangleF destRectThe = new RectangleF(x + 9 * w, y + 8.5f * w, w / 2, w / 2); Rectangle sourceRect = new Rectangle(0, 0, bGreen.Width, bGreen.Height); if (openedFruThe) { g.DrawImage(bGreen, destRectSta, sourceRect, GraphicsUnit.Pixel); g.DrawImage(bGreen, destRectRev, sourceRect, GraphicsUnit.Pixel); g.DrawImage(bRed, destRectFru, sourceRect, GraphicsUnit.Pixel); g.DrawImage(bRed, destRectThe, sourceRect, GraphicsUnit.Pixel); } else { g.DrawImage(bRed, destRectSta, sourceRect, GraphicsUnit.Pixel); g.DrawImage(bRed, destRectRev, sourceRect, GraphicsUnit.Pixel); g.DrawImage(bGreen, destRectFru, sourceRect, GraphicsUnit.Pixel); g.DrawImage(bGreen, destRectThe, sourceRect, GraphicsUnit.Pixel); } f.pictureBox1.Invalidate(); } f.textBox1.Text = d.MakeStatistics(t); counterRevSta--; counterFruThe--; if (counterRevSta == 0) { counterRevSta = timeTrafLigRevSta; if (openedRevSta) { openedRevSta = false; } else { openedRevSta = true; } } if (counterFruThe == 0) { counterFruThe = timeTrafLigFruThe; if (openedFruThe) { openedFruThe = false; } else { openedFruThe = true; } } if (openedRevSta && tTheFru2.cars.Count == 0 && tTheFru3.cars.Count == 0 && tTheSta1.cars.Count == 0 && tTheSta2.cars.Count == 0 && tFruThe1.cars.Count == 0 && tFruThe2.cars.Count == 0 && tFruThe31.cars.Count == 0 && tFruThe32.cars.Count == 0 && tFruRev1.cars.Count == 0) { cpStaForwTrafLight1.status = cpRevForwTrafLight1.status = cpRevForwTrafLight2.status = cpRevForwTrafLight3.status = CheckPointStatus.Go; } else { cpStaForwTrafLight1.status = cpRevForwTrafLight1.status = cpRevForwTrafLight2.status = cpRevForwTrafLight3.status = CheckPointStatus.Stop; } if (openedFruThe && tStaRev1.cars.Count == 0 && tStaFru1.cars.Count == 0 && tStaFru2.cars.Count == 0 && tRevSta11.cars.Count == 0 && tRevSta12.cars.Count == 0 && tRevFru21.cars.Count == 0 && tRevFru22.cars.Count == 0 && tRevSta2.cars.Count == 0 && tRevThe2.cars.Count == 0 && tRevThe3.cars.Count == 0) { cpTheForwTrafLight1.status = cpTheForwTrafLight2.status = cpTheForwTrafLight3.status = cpFruForwTrafLight1.status = cpFruForwTrafLight2.status = CheckPointStatus.Go; } else { cpTheForwTrafLight1.status = cpTheForwTrafLight2.status = cpTheForwTrafLight3.status = cpFruForwTrafLight1.status = cpFruForwTrafLight2.status = CheckPointStatus.Stop; } if ((tStaForw1.cars.Count == 0 || cpStaForwTrafLight1.status == CheckPointStatus.Stop) && tStaRev1.GetDistance(0, true) >= distSafe) { cpRevForwLeft1.status = CheckPointStatus.Go; } else { cpRevForwLeft1.status = CheckPointStatus.Stop; } if (sleepTime > 0) { Thread.Sleep(sleepTime); } } } //catch //{ // MessageBox.Show("Непредвиденная ошибка, попробуйте " + // "изменить начальные параметры", "Ошибка", // MessageBoxButtons.OK, MessageBoxIcon.Error); //} }