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); }
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())); }
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"); } }
private static bool GetIsValid(CgmPoint leftUpperCorner, CgmPoint rightUpperCorner, CgmPoint leftLowerCorner, CgmPoint rightLowerCorner) { if (leftUpperCorner.Y != rightUpperCorner.Y) { return(false); } if (leftLowerCorner.Y != rightLowerCorner.Y) { return(false); } if (leftUpperCorner.X != leftLowerCorner.X) { return(false); } if (rightUpperCorner.X != rightLowerCorner.X) { return(false); } return(true); }
/// <summary> /// Create a rectangle from the rectangle points. /// </summary> /// <param name="leftUpperCorner">The left upper corner.</param> /// <param name="rightUpperCorner">The right upper corner.</param> /// <param name="leftLowerCorner">The left lower corner.</param> /// <param name="rightLowerCorner">The right lower corner.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public static CgmRectangle FromPoints(CgmPoint leftUpperCorner, CgmPoint rightUpperCorner, CgmPoint leftLowerCorner, CgmPoint rightLowerCorner) { ValidationCorners(leftUpperCorner, rightUpperCorner, leftLowerCorner, rightLowerCorner); return(new CgmRectangle(leftUpperCorner.X, leftUpperCorner.Y, rightUpperCorner.X - leftUpperCorner.X, leftLowerCorner.Y - leftUpperCorner.Y)); }
/// <summary> /// Determines if the specified point is contained within this rectangle. /// </summary> /// <param name="point">The point to test.</param> /// <param name="maxDistance">The maximum distance to the rectangle border.</param> /// <returns> /// This method returns true if the point defined by <paramref name="x" /> and <paramref name="y" /> is contained within this rectangle; otherwise false. /// </returns> public bool Contains(CgmPoint point, double maxDistance) { return(Contains(point.X, point.Y, maxDistance)); }
/// <summary>Determines if the specified point is contained within this rectangle.</summary> /// <returns>This method returns true if the point defined by <paramref name="x" /> and <paramref name="y" /> is contained within this rectangle; otherwise false.</returns> /// <param name="point">The point to test. </param> public bool Contains(CgmPoint point) { return(Contains(point.X, point.Y)); }
public void SetLowerRight(CgmPoint p) { _rightLowerCorner = p; }
public void SetLowerLeft(CgmPoint p) { _leftLowerCorner = p; }
public bool IsLowerRight(CgmPoint p) { return(_bottomLine.B.CompareTo(p) == 0); }
public bool IsUpperRight(CgmPoint p) { return(_topLine.B.CompareTo(p) == 0); }
public bool IsLowerLeft(CgmPoint p) { return(_bottomLine.A.CompareTo(p) == 0); }
public bool IsUpperLeft(CgmPoint p) { return(_topLine.A.CompareTo(p) == 0); }
/// <summary> /// Determines whether point A is near point b /// </summary> /// <param name="pointA">The start point.</param> /// <param name="pointWithinRange">The point with have to be within the range of the start point.</param> /// <param name="rangeDistance">The range distance.</param> public static bool IsNearBy(CgmPoint pointA, CgmPoint pointWithinRange, float rangeDistance) { var rect = new CgmRectangle((float)pointA.X - rangeDistance, (float)pointA.Y - rangeDistance, rangeDistance * 2, rangeDistance * 2); return(rect.Contains(pointWithinRange)); }
private static bool IsVerticalLine(CgmPoint a, CgmPoint b) { return(CgmPoint.IsSame(a.X, b.X) && !CgmPoint.IsSame(a.Y, b.Y)); }
private static bool IsHorizontalLine(CgmPoint a, CgmPoint b) { return(CgmPoint.IsSame(a.Y, b.Y) && !CgmPoint.IsSame(a.X, b.X)); }