Пример #1
0
        /// <summary>
        ///     Applies Affine transformation to point
        /// </summary>
        /// <param name="point"></param>
        /// <param name="transormationMatrix"></param>
        /// <returns></returns>
        private Point Transform(Point point, Matrix transormationMatrix)
        {
            var transformMatrix = new AffineTransform(transormationMatrix[Matrix.I11], transormationMatrix[Matrix.I12],
                                                      transormationMatrix[Matrix.I21], transormationMatrix[Matrix.I22],
                                                      transormationMatrix[Matrix.I31], transormationMatrix[Matrix.I32]);

            var dst = new Point2D.Float();

            transformMatrix.Transform(new Point2D.Float(point.X, point.Y), dst);


            if (_pageRotation == 0)
            {
                return(new Point(dst.x, _pageHeight - dst.y));
            }

            if (_pageRotation == 90)
            {
                return(new Point(dst.x, dst.y));
            }

            throw new Exception("Current page rotation is not handled");
        }
Пример #2
0
        //TODO: Add logic to analyze lines as delimeters
        /// <summary>
        ///     Tries to construct a rectangle from current points
        /// </summary>
        /// <param name="ctm"></param>
        private void ConstructShape(Matrix ctm)
        {
            if (_currentPoints.Count > 0)
            {
                if (((_currentPoints.Count == 5) && _currentPoints[0].IsEqual(_currentPoints[4])) ||
                    (_currentPoints.Count == 4))
                {
                    var point = new Point[3];
                    point[0] = Transform(_currentPoints[0], ctm);
                    point[1] = Transform(_currentPoints[1], ctm);
                    point[2] = Transform(_currentPoints[2], ctm);

                    var maxY = point.Max(x => x.Y);
                    var minY = point.Min(x => x.Y);
                    var maxX = point.Max(x => x.X);
                    var minX = point.Min(x => x.X);


                    _rectangles.Add(new Rectangle
                    {
                        X      = minX,
                        Y      = maxY,
                        Height = maxY - minY,
                        Width  = maxX - minX
                    });
                    return;
                }
            }


            if (_currentPoints.Count == 0)
            {
                return;
            }

            var points =
                _currentPoints.Select(currentPoint => Transform(currentPoint, ctm))
                .DistinctBy(p => new { p.X, p.Y })
                .ToList();

            //we have a rectangle
            if (points.Count == 4)
            {
                var sortedPoints = points.OrderBy(x => x.X).ThenByDescending(x => x.Y).ToList();
                _rectangles.Add(new Rectangle
                {
                    X      = sortedPoints[1].X,
                    Y      = sortedPoints[1].Y,
                    Height = Math.Abs(sortedPoints[1].Y - sortedPoints[0].Y),
                    Width  = sortedPoints[2].X - sortedPoints[0].X
                });
                return;
            }

            if (_currentPoints.Count == 2)
            {
                var beginPoint = Transform(_currentPoints[0], ctm);
                var endPoint   = Transform(_currentPoints[1], ctm);
                _lines.Add(new Line(beginPoint, endPoint));
            }
        }