示例#1
0
        ///<summary>
        ///Given a cost value and an index to a valid node this function
        ///examines all a node's edges, calculates their length, and
        ///multiplies the value with the weight. Useful for setting terrain
        ///costs.
        ///</summary>
        ///<param name="graph"></param>
        ///<param name="node"></param>
        ///<param name="weight"></param>
        public static void WeightNavGraphNodeEdges(
            SparseGraph graph,
            int node,
            float weight)
        {
            //make sure the node is present
            Assert.Fatal(node < graph.NumNodes,
                         "GraphUtil.WeightNavGraphNodeEdges: node index out of range");

            //set the cost for each edge
            foreach (NavGraphEdge curEdge in graph.Edges[node])
            {
                //calculate the distance between nodes
                float dist = (graph.GetNode(curEdge.From).Position -
                              graph.GetNode(curEdge.To).Position).Length();

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

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