def CalculateAverageGraphEdgeLength(graph): """ Determines the average length of the edges in a navGraph (using the distance between the source & target node positions (not the cost of the edge as represented in the graph, which may account for all sorts of other factors such as terrain type, gradients etc) :param graph: :return: """ totalLength = 0 numberOfEdgesCounted = 0 for node in graph.Nodes: if node.Index != -1: for edge in graph.GetNodeEdges(node.Index): # Increment edge counter numberOfEdgesCounted += 1 # Add length of edge to total length totalLength += Vector2D.Vec2DDistance( graph.GetNode(edge.From).Position, graph.GetNode(edge.To).Position) return totalLength / numberOfEdgesCounted
def WeightNavGraphNodeEdges(graph, nodeIndex, weight): """ 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. :param graph: :param nodeIndex: :param weight: :return: """ # Make sure the node is present if nodeIndex > graph.NumberOfNodes(): raise Exception("Invalid node index") # Set the cost for each edge for edge in graph.GetNodeEdges(nodeIndex): # Calculate the distance between nodes distance = Vector2D.Vec2DDistance( graph.GetNode(edge.From).Position, graph.GetNode(edge.To).Position) # Set the cost of this edge graph.SetEdgeCost(edge.From, edge.To, distance * weight) # If not a digraph, set the cost of the parallel edge to be the same if not graph.IsDigraph: graph.SetEdgeCost(edge.To, edge.From, distance * weight)
def Calculate(graph, nodeIndex1, nodeIndex2): """ Calculate the straight line distance from node nodeIndex1 to node nodeIndex2 :param graph: :param nodeIndex1: :param nodeIndex2: :return: """ return Vector2D.Vec2DDistance( graph.GetNode(nodeIndex1).Position, graph.GetNode(nodeIndex2).Position) * uniform(0.9, 1.1)
def test_SpecifyThatTheDistanceBetweenTwoVectorsCanBeCalculatedUsingAStaticMethod( self): v1 = Vector2D(1, 0) v2 = Vector2D(1, 3) self.assertAlmostEqual(3.0, Vector2D.Vec2DDistance(v1, v2), places=3)