Exemple #1
0
        /// <summary>
        /// Determines the squared distance from this ring to the given point <paramref name="p"/>.
        /// </summary>
        /// <param name="p">The point to determine the squared distance to.</param>
        /// <returns>The squared distance from this ring to the given point <paramref name="p"/>.</returns>
        /// <remarks>
        /// The squared distance will be 0 when the ring is a fill and the point lies within the boundary, when the ring is a hole and the point lies outside of the boundary, or when the point lies on the boundary.
        /// </remarks>
        public double DistanceSquared(Point2 p)
        {
            Contract.Ensures(
                Contract.Result <double>() >= 0.0 ||
                Double.IsNaN(Contract.Result <double>()) ||
                Double.IsPositiveInfinity(Contract.Result <double>()));
            var lastIndex = _pointList.Count - 1;

            if (lastIndex > 1)
            {
                if (Intersects(p, false))
                {
                    return(0);
                }
                var d = Segment2.DistanceSquared(_pointList[lastIndex], _pointList[0], p);
                for (int i = 0, nextIndex = 1; i < lastIndex; i = nextIndex++)
                {
                    d = Math.Min(d, Segment2.DistanceSquared(_pointList[i], _pointList[nextIndex], p));
                }
                return(d);
            }
            return(0 == lastIndex
                ? _pointList[0].DistanceSquared(p)
                : 1 == lastIndex
                    ? Segment2.DistanceSquared(_pointList[0], _pointList[1], p)
                    : Double.NaN
                   );
        }
Exemple #2
0
        /// <summary>
        /// Calculates the squared distance between this line string and the point, <paramref name="p"/>.
        /// </summary>
        /// <param name="p">The point to calculate squared distance to.</param>
        /// <returns>The squared distance.</returns>
        public double DistanceSquared(Point2 p)
        {
            Contract.Ensures(Contract.Result <double>() >= 0.0 || Contract.Result <double>().Equals(Double.NaN));
            var lastIndex = Count - 1;

            if (lastIndex < 1)
            {
                return(0 == lastIndex ? this[0].DistanceSquared(p) : Double.NaN);
            }

            var minDistance = Segment2.DistanceSquared(this[0], this[1], p);

            for (int i = 1, nextIndex = 2; i < lastIndex; i = nextIndex++)
            {
                var localDistance = Segment2.DistanceSquared(this[i], this[nextIndex], p);
                if (localDistance < minDistance)
                {
                    minDistance = localDistance;
                }
            }
            return(minDistance);
        }