Ejemplo n.º 1
0
        /// <summary>
        /// Get bounding rectangle of the specified list of points.
        /// </summary>
        /// 
        /// <param name="cloud">Collection of points to get bounding rectangle for.</param>
        /// <param name="minXY">Point comprised of smallest X and Y coordinates.</param>
        /// <param name="maxXY">Point comprised of biggest X and Y coordinates.</param>
        /// 
        public static void GetBoundingRectangle( IEnumerable<IntPoint> cloud, out IntPoint minXY, out IntPoint maxXY )
        {
            int minX = int.MaxValue;
            int maxX = int.MinValue;
            int minY = int.MaxValue;
            int maxY = int.MinValue;

            foreach ( IntPoint pt in cloud )
            {
                int x = pt.X;
                int y = pt.Y;

                // check X coordinate
                if ( x < minX )
                    minX = x;
                if ( x > maxX )
                    maxX = x;

                // check Y coordinate
                if ( y < minY )
                    minY = y;
                if ( y > maxY )
                    maxY = y;
            }

            if ( minX > maxX ) // if no point appeared to set either minX or maxX
                throw new ArgumentException( "List of points can not be empty." );

            minXY = new IntPoint( minX, minY );
            maxXY = new IntPoint( maxX, maxY );
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Calculate Euclidean distance between two points.
        /// </summary>
        /// 
        /// <param name="anotherPoint">Point to calculate distance to.</param>
        /// 
        /// <returns>Returns Euclidean distance between this point and
        /// <paramref name="anotherPoint"/> points.</returns>
        /// 
        public float DistanceTo( IntPoint anotherPoint )
        {
            int dx = X - anotherPoint.X;
            int dy = Y - anotherPoint.Y;

            return (float) System.Math.Sqrt( dx * dx + dy * dy );
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Shift cloud by adding specified value to all points in the collection.
 /// </summary>
 /// 
 /// <param name="cloud">Collection of points to shift their coordinates.</param>
 /// <param name="shift">Point to shift by.</param>
 /// 
 public static void Shift( IList<IntPoint> cloud, IntPoint shift )
 {
     for ( int i = 0, n = cloud.Count; i < n; i++ )
     {
         cloud[i] = cloud[i] + shift;
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Find the furthest point from the specified line.
        /// </summary>
        /// 
        /// <param name="cloud">Collection of points to search furthest points in.</param>
        /// <param name="linePoint1">First point forming the line.</param>
        /// <param name="linePoint2">Second point forming the line.</param>
        /// <param name="distance">Distance between the furthest found point and the given line.</param>
        /// 
        /// <returns>Returns a point, which is the furthest away from the
        /// specified line.</returns>
        /// 
        /// <remarks><para>The method finds the furthest point from the specified line.
        /// Unlike the <see cref="GetFurthestPointsFromLine( IEnumerable{IntPoint}, IntPoint, IntPoint, out IntPoint, out float, out IntPoint, out float )"/>
        /// method, this method find only one point, which is the furthest away from the line
        /// regardless of side from the line.</para></remarks>
        ///
        public static IntPoint GetFurthestPointFromLine( IEnumerable<IntPoint> cloud, IntPoint linePoint1, IntPoint linePoint2, out float distance )
        {
            IntPoint furthestPoint = linePoint1;
            distance = 0;

            if ( linePoint2.X != linePoint1.X )
            {
                // line's equation y(x) = k * x + b
                float k = (float) ( linePoint2.Y - linePoint1.Y ) / ( linePoint2.X - linePoint1.X );
                float b = linePoint1.Y - k * linePoint1.X;

                float div = (float) Math.Sqrt( k * k + 1 );
                float pointDistance = 0;

                foreach ( IntPoint point in cloud )
                {
                    pointDistance = Math.Abs( ( k * point.X + b - point.Y ) / div );

                    if ( pointDistance > distance )
                    {
                        distance = pointDistance;
                        furthestPoint = point;
                    }
                }
            }
            else
            {
                int lineX = linePoint1.X;
                float pointDistance = 0;

                foreach ( IntPoint point in cloud )
                {
                    distance = Math.Abs( lineX - point.X );

                    if ( pointDistance > distance )
                    {
                        distance = pointDistance;
                        furthestPoint = point;
                    }
                }
            }

            return furthestPoint;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Find the furthest point from the specified line.
        /// </summary>
        /// 
        /// <param name="cloud">Collection of points to search furthest point in.</param>
        /// <param name="linePoint1">First point forming the line.</param>
        /// <param name="linePoint2">Second point forming the line.</param>
        /// 
        /// <returns>Returns a point, which is the furthest away from the
        /// specified line.</returns>
        /// 
        /// <remarks><para>The method finds the furthest point from the specified line.
        /// Unlike the <see cref="GetFurthestPointsFromLine( IEnumerable{IntPoint}, IntPoint, IntPoint, out IntPoint, out IntPoint )"/>
        /// method, this method find only one point, which is the furthest away from the line
        /// regardless of side from the line.</para></remarks>
        ///
        public static IntPoint GetFurthestPointFromLine( IEnumerable<IntPoint> cloud, IntPoint linePoint1, IntPoint linePoint2 )
        {
            float d;

            return GetFurthestPointFromLine( cloud, linePoint1, linePoint2, out d );
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Find two furthest points from the specified line.
        /// </summary>
        /// 
        /// <param name="cloud">Collection of points to search furthest points in.</param>
        /// <param name="linePoint1">First point forming the line.</param>
        /// <param name="linePoint2">Second point forming the line.</param>
        /// <param name="furthestPoint1">First found furthest point.</param>
        /// <param name="distance1">Distance between the first found point and the given line.</param>
        /// <param name="furthestPoint2">Second found furthest point (which is on the
        /// opposite side from the line compared to the <paramref name="furthestPoint1"/>);</param>
        /// <param name="distance2">Distance between the second found point and the given line.</param>
        /// 
        /// <remarks><para>The method finds two furthest points from the specified line,
        /// where one point is on one side from the line and the second point is on
        /// another side from the line.</para></remarks>
        ///
        public static void GetFurthestPointsFromLine( IEnumerable<IntPoint> cloud, IntPoint linePoint1, IntPoint linePoint2,
            out IntPoint furthestPoint1, out float distance1, out IntPoint furthestPoint2, out float distance2 )
        {
            furthestPoint1 = linePoint1;
            distance1 = 0;

            furthestPoint2 = linePoint2;
            distance2 = 0;

            if ( linePoint2.X != linePoint1.X )
            {
                // line's equation y(x) = k * x + b
                float k = (float) ( linePoint2.Y - linePoint1.Y ) / ( linePoint2.X - linePoint1.X );
                float b = linePoint1.Y - k * linePoint1.X;

                float div = (float) Math.Sqrt( k * k + 1 );
                float distance = 0;

                foreach ( IntPoint point in cloud )
                {
                    distance = ( k * point.X + b - point.Y ) / div;

                    if ( distance > distance1 )
                    {
                        distance1 = distance;
                        furthestPoint1 = point;
                    }
                    if ( distance < distance2 )
                    {
                        distance2 = distance;
                        furthestPoint2 = point;
                    }
                }
            }
            else
            {
                int lineX = linePoint1.X;
                float distance = 0;

                foreach ( IntPoint point in cloud )
                {
                    distance = lineX - point.X;

                    if ( distance > distance1 )
                    {
                        distance1 = distance;
                        furthestPoint1 = point;
                    }
                    if ( distance < distance2 )
                    {
                        distance2 = distance;
                        furthestPoint2 = point;
                    }
                }
            }

            distance2 = -distance2;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Find two furthest points from the specified line.
        /// </summary>
        /// 
        /// <param name="cloud">Collection of points to search furthest points in.</param>
        /// <param name="linePoint1">First point forming the line.</param>
        /// <param name="linePoint2">Second point forming the line.</param>
        /// <param name="furthestPoint1">First found furthest point.</param>
        /// <param name="furthestPoint2">Second found furthest point (which is on the
        /// opposite side from the line compared to the <paramref name="furthestPoint1"/>);</param>
        /// 
        /// <remarks><para>The method finds two furthest points from the specified line,
        /// where one point is on one side from the line and the second point is on
        /// another side from the line.</para></remarks>
        /// 
        public static void GetFurthestPointsFromLine( IEnumerable<IntPoint> cloud, IntPoint linePoint1, IntPoint linePoint2,
            out IntPoint furthestPoint1, out IntPoint furthestPoint2 )
        {
            float d1, d2;

            GetFurthestPointsFromLine( cloud, linePoint1, linePoint2,
                out furthestPoint1, out d1, out furthestPoint2, out d2 );
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Find furthest point from the specified point.
        /// </summary>
        /// 
        /// <param name="cloud">Collection of points to search furthest point in.</param>
        /// <param name="referencePoint">The point to search furthest point from.</param>
        /// 
        /// <returns>Returns a point, which is the furthest away from the <paramref name="referencePoint"/>.</returns>
        /// 
        public static IntPoint GetFurthestPoint( IEnumerable<IntPoint> cloud, IntPoint referencePoint )
        {
            IntPoint furthestPoint = referencePoint;
            float maxDistance = -1;

            int rx = referencePoint.X;
            int ry = referencePoint.Y;

            foreach ( IntPoint point in cloud )
            {
                int dx = rx - point.X;
                int dy = ry - point.Y;
                // we are not calculating square root for finding "real" distance,
                // since it is really not important for finding furthest point
                float distance = dx * dx + dy * dy;

                if ( distance > maxDistance )
                {
                    maxDistance = distance;
                    furthestPoint = point;
                }
            }

            return furthestPoint;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Division operator - divides coordinates of the specified point by scalar value.
 /// </summary>
 /// 
 /// <param name="point">Point to divide coordinates of.</param>
 /// <param name="factor">Division factor.</param>
 /// 
 /// <returns>Returns new point which coordinates equal to coordinates of
 /// the specified point divided by specified value.</returns>
 /// 
 public static IntPoint Divide( IntPoint point, int factor )
 {
     return new IntPoint( point.X / factor, point.Y / factor );
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Multiplication operator - multiplies coordinates of the specified point by scalar value.
 /// </summary>
 /// 
 /// <param name="point">Point to multiply coordinates of.</param>
 /// <param name="factor">Multiplication factor.</param>
 /// 
 /// <returns>Returns new point which coordinates equal to coordinates of
 /// the specified point multiplied by specified value.</returns>
 ///
 public static IntPoint Multiply( IntPoint point, int factor )
 {
     return new IntPoint( point.X * factor, point.Y * factor );
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Subtraction operator - subtracts scalar from the specified point.
 /// </summary>
 /// 
 /// <param name="point">Point to decrease coordinates of.</param>
 /// <param name="valueToSubtract">Value to subtract from coordinates of the specified point.</param>
 /// 
 /// <returns>Returns new point which coordinates equal to coordinates of
 /// the specified point decreased by specified value.</returns>
 /// 
 public static IntPoint Subtract( IntPoint point, int valueToSubtract )
 {
     return new IntPoint( point.X - valueToSubtract, point.Y - valueToSubtract );
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Addition operator - adds scalar to the specified point.
 /// </summary>
 /// 
 /// <param name="point">Point to increase coordinates of.</param>
 /// <param name="valueToAdd">Value to add to coordinates of the specified point.</param>
 /// 
 /// <returns>Returns new point which coordinates equal to coordinates of
 /// the specified point increased by specified value.</returns>
 /// 
 public static IntPoint Add( IntPoint point, int valueToAdd )
 {
     return new IntPoint( point.X + valueToAdd, point.Y + valueToAdd );
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Subtraction operator - subtracts values of two points.
 /// </summary>
 /// 
 /// <param name="point1">Point to subtract from.</param>
 /// <param name="point2">Point to subtract.</param>
 /// 
 /// <returns>Returns new point which coordinates equal to difference of corresponding
 /// coordinates of specified points.</returns>
 ///
 public static IntPoint Subtract( IntPoint point1, IntPoint point2 )
 {
     return new IntPoint( point1.X - point2.X, point1.Y - point2.Y );
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Addition operator - adds values of two points.
 /// </summary>
 /// 
 /// <param name="point1">First point for addition.</param>
 /// <param name="point2">Second point for addition.</param>
 /// 
 /// <returns>Returns new point which coordinates equal to sum of corresponding
 /// coordinates of specified points.</returns>
 /// 
 public static IntPoint Add( IntPoint point1, IntPoint point2 )
 {
     return new IntPoint( point1.X + point2.X, point1.Y + point2.Y );
 }