/// <summary>
        /// Normalizes point cloud to range [-1..1]. Ratios will be preserved.
        /// <para>A bounding rectangle will be determined and then </para>
        /// <para>  1) points will be translated for (rect center X, rect center Y)</para>
        /// <para>  2) and then rescaled for (1/scale, 1/scale) where scale is max(width, height).</para>
        /// </summary>
        /// <param name="points">Points to normalize.</param>
        /// <returns>Normalized points.</returns>
        public static IEnumerable<PointF> Normalize(this IEnumerable<PointF> points)
        {
            var rect = points.BoundingRect();
            var center = rect.Center();
            var scaleFactor = System.Math.Max(rect.Width, rect.Height);

            var transform = Transforms2D.Combine
                            (
                                Transforms2D.Translation(-center.X, -center.Y),
                                Transforms2D.Scale(1 / scaleFactor, 1 / scaleFactor)
                            );

            points = points.Transform(transform);

            return points;
        }
Пример #2
0
 public static WriteableBitmap MakeSmallerCopy(this WriteableBitmap original, int maxDimension)
 {
     var ratio = 0.0;
     if (original.PixelHeight > original.PixelWidth)
     {
         ratio = ((double)original.PixelHeight) / maxDimension;
     }
     else
     {
         ratio = ((double)original.PixelWidth) / maxDimension;
     }
     if (ratio > 1.0)
         ratio = 1.0;
     var newWidth = (int)Math.Floor(((double)original.PixelWidth) / ratio);
     var newHeight = (int)Math.Floor(((double)original.PixelHeight) / ratio);
     var toReturn = new WriteableBitmap(newWidth, newHeight);
     toReturn.Blit(toReturn.BoundingRect(), original, original.BoundingRect());
     return toReturn;
 }
Пример #3
0
        /// <summary>
        /// Determines whether the polygon forms rectangle.
        /// </summary>
        /// <param name="points">Polygon.</param>
        /// <returns>True if the polygon forms rectangle, false otherwise.</returns>
        public static bool IsRectangle(this IEnumerable<Point> points)
        {
            if (points.Count() != 4)
                return false;

            var rect = points.BoundingRect();

            bool hasTopLeft = false, hasTopRight = false, hasBottomLeft = false, hasBottomRight = false;

            foreach (var pt in points)
            {
                if (rect.Top == pt.Y)
                {
                    if (rect.X == pt.X)
                        hasTopLeft = true;

                    if (rect.Right == pt.X)
                        hasTopRight = true;
                }

                if (rect.Bottom == pt.Y)
                {
                    if (rect.X == pt.X)
                        hasBottomLeft = true;

                    if (rect.Right == pt.X)
                        hasBottomRight = true;
                }
            }

            return hasTopLeft && hasTopRight && hasBottomLeft && hasBottomRight;
        }
Пример #4
0
 public static RectFloat BoundingRect(this IEnumerable<Z2<float>> items)
 {
     return items.BoundingRect(NegInfRect);
 }