public void FindNextIntersections(int start, int[] expected) { var intersections = new[] { new Intersection(new Vector(100, 100), 10), new Intersection(new Vector(100, 200), 10), new Intersection(new Vector(200, 200), 10), new Intersection(new Vector(200, 100), 10) }; var city = new CityBuilder() .Road(new Road(new Vector(100, 105), new Vector(100, 195), 10, 100)) .Road(new Road(new Vector(105, 100), new Vector(195, 100), 10, 100)) .Road(new Road(new Vector(105, 200), new Vector(195, 200), 10, 100)) .Road(new Road(new Vector(200, 105), new Vector(200, 195), 10, 100)) .Intersection(intersections[0]) .Intersection(intersections[1]) .Intersection(intersections[2]) .Intersection(intersections[3]) .Build(); var result = GPSSystem.FindNextIntersections(intersections[start]).ToList(); Assert.AreEqual(expected.Length, result.Count); foreach (var index in expected) { Assert.Contains(intersections[index], result); } }
/// <summary> /// Calculates the path to the customergroup using Dijkstra algorithm /// </summary> /// <param name="start">start path</param> /// <param name="group">group customers</param> /// <returns>list with the roads to take</returns> public static List <Road> CalculateRouteToCustomerGroup(Vector start, CustomerGroup group) { var startRoad = GPSSystem.NearestRoad(start); var endRoad = GPSSystem.NearestRoad(group.Location); var hitEnd = false; var listPaths = new List <Path>(); // if the starroad equals to the endRoad if (startRoad == endRoad) { return(new List <Road> { startRoad }); } ; //add the first two intersections of the startRoad to the PathList; foreach (var item in GPSSystem.FindIntersectionsRoad(startRoad)) { listPaths.Add(new Path(start, group.Location, item)); } // loop until the end is found while (hitEnd == false) { var newListPaths = new List <Path>(); foreach (var path in listPaths) { // new found intersections var lastIntersectionOfPath = path.Intersections.Last(); var newFoundIntersections = GPSSystem.FindNextIntersections(lastIntersectionOfPath); // add the path of every new intersetion to the new ListPath foreach (var intersection in newFoundIntersections) { var intersections = new List <Intersection>(path.Intersections); intersections.Add(intersection); var newPath = new Path(start, group.Location, intersection); newListPaths.Add(newPath); // check if endLocation is between these intersections if ((endRoad.End == intersection.Location || endRoad.Start == intersection.Location) && (endRoad.End == lastIntersectionOfPath.Location || endRoad.Start == lastIntersectionOfPath.Location)) { return(newPath.PathToRoadList()); } else { throw new Exception("roads are not horizontal or vertical"); } } } // set the new list listPaths = newListPaths; } return(null); }
public override List <Road> CalculatePath(Vector start, CustomerGroup group) { var startRoad = GPSSystem.NearestRoad(start); var endRoad = GPSSystem.NearestRoad(group.Location); var listPaths = new List <Path>(); // if the startroad equals to the endRoad if (startRoad == endRoad) { return(new List <Road> { startRoad }); } ; //add the first two intersections of the startRoad to the PathList; foreach (var item in GPSSystem.FindIntersectionsRoad(startRoad)) { listPaths.Add(new Path(start, group.Location, item)); } // order list orderListByPriority(ref listPaths); // loop until the end is found while (true) { // new found intersections var lastIntersectionOfPath = listPaths[0].Intersections.Last(); var newFoundIntersections = GPSSystem.FindNextIntersections(lastIntersectionOfPath); // add the path of every new intersetion to the new ListPath foreach (var intersection in newFoundIntersections) { var intersections = new List <Intersection>(listPaths[0].Intersections); intersections.Add(intersection); var newPath = new Path(start, group.Location, intersection); listPaths.Add(newPath); // check if endLocation is between these intersections if ((endRoad.End == intersection.Location || endRoad.Start == intersection.Location) && (endRoad.End == lastIntersectionOfPath.Location || endRoad.Start == lastIntersectionOfPath.Location)) { return(newPath.PathToRoadList()); } } listPaths.RemoveAt(0); orderListByPriority(ref listPaths); } }