Algorithms.RectHV.DistanceSquaredTo C# (CSharp) Метод

DistanceSquaredTo() публичный Метод

public DistanceSquaredTo ( Point2D p ) : double
p Point2D
Результат double
        public double DistanceSquaredTo(Point2D p) {
            double dx = 0, dy = 0;
            if (p.X < Xmin) {
                dx = p.X - Xmin;
            }
            else if (p.X > Xmax) {
                dx = p.X - Xmax;
            }
            if (p.Y < Ymin) {
                dy = p.Y - Ymin;
            }
            else if (p.Y > Ymax) {
                dy = p.Y - Ymax;
            }
            return dx * dx + dy * dy;
        }

Usage Example

Пример #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.RectHV::DistanceSquaredTo