Algorithms.Point2D.DistanceSquaredTo C# (CSharp) Method

DistanceSquaredTo() public method

public DistanceSquaredTo ( Point2D that ) : double
that Point2D
return double
        public double DistanceSquaredTo(Point2D that) {
            var dx = X - that.X;
            var dy = Y - that.Y;
            return dx * dx + dy * dy;
        }
    }

Usage Example

Ejemplo n.º 1
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);
        }
All Usage Examples Of Algorithms.Point2D::DistanceSquaredTo