Пример #1
0
        //------------ Helper_AddAllNeighboursToGridNode ------------------
        //
        //  use to add the eight neighboring edges of a graph node that
        //  are positioned in a grid layout
        //------------------------------------------------------------------------
        public static void Helper_AddAllNeighboursToGridNode(SparseGraph graph,
                                                             int row,
                                                             int col,
                                                             int NumCellsX,
                                                             int NumCellsY)
        {
            for (int i = -1; i < 2; ++i)
            {
                for (int j = -1; j < 2; ++j)
                {
                    int nodeX = col + j;
                    int nodeY = row + i;

                    //skip if equal to this node
                    if ((i == 0) && (j == 0))
                    {
                        continue;
                    }

                    //check to see if this is a valid neighbour
                    if (ValidNeighbour(nodeX, nodeY, NumCellsX, NumCellsY))
                    {
                        //calculate the distance to this node
                        Vector2D PosNode      = graph.GetNode(row * NumCellsX + col).Pos;
                        Vector2D PosNeighbour = graph.GetNode(nodeY * NumCellsX + nodeX).Pos;

                        double dist = PosNode.Distance(PosNeighbour);

                        NavGraphEdge NewEdge;

                        //this neighbour is okay so it can be added
                        NewEdge = new NavGraphEdge(row * NumCellsX + col,
                                                   nodeY * NumCellsX + nodeX,
                                                   dist);
                        graph.AddEdge(NewEdge);

                        //if graph is not a diagraph then an edge needs to be added going
                        //in the other direction
                        if (!graph.isDigraph())
                        {
                            NewEdge = new NavGraphEdge(nodeY * NumCellsX + nodeX,
                                                       row * NumCellsX + col,
                                                       dist);
                            graph.AddEdge(NewEdge);
                        }
                    }
                }
            }
        }
Пример #2
0
        public static void Helper_WeightNavGraphNodeEdges(SparseGraph graph, int node, double weight)
        {
            //make sure the node is present
            Debug.Assert(node < graph.NumNodes(), "Node can not exist in graph!");

            //set the cost for each edge
            SparseGraph.EdgeIterator EdgeItr = new SparseGraph.EdgeIterator(graph, node);

            while (EdgeItr.MoveNext())
            {
                //calculate the distance between nodes
                double dist = Vector2D.Vec2DDistance(graph.GetNode(EdgeItr.Current.From).Pos,
                                                     graph.GetNode(EdgeItr.Current.To).Pos);

                //set the cost of this edge
                graph.SetEdgeCost(EdgeItr.Current.From, EdgeItr.Current.To, dist * weight);

                //if not a digraph, set the cost of the parallel edge to be the same
                if (!graph.isDigraph())
                {
                    graph.SetEdgeCost(EdgeItr.Current.To, EdgeItr.Current.From, dist * weight);
                }
            }
        }