public IEnumerable <Point2D> Range(RectHV rect) { var queue = new Queue <Point2D>(); Range(root, Container, rect, queue); return(queue); }
private static RectHV RightRect(RectHV rect, KdNode node) { if (node.Vertical) { return(new RectHV(node.X, rect.Ymin, rect.Xmax, rect.Ymax)); } return(new RectHV(rect.Xmin, node.Y, rect.Xmax, rect.Ymax)); }
private Point2D Nearest(KdNode node, RectHV rect, double x, double y, Point2D candidate) { if (node == null) { return(candidate); } double dqn = 0; double drq = 0; var query = new Point2D(x, y); var nearest = candidate; if (nearest != null) { dqn = query.DistanceSquaredTo(nearest); drq = rect.DistanceSquaredTo(query); } if (nearest == null || dqn < drq) { var point = new Point2D(node.X, node.Y); if (nearest == null || dqn > query.DistanceSquaredTo(point)) { nearest = point; } } var left = LeftRect(rect, node); var right = RightRect(rect, node); if (node.Vertical) { if (x < node.X) { nearest = Nearest(node.Left, left, x, y, nearest); nearest = Nearest(node.Right, right, x, y, nearest); } else { nearest = Nearest(node.Right, right, x, y, nearest); nearest = Nearest(node.Left, left, x, y, nearest); } } else { if (y < node.Y) { nearest = Nearest(node.Left, left, x, y, nearest); nearest = Nearest(node.Right, right, x, y, nearest); } else { nearest = Nearest(node.Right, right, x, y, nearest); nearest = Nearest(node.Left, left, x, y, nearest); } } return(nearest); }
private void Range(KdNode node, RectHV nrect, RectHV rect, Queue<Point2D> queue) { if (node == null) { return; } if (rect.Intersects(nrect)) { var p = new Point2D(node.X, node.Y); if (rect.Contains(p)) { queue.Enqueue(p); } Range(node.Left, LeftRect(nrect, node), rect, queue); Range(node.Right, RightRect(nrect, node), rect, queue); } }
private void Range(KdNode node, RectHV nrect, RectHV rect, Queue <Point2D> queue) { if (node == null) { return; } if (rect.Intersects(nrect)) { var p = new Point2D(node.X, node.Y); if (rect.Contains(p)) { queue.Enqueue(p); } Range(node.Left, LeftRect(nrect, node), rect, queue); Range(node.Right, RightRect(nrect, node), rect, queue); } }
public bool Intersects(RectHV that) { return(Xmax >= that.Xmin && Ymax >= that.Ymin && that.Xmax >= Xmin && that.Ymax >= Ymin); }
private static RectHV RightRect(RectHV rect, KdNode node) { if (node.Vertical) { return new RectHV(node.X, rect.Ymin, rect.Xmax, rect.Ymax); } return new RectHV(rect.Xmin, node.Y, rect.Xmax, rect.Ymax); }
public IEnumerable<Point2D> Range(RectHV rect) { var queue = new Queue<Point2D>(); Range(root, Container, rect, queue); return queue; }
public bool Intersects(RectHV that) { return Xmax >= that.Xmin && Ymax >= that.Ymin && that.Xmax >= Xmin && that.Ymax >= Ymin; }
private Point2D Nearest(KdNode node, RectHV rect, double x, double y, Point2D candidate) { if (node == null) { return candidate; } double dqn = 0; double drq = 0; var query = new Point2D(x, y); var nearest = candidate; if (nearest != null) { dqn = query.DistanceSquaredTo(nearest); drq = rect.DistanceSquaredTo(query); } if (nearest == null || dqn < drq) { var point = new Point2D(node.X, node.Y); if (nearest == null || dqn > query.DistanceSquaredTo(point)) { nearest = point; } } var left = LeftRect(rect, node); var right = RightRect(rect, node); if (node.Vertical) { if (x < node.X) { nearest = Nearest(node.Left, left, x, y, nearest); nearest = Nearest(node.Right, right, x, y, nearest); } else { nearest = Nearest(node.Right, right, x, y, nearest); nearest = Nearest(node.Left, left, x, y, nearest); } } else { if (y < node.Y) { nearest = Nearest(node.Left, left, x, y, nearest); nearest = Nearest(node.Right, right, x, y, nearest); } else { nearest = Nearest(node.Right, right, x, y, nearest); nearest = Nearest(node.Left, left, x, y, nearest); } } return nearest; }