예제 #1
0
        public static CgmRectangle GetRectangle(Polyline polyline)
        {
            if (IsRectangle(polyline))
            {
                var points = polyline.Points;

                // rectangle is descriped counter clock-wise starting right
                if (CgmPoint.IsSame(points[0].Y, points[1].Y) && CgmPoint.IsSame(points[1].X, points[2].X) && CgmPoint.IsSame(points[2].Y, points[3].Y))
                {
                    if (points[1].Y < points[2].Y)
                    {
                        return(CgmRectangle.FromPoints(points[1], points[0], points[2], points[3]));
                    }
                    else if (points[0].X < points[1].X) // starting left
                    {
                        return(CgmRectangle.FromPoints(points[3], points[2], points[0], points[1]));
                    }
                    else
                    {
                        return(CgmRectangle.FromPoints(points[2], points[3], points[1], points[0]));
                    }
                }

                // rectangle is described clock wise
                if (CgmPoint.IsSame(points[0].X, points[1].X) && CgmPoint.IsSame(points[1].Y, points[2].Y) && CgmPoint.IsSame(points[2].X, points[3].X))
                {
                    return(CgmRectangle.FromPoints(points[4], points[0], points[3], points[1]));
                }
            }

            return(CgmRectangle.Empty);
        }
예제 #2
0
        private static IEnumerable <CgmRectangle> FindRectangleInSimpleLines(IEnumerable <Polyline> simpleLines)
        {
            // get all horizontal lines
            var horizontalLines = simpleLines.Where(l => IsHorizontalLine(l.Points[0], l.Points[1])).Select(l => new CgmLine(l.Points[0], l.Points[1]));
            var verticalLines   = simpleLines.Where(l => IsVerticalLine(l.Points[0], l.Points[1])).Select(l => new CgmLine(l.Points[0], l.Points[1]));
            var rects           = new List <RectanglePoints>();

            // loop through horizontal lines and find the two parelles each
            foreach (var horzLine in horizontalLines)
            {
                var others = horizontalLines.Where(l => CgmPoint.IsSame(l.A.X, horzLine.A.X) && l.A.Y > horzLine.A.Y);

                if (others.Any())
                {
                    var nearest = others.OrderBy(l => l.A.Y).Last();
                    rects.Add(new RectanglePoints(horzLine, nearest));
                }
            }

            // loop the vertical lines and find the ones linking to the horizontal ones
            foreach (var verticalLine in verticalLines)
            {
                var l = rects.FirstOrDefault(h => h.IsUpperLeft(verticalLine.A));

                if (l != null && l.IsLowerLeft(verticalLine.B))
                {
                    l.SetLowerLeft(verticalLine.B);
                    continue;
                }

                l = rects.FirstOrDefault(h => h.IsUpperRight(verticalLine.A));

                if (l != null && l.IsLowerRight(verticalLine.B))
                {
                    l.SetLowerRight(verticalLine.B);
                }
            }

            return(rects.Where(r => r.IsValid).Select(r => r.ToRectangle()));
        }
예제 #3
0
파일: CGMRectangle.cs 프로젝트: twenzel/CGM
        internal static void ValidationCorners(CgmPoint leftUpperCorner, CgmPoint rightUpperCorner, CgmPoint leftLowerCorner, CgmPoint rightLowerCorner)
        {
            if (!CgmPoint.IsSame(leftUpperCorner.Y, rightUpperCorner.Y))
            {
                throw new ArgumentException("The left upper corner is not at the same height as the right upper corner");
            }

            if (!CgmPoint.IsSame(leftLowerCorner.Y, rightLowerCorner.Y))
            {
                throw new ArgumentException("The left lower corner is not at the same height as the right lower corner");
            }

            if (!CgmPoint.IsSame(leftUpperCorner.X, leftLowerCorner.X))
            {
                throw new ArgumentException("The left upper corner is not at the same X as the left lower corner");
            }

            if (!CgmPoint.IsSame(rightUpperCorner.X, rightLowerCorner.X))
            {
                throw new ArgumentException("The right upper corner is not at the same X as the right lower corner");
            }
        }
예제 #4
0
 private static bool IsVerticalLine(CgmPoint a, CgmPoint b)
 {
     return(CgmPoint.IsSame(a.X, b.X) && !CgmPoint.IsSame(a.Y, b.Y));
 }
예제 #5
0
 private static bool IsHorizontalLine(CgmPoint a, CgmPoint b)
 {
     return(CgmPoint.IsSame(a.Y, b.Y) && !CgmPoint.IsSame(a.X, b.X));
 }