diff --git a/src/main/org/insa/graph/Graph.java b/src/main/org/insa/graph/Graph.java index 07b30f6..df7bf3d 100644 --- a/src/main/org/insa/graph/Graph.java +++ b/src/main/org/insa/graph/Graph.java @@ -24,6 +24,26 @@ public class Graph { */ public ArrayList getNodes() { return nodes; } + /** + * Find the closet node to the given point. + * + * @param point + * + * @return Closest node to the given point. + */ + public Node findClosestNode(Point point) { + Node node = null; + double minDis = Double.POSITIVE_INFINITY; + for (int n = 0 ; n < nodes.size(); ++n) { + double dis = point.distanceTo(nodes.get(n).getPoint()); + if (dis < minDis) { + node = nodes.get(n); + minDis = dis; + } + } + return node; + } + /** * @return Map ID of this graph. */