// // Rectangle.cs // // Author: // Mike Becker // // Copyright (c) 2019 Mike Becker // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System; using System.Text; namespace MBS.Framework.Drawing { public struct Rectangle : IComparable, IEquatable { public static readonly Rectangle Empty = new Rectangle(); public Rectangle(Vector2D location, Dimension2D size) { X = location.X; Y = location.Y; Width = size.Width; Height = size.Height; } public Rectangle(double x, double y, double width, double height) { X = x; Y = y; Width = width; Height = height; } public double X { get; set; } public double Y { get; set; } public double Width { get; set; } public double Height { get; set; } public Vector2D Location { get { return new Vector2D(X, Y); } set { X = value.X; Y = value.Y; } } public Dimension2D Size { get { return new Dimension2D(Width, Height); } set { Width = value.Width; Height = value.Height; } } public double Right { get { return X + Width; } set { Width = value - X; } } public double Bottom { get { return Y + Height; } set { Height = value - Y; } } public Rectangle Deflate(Padding padding) { Rectangle rect = this; rect.X += padding.Left; rect.Y += padding.Top; rect.Width -= padding.Right; rect.Height -= padding.Bottom; return rect; } public bool Contains(double x, double y) { return (x >= X && y >= Y && x <= Right && y <= Bottom); } public bool Contains(Vector2D point) { return Contains(point.X, point.Y); } public int CompareTo(Rectangle other) { double thisArea = this.Width * this.Height; double otherArea = other.Width * other.Height; return (int)(thisArea - otherArea); } #region IEquatable implementation public bool Equals(Rectangle other) { return (this.Width == other.Width && this.Height == other.Height); } #endregion public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append("("); sb.Append(X.ToString()); sb.Append(", "); sb.Append(Y.ToString()); sb.Append(")-("); sb.Append(Right.ToString()); sb.Append(", "); sb.Append(Bottom.ToString()); sb.Append(")"); sb.Append(", "); sb.Append(Width.ToString()); sb.Append("x"); sb.Append(Height.ToString()); return sb.ToString(); } public static bool operator ==(Rectangle left, Rectangle right) { return left.Equals(right); } public static bool operator !=(Rectangle left, Rectangle right) { return !left.Equals(right); } public override bool Equals(object obj) { return base.Equals(obj); } } }