public void sendPacket(PacketClass packet) { Console.WriteLine("Sending packet..."); NodeButton nextNode = findNextNode(packet.destinationAddress); if (nextNode != null) { nextNode.receivePacket(packet); } else { Console.WriteLine("Next Node not found!"); } }
public NodeButton findNextNode(AddressClass destinationAddress) { int x1 = this.nodeAddress.x; int y1 = this.nodeAddress.y; int x2 = destinationAddress.x; int y2 = destinationAddress.y; NodeButton closestNode = null; double distance = -1; List <NodeButton> towardsDestination = returnNodesTowardsDestination(destinationAddress); foreach (NodeButton node in towardsDestination) { AddressClass nodeAddress = node.nodeAddress; double x0 = nodeAddress.x; double y0 = nodeAddress.y; double x2_x1 = x2 - x1; double y1_y0 = y1 - y0; double x1_x0 = x1 - x0; double y2_y1 = y2 - y1; double numerator = System.Math.Abs(x2_x1 * y1_y0 - x1_x0 * y2_y1); double denominator = System.Math.Sqrt(x2_x1 * x2_x1 + y2_y1 * y2_y1); double tempDistance = numerator / denominator; if (distance == -1) { closestNode = node; distance = tempDistance; } else if (tempDistance < distance) { closestNode = node; distance = tempDistance; } } Console.WriteLine("Least Distance: " + distance); return(closestNode); }