summaryrefslogtreecommitdiffstats
path: root/Algorithms/Part-I/5-KdTrees/KdTree.java
diff options
context:
space:
mode:
Diffstat (limited to 'Algorithms/Part-I/5-KdTrees/KdTree.java')
-rw-r--r--Algorithms/Part-I/5-KdTrees/KdTree.java18
1 files changed, 9 insertions, 9 deletions
diff --git a/Algorithms/Part-I/5-KdTrees/KdTree.java b/Algorithms/Part-I/5-KdTrees/KdTree.java
index bcf4609..e11a769 100644
--- a/Algorithms/Part-I/5-KdTrees/KdTree.java
+++ b/Algorithms/Part-I/5-KdTrees/KdTree.java
@@ -207,7 +207,7 @@ public class KdTree
private void nearest(Node n, Point2D p, NearestChampion ncp, boolean vsplit)
{
- double d2 = p.distanceTo(n.p);
+ double d2 = p.distanceSquaredTo(n.p);
if (d2 < ncp.d)
{
ncp.d = d2;
@@ -215,37 +215,37 @@ public class KdTree
}
if (vsplit)
{
- double nx = n.p.x();
- if (p.x() < nx)
+ double d3 = n.p.x() - p.x();
+ if (d3 > 0)
{
if (n.left != null)
nearest(n.left, p, ncp, !vsplit);
- if (n.right != null && ((nx - p.x()) < ncp.d))
+ if (n.right != null && ((d3 * d3) < ncp.d))
nearest(n.right, p, ncp, !vsplit);
}
else
{
if (n.right != null)
nearest(n.right, p, ncp, !vsplit);
- if (n.left != null && ((p.x() - nx) < ncp.d))
+ if (n.left != null && ((d3 * d3) < ncp.d))
nearest(n.left, p, ncp, !vsplit);
}
}
else
{
- double ny = n.p.y();
- if (p.y() < ny)
+ double d3 = n.p.y() - p.y();
+ if (d3 > 0)
{
if (n.left != null)
nearest(n.left, p, ncp, !vsplit);
- if (n.right != null && ((ny - p.y()) < ncp.d))
+ if (n.right != null && ((d3 * d3) < ncp.d))
nearest(n.right, p, ncp, !vsplit);
}
else
{
if (n.right != null)
nearest(n.right, p, ncp, !vsplit);
- if (n.left != null && ((p.y() - ny) < ncp.d))
+ if (n.left != null && ((d3 * d3) < ncp.d))
nearest(n.left, p, ncp, !vsplit);
}
}