From 1eee9b8dd9f26dd2c8fc8ba934827ef46cc3c576 Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Tue, 20 Feb 2018 11:41:02 +0100 Subject: [PATCH] Add method in graph to find node from position. --- src/main/org/insa/graph/Graph.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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. */