From 60d3bf03904bc4f691d98b63ece8a1a7a482f182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 27 Mar 2013 16:59:00 +0100 Subject: Algorithms-I : 5-KdTrees: nearest must use local variables --- Algorithms/Part-I/5-KdTrees/KdTree.java | 44 +++++++++++++++++---------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/Algorithms/Part-I/5-KdTrees/KdTree.java b/Algorithms/Part-I/5-KdTrees/KdTree.java index 3607976..d0c0fcd 100644 --- a/Algorithms/Part-I/5-KdTrees/KdTree.java +++ b/Algorithms/Part-I/5-KdTrees/KdTree.java @@ -192,13 +192,15 @@ public class KdTree return nearest(root, p, null, Double.MAX_VALUE, true); } - private Point2D nearest(Node n, Point2D p, Point2D ncp, double d, boolean vsplit) + private Point2D nearest(Node n, Point2D p, Point2D cp, double d, boolean vsplit) { + Point2D cp0 = cp; + double d1 = d; double d2 = p.distanceSquaredTo(n.p); - if (d2 < d) + if (d2 < d1) { - ncp = n.p; - d = d2; + cp0 = n.p; + d1 = d2; } if (vsplit) @@ -208,21 +210,21 @@ public class KdTree { if (n.left != null) { - ncp = nearest(n.left, p, ncp, d, !vsplit); - d = p.distanceSquaredTo(ncp); + cp0 = nearest(n.left, p, cp0, d1, !vsplit); + d1 = p.distanceSquaredTo(cp0); } - if (n.right != null && ((d2 * d2) < d)) - ncp = nearest(n.right, p, ncp, d, !vsplit); + if (n.right != null && ((d2 * d2) < d1)) + cp0 = nearest(n.right, p, cp0, d1, !vsplit); } else { if (n.right != null) { - ncp = nearest(n.right, p, ncp, d, !vsplit); - d = p.distanceSquaredTo(ncp); + cp0 = nearest(n.right, p, cp0, d1, !vsplit); + d1 = p.distanceSquaredTo(cp0); } - if (n.left != null && ((d2 * d2) < d)) - ncp = nearest(n.left, p, ncp, d, !vsplit); + if (n.left != null && ((d2 * d2) < d1)) + cp0 = nearest(n.left, p, cp0, d1, !vsplit); } } else @@ -232,24 +234,24 @@ public class KdTree { if (n.left != null) { - ncp = nearest(n.left, p, ncp, d, !vsplit); - d = p.distanceSquaredTo(ncp); + cp0 = nearest(n.left, p, cp0, d1, !vsplit); + d1 = p.distanceSquaredTo(cp0); } - if (n.right != null && ((d2 * d2) < d)) - ncp = nearest(n.right, p, ncp, d, !vsplit); + if (n.right != null && ((d2 * d2) < d1)) + cp0 = nearest(n.right, p, cp0, d1, !vsplit); } else { if (n.right != null) { - ncp = nearest(n.right, p, ncp, d, !vsplit); - d = p.distanceSquaredTo(ncp); + cp0 = nearest(n.right, p, cp0, d1, !vsplit); + d1 = p.distanceSquaredTo(cp0); } - if (n.left != null && ((d2 * d2) < d)) - ncp = nearest(n.left, p, ncp, d, !vsplit); + if (n.left != null && ((d2 * d2) < d1)) + cp0 = nearest(n.left, p, cp0, d1, !vsplit); } } - return ncp; + return cp0; } } -- cgit v1.1-2-g2b99