Beispiel #1
0
        public IEnumerable <Point2D> Range(RectHV rect)
        {
            var queue = new Queue <Point2D>();

            Range(root, Container, rect, queue);
            return(queue);
        }
Beispiel #2
0
 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));
 }
Beispiel #3
0
        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);
        }
Beispiel #4
0
 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);
     }
 }
Beispiel #5
0
 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);
     }
 }
Beispiel #6
0
 public bool Intersects(RectHV that)
 {
     return(Xmax >= that.Xmin && Ymax >= that.Ymin &&
            that.Xmax >= Xmin && that.Ymax >= Ymin);
 }
Beispiel #7
0
 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);
 }
Beispiel #8
0
 public IEnumerable<Point2D> Range(RectHV rect) {
     var queue = new Queue<Point2D>();
     Range(root, Container, rect, queue);
     return queue;
 }
Beispiel #9
0
 public bool Intersects(RectHV that) {
     return Xmax >= that.Xmin && Ymax >= that.Ymin
            && that.Xmax >= Xmin && that.Ymax >= Ymin;
 }
Beispiel #10
0
        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;
        }