/// <summary> /// Checks if the rectangle intersects with other specified rectangle. /// </summary> /// <param name="rect">Rectangle to check.</param> /// <returns>True - if yes; otherwise - false.</returns> public bool Intersects(MyRectangle rect) { return(rect.Peak.X < this.Peak.X + this.Width && this.Peak.X < rect.Peak.X + rect.Width && rect.Peak.Y < this.Peak.Y + this.Height && this.Peak.Y < rect.Peak.Y + rect.Height); }
/// <summary> /// Returns a new rectangle as a result of intersection of two rectangles. /// </summary> /// <param name="rect1">First rectangle.</param> /// <param name="rect2">Second rectangle.</param> /// <returns>New rectangle.</returns> public static MyRectangle BuildIntersection(MyRectangle rect1, MyRectangle rect2) { if (rect1 is null || rect2 is null) { throw new ArgumentNullException(); } if (rect1.Intersects(rect2)) { int rigthSide = Math.Min(rect1.Peak.X + rect1.Width, rect2.Peak.X + rect2.Width); int leftSide = Math.Max(rect1.Peak.X, rect2.Peak.X); int topSide = Math.Min(rect1.Peak.Y, rect2.Peak.Y); int bottomSide = Math.Min(rect1.Peak.Y + rect1.Height, rect2.Peak.Y + rect2.Height); return(new MyRectangle(new Point2D(leftSide, topSide), rigthSide - leftSide, bottomSide - topSide)); } else { return(new MyRectangle()); } }
/// <summary> /// Returns a new rectangle that contains in two specified rectangles. /// </summary> /// <param name="rect1">First rectangle.</param> /// <param name="rect2">Second rectangle.</param> /// <returns>New rectangle.</returns> public static MyRectangle BuildContainerRect(MyRectangle rect1, MyRectangle rect2) { if (rect1 is null || rect2 is null) { throw new ArgumentNullException(); } //select top left point //select the smallest x. int x = Math.Min(rect1.Peak.X, rect2.Peak.X); //select the greatest y. int y = Math.Max(rect1.Peak.Y, rect2.Peak.Y); //bottom right int width = 2 * Math.Abs(Math.Max(rect1.Width, rect2.Width) - Math.Min(rect1.Width, rect2.Width)); int height = 2 * Math.Abs(Math.Max(rect1.Height, rect2.Height) - Math.Min(rect1.Height, rect2.Height)); return(new MyRectangle(new Point2D(x, y), width, height)); }