コード例 #1
0
        public void FindPath_WhenNoPathExists_WillBeNull()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(5);

            digraph.AddEdge(new DirectedEdge(0, 1, 2.5));
            digraph.AddEdge(new DirectedEdge(1, 2, 3.25));
            digraph.AddEdge(new DirectedEdge(1, 4, 4.75));
            digraph.AddEdge(new DirectedEdge(2, 3, 1.25));
            digraph.AddEdge(new DirectedEdge(3, 4, 2));

            IEnumerable <DirectedEdge> path = DijkstraShortestPath.FindPath(digraph, 4, 0);

            Assert.IsNull(path);
        }
コード例 #2
0
        public void FindPath_WhenMultiplePathsExistBetween0And4_WillBeShortestPath()
        {
            EdgeWeightedDigraph digraph = new EdgeWeightedDigraph(5);

            digraph.AddEdge(new DirectedEdge(0, 1, 2.5));
            digraph.AddEdge(new DirectedEdge(1, 2, 3.25));
            digraph.AddEdge(new DirectedEdge(1, 4, 4.75));
            digraph.AddEdge(new DirectedEdge(2, 3, 1.25));
            digraph.AddEdge(new DirectedEdge(3, 4, 2));

            DirectedEdge[] path = DijkstraShortestPath.FindPath(digraph, 0, 4).ToArray();

            Assert.AreEqual("From: 0, To: 1, Weight: 2.5", path[0].ToString());
            Assert.AreEqual("From: 1, To: 4, Weight: 4.75", path[1].ToString());
            Assert.AreEqual(2, path.Length);
        }
コード例 #3
0
        private IEnumerable <Cell> ShortestPathCells(Cell source, Cell destination)
        {
            IEnumerable <DirectedEdge> path = DijkstraShortestPath
                                              .FindPath(_graph, IndexFor(source.Point), IndexFor(destination.Point));

            if (path == null)
            {
                yield break;
            }

            yield return(source);

            foreach (DirectedEdge edge in path)
            {
                yield return(CellFor(edge.To));
            }
        }
コード例 #4
0
        private IEnumerable <ITile> ShortestPathCells(ITile source, ITile destination)
        {
            IEnumerable <DirectedEdge> path = DijkstraShortestPath.FindPath(_graph, IndexFor(source), IndexFor(destination));

            if (path == null)
            {
                yield return(null);
            }
            else
            {
                yield return(source);

                foreach (DirectedEdge edge in path)
                {
                    yield return(CellFor(edge.To));
                }
            }
        }
コード例 #5
0
        private IEnumerable <ICell> ShortestPathCells(ICell source, ICell destination)
        {
            var path = DijkstraShortestPath.FindPath(_graph, IndexFor(source), IndexFor(destination));

            if (path == null)
            {
                yield return(null);
            }
            else
            {
                yield return(source);

                foreach (var edge in path)
                {
                    yield return(CellFor(edge.To));
                }
            }
        }
コード例 #6
0
        public List <Point> ShortestPathList(Point source, Point destination)
        {
            var list = new List <Point>();
            IEnumerable <DirectedEdge> path = DijkstraShortestPath
                                              .FindPath(_graph, IndexFor(source), IndexFor(destination));

            if (path == null)
            {
                return(list);
            }

            foreach (DirectedEdge edge in path)
            {
                list.Add(CoordFor(edge.To));
            }

            return(list);
        }
コード例 #7
0
        /// <summary>
        /// Get instructions for Roomba to get to room
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        public List <Instruction> GetDirections(int room)
        {
            _directions = new List <Instruction>();
            _distance   = 0;
            int tempX = 0, tempY = 0; //for comparing X and Y of previous node

            var instructions = new List <Instruction>();

            endVertex = FindRoom(room);

            if (endVertex != null)
            {
                var edgePath = DijkstraShortestPath.FindPath(graph, startVertex.Id, endVertex.Id);

                foreach (var edge in edgePath)
                {
                    var from = GetVertex(edge.From);
                    var to   = GetVertex(edge.To);

                    //Turning from start
                    if (edge.From == startVertex.Id)
                    {
                        //ASSUME STARTING POSITION IS FACING NORTH (north as defined by map)
                        int xDiff = to.X - startVertex.X;
                        if (xDiff > 0)
                        {
                            //go east
                            //TURN RIGHT
                            instructions.Add(new Instruction(InstructionType.Angle, 90));
                            currentDirection += 90;
                        }
                        else if (xDiff < 0)
                        {
                            //go west
                            //TURN LEFT
                            instructions.Add(new Instruction(InstructionType.Angle, -90));
                            currentDirection -= 90;
                        }
                        else
                        {
                            int yDiff = to.Y - startVertex.Y;
                            if (yDiff < 0)
                            {
                                //go south
                                //TURN AROUND
                                instructions.Add(new Instruction(InstructionType.Angle, 180));
                                currentDirection += 180;
                            }
                            else if (yDiff > 0)
                            {
                                //go up (roomba starting should face north so no turn)
                            }
                        }
                    }
                    //Turning from a previous place
                    else
                    {
                        //Only turn if both X AND Y value is different
                        FindTurn(tempX, tempY, from.X, from.Y, to.X, to.Y);
                    }

                    //Add onto move distance (continuing in same direction)
                    _distance += edge.Weight;

                    //Check if we've reached the end and are done getting directions!
                    if (to.Id == endVertex.Id)
                    {
                        _directions.Add(new Instruction(InstructionType.Distance, _distance));
                        break;
                    }

                    //Set for next iteration of loop
                    tempX = from.X;
                    tempY = from.Y;
                }

                PrepareNextNavigation();
            }

            return(_directions);
        }