aboutsummaryrefslogtreecommitdiffstatsci
path: root/Drawing2D/src/Rectangle.cs
blob: d120de693ee8d2d6cea087cc286320f1525dbeb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2013-2022  Bruyère Jean-Philippe jp_bruyere@hotmail.com
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using System.Runtime.InteropServices;

namespace Drawing2D {
	[StructLayout(LayoutKind.Sequential)]
	public struct Rectangle : IEquatable<Rectangle>
	{
		public static readonly Rectangle Zero = new Rectangle (0, 0, 0, 0);

		public int X, Y, Width, Height;

		#region ctor
		public Rectangle(Point p, Size s): this (p.X, p.Y, s.Width, s.Height) { }
		public Rectangle(Size s) : this (0, 0, s.Width, s.Height) { }
		public Rectangle(int x, int y, int width, int height) {
			X = x;
			Y = y;
			Width = width;
			Height = height;
		}
		#endregion

		#region PROPERTIES
		public int Left{
			get => X;
			set { X = value; }
		}
		public int Top{
			get => Y;
			set { Y = value; }
		}
		public int Right => X + Width;
		public int Bottom => Y + Height;
		public Size Size{
			get => new Size (Width, Height);
			set {
				Width = value.Width;
				Height = value.Height;
			}
		}
		public SizeD SizeD => new SizeD (Width, Height);
		public Point Position{
			get => new Point (X, Y);
			set {
				X = value.X;
				Y = value.Y;
			}
		}
		public Point TopLeft{
			get => new Point (X, Y);
			set {
				X = value.X;
				Y = value.Y;
			}
		}
		public Point TopRight => new Point (Right, Y);
		public Point BottomLeft => new Point (X, Bottom);
		public Point BottomRight => new Point (Right, Bottom);
		public Point Center => new Point (Left + Width / 2, Top + Height / 2);
		public Point CenterD => new PointD (Left + Width / 2.0, Top + Height / 2.0);
		public bool IsValid => Width > 0 && Height > 0;
		#endregion

		#region FUNCTIONS
		public void Inflate(int xDelta, int yDelta)
		{
			this.X -= xDelta;
			this.Width += 2 * xDelta;
			this.Y -= yDelta;
			this.Height += 2 * yDelta;
		}
		public void Inflate(int delta)
		{
			Inflate (delta, delta);
		}
		public Rectangle Inflated (int delta) => Inflated (delta, delta);
		public Rectangle Inflated (int deltaX, int deltaY) {
			Rectangle r = this;
			r.Inflate (deltaX, deltaY);
			return r;
		}
		public void Scale (double factor) {
			X = (int)Math.Round(factor * X);
			Y = (int)Math.Round(factor * Y);
			Width = (int)Math.Round(factor * Width);
			Height = (int)Math.Round(factor * Height);
		}
		public Rectangle Scaled (double factor) {
			return new Rectangle (
				(int)Math.Round(factor * X),
				(int)Math.Round(factor * Y),
				(int)Math.Round(factor * Width),
				(int)Math.Round(factor * Height));
		}
		public RectangleD ScaledD (double factor) {
			return new RectangleD (
				factor * X,
				factor * Y,
				factor * Width,
				factor * Height);
		}
		public bool ContainsOrIsEqual (Point p) => (p.X >= X && p.X <= X + Width && p.Y >= Y && p.Y <= Y + Height);
		public bool ContainsOrIsEqual (Rectangle r) => r.TopLeft >= this.TopLeft && r.BottomRight <= this.BottomRight;
		public bool Intersect(Rectangle r)
		{
			int maxLeft = Math.Max(this.Left, r.Left);
			int minRight = Math.Min(this.Right, r.Right);
			int maxTop = Math.Max(this.Top, r.Top);
			int minBottom = Math.Min(this.Bottom, r.Bottom);

			return (maxLeft < minRight) && (maxTop < minBottom);
		}
		public Rectangle Intersection(Rectangle r)
		{
			Rectangle result = new Rectangle();

			if (r.Left >= Left)
				result.Left = r.Left;
			else
				result.TopLeft = TopLeft;

			if (r.Right >= Right)
				result.Width = Right - result.Left;
			else
				result.Width = r.Right - result.Left;

			if (r.Top >= Top)
				result.Top = r.Top;
			else
				result.Top = Top;

			if (r.Bottom >= Bottom)
				result.Height = Bottom - result.Top;
			else
				result.Height = r.Bottom - result.Top;

			return result;
		}
		#endregion

		#region operators
		public static Rectangle operator +(Rectangle r1, Rectangle r2)
		{
			int x = Math.Min(r1.X, r2.X);
			int y = Math.Min(r1.Y, r2.Y);
			int x2 = Math.Max(r1.Right, r2.Right);
			int y2 = Math.Max(r1.Bottom, r2.Bottom);
			return new Rectangle(x, y, x2 - x, y2 - y);
		}
		public static Rectangle operator + (Rectangle r, Point p) => new Rectangle (r.X + p.X, r.Y + p.Y, r.Width, r.Height);
		public static Rectangle operator - (Rectangle r, Point p) => new Rectangle (r.X - p.X, r.Y - p.Y, r.Width, r.Height);
		public static bool operator == (Rectangle r1, Rectangle r2) => r1.Equals (r2);
		public static bool operator != (Rectangle r1, Rectangle r2) => !r1.Equals (r2);

		public static implicit operator Rectangle (RectangleD r) => new Rectangle ((int)Math.Round(r.X), (int)Math.Round (r.Y),
			(int)Math.Round (r.Width), (int)Math.Round (r.Height));
		#endregion

		public override int GetHashCode () => HashCode.Combine (X, Y, Width, Height);
		public override bool Equals (object obj) => obj is Rectangle r ? Equals (r) : false;
		public bool Equals(Rectangle other) =>
			X == other.X && Y == other.Y && Width == other.Width && Height == other.Height;

		public override string ToString () => $"{X},{Y},{Width},{Height}";
		public static Rectangle Parse(string s)
		{
			string[] d = s.Split(new char[] { ',' });
			return new Rectangle(
				int.Parse(d[0]),
				int.Parse(d[1]),
				int.Parse(d[2]),
				int.Parse(d[3]));
		}
	}
}