public Edge(string id, Node start, Node end, float cost) { //unique id so we can delete by id later if necessary this.id = id; //set edge start and end points this.start = start; this.end = end; //straight-line distance between two points this.distance = Vector3.Distance(start.Position, end.Position); //cost of using the link is distance by some multiplier this.cost = distance * cost; }
/// <summary> /// Gets the distance * cost (usually 1) value to move to from start to end node (assumes they are connected by an edge) /// </summary> /// <param name="startNode"></param> /// <param name="endNode"></param> /// <returns></returns> public double GetCostBetweenNodes(Node startNode, Node endNode) { foreach (Edge edge in edgeList) { if (edge.Start.Equals(startNode) && edge.End.Equals(endNode)) { return edge.Cost; } } return 0; }
/// <summary> /// Add nodes that will be connected by edges - give each node a name and position /// </summary> /// <param name="node"></param> public void AddNode(Node node) { nodeDictionary.Add(node.ID, node); }
/// <summary> /// Gets a list of all nodes connected to a specfied node /// </summary> /// <param name="node"></param> /// <returns></returns> public List<Node> GetConnectedNodes(Node node) { List<Node> connectedNodeList = new List<Node>(); foreach (Edge edge in edgeList) { if (edge.Start.Equals(node) && originalNodeList.Contains(node)) { connectedNodeList.Add(edge.End); } } return connectedNodeList; }