/// <summary> /// Returns a larger Rect2d that contains this Rect2d and `b`. /// </summary> /// <param name="b">The other Rect2d.</param> /// <returns>The merged Rect2d.</returns> public Rect2d Merge(Rect2d b) { Rect2d newRect; newRect._position.x = Mathd.Min(b._position.x, _position.x); newRect._position.y = Mathd.Min(b._position.y, _position.y); newRect._size.x = Mathd.Max(b._position.x + b._size.x, _position.x + _size.x); newRect._size.y = Mathd.Max(b._position.y + b._size.y, _position.y + _size.y); newRect._size -= newRect._position; // Make relative again return(newRect); }
/// <summary> /// Returns the intersection of this Rect2d and `b`. /// If the rectangles do not intersect, an empty Rect2d is returned. /// </summary> /// <param name="b">The other Rect2d.</param> /// <returns>The intersection of this Rect2d and `b`, or an empty Rect2d if they do not intersect.</returns> public Rect2d Clip(Rect2d b) { var newRect = b; if (!Intersects(newRect)) { return(new Rect2d()); } newRect._position.x = Mathd.Max(b._position.x, _position.x); newRect._position.y = Mathd.Max(b._position.y, _position.y); Vector2d bEnd = b._position + b._size; Vector2d end = _position + _size; newRect._size.x = Mathd.Min(bEnd.x, end.x) - newRect._position.x; newRect._size.y = Mathd.Min(bEnd.y, end.y) - newRect._position.y; return(newRect); }