namespace GeometriCS { /// /// Geometric extents of a finitely large entity. Double precision. /// public struct Extents2d { /// /// The point with the lowest Y and X. /// public Vector2d MinPoint { get; } /// /// The point with the highest Y and X. /// public Vector2d MaxPoint { get; } /// /// Take two points and create Extents from their bounding box. /// /// First point. /// Second point. /// Extents of the bounding box of the two points. public static Extents2d FromTwoPoints(Vector2d pt1, Vector2d pt2) { // Find the smallest and largest x and y values. double minx = pt1.X < pt2.X ? pt1.X : pt2.X; double miny = pt1.Y < pt2.Y ? pt1.Y : pt2.Y; double maxx = pt1.X > pt2.X ? pt1.X : pt2.X; double maxy = pt1.Y > pt2.Y ? pt1.Y : pt2.Y; // Create new points from them and create the extents. return new Extents2d(new Vector2d(minx, miny), new Vector2d(maxx, maxy)); } /// /// Is there an overlap between the bounding boxes of these two extents? /// /// /// true if there is an overlap, otherwise false. public bool Overlaps(Extents2d other) { // If at least one of my points are in others X band, or at least one of others points are in my X band // check whether at least one of my points are in others Y band, or at least one of the others points // are in my Y band. If both conditions are met, there is an overlap. return (MinPoint.X.IsInRange(other.MinPoint.X, other.MaxPoint.X) || MaxPoint.X.IsInRange(other.MinPoint.X, other.MaxPoint.X) || other.MinPoint.X.IsInRange(MinPoint.X, MaxPoint.X) || other.MaxPoint.X.IsInRange(MinPoint.X, MaxPoint.X) ) && ( MinPoint.Y.IsInRange(other.MinPoint.Y, other.MaxPoint.Y) || MaxPoint.Y.IsInRange(other.MinPoint.Y, other.MaxPoint.Y) || other.MinPoint.Y.IsInRange(MinPoint.Y, MaxPoint.Y) || other.MaxPoint.Y.IsInRange(MinPoint.Y, MaxPoint.Y)); } /// /// Does the bounding box contain the point. /// /// Point to test. /// true if the point is inside of the bounding box, otherwise false. public bool Contains(Vector2d point) { return point.X.IsInRange(MinPoint.X, MaxPoint.X) && point.Y.IsInRange(MinPoint.Y, MaxPoint.Y); } /// /// Constructor for the Extents2d. /// /// The point with the lowest X and Y. /// The point with the greatest X and Y. /// /// The maxpoint is not greater in both dimensions than the minpoint. /// public Extents2d(Vector2d minPoint, Vector2d maxPoint) { /// Check validity if((minPoint.X > maxPoint.X && !Utils.DoubleEquals(minPoint.X, maxPoint.X)) || (minPoint.Y > maxPoint.Y && !Utils.DoubleEquals(minPoint.Y, maxPoint.Y))) { throw new ArgumentException($"The minpoint {minPoint} has to " + $"have lesser both X and Y than the maxpoint {maxPoint}."); } // Assign the properties. MinPoint = minPoint; MaxPoint = maxPoint; } } }