aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/Rectangle.cs
blob: 9a4ef396dfd64430585277b1f0913ed47a70bb75 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Crow
{    
	public struct Rectangle
    {
		#region private fields
        int _x;
        int _y;
        int _width;
        int _height;
		#endregion

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

		#region PROPERTIES
        [XmlIgnore]public int X{
            get { return _x; }
            set { _x = value; }
        }
		[XmlIgnore]public int Y{
            get { return _y; }
            set { _y = value; }
        }
		[XmlIgnore]public int Left{
            get { return _x; }
            set { _x = value; }
        }
		[XmlIgnore]public int Top{
            get { return _y; }
            set { _y = value; }
        }
		[XmlIgnore]public int Right{
            get { return _x + _width; }
        }
		[XmlIgnore]public int Bottom{
            get { return _y + _height; }
        }
		[XmlIgnore]public int Width{
            get { return _width; }
            set { _width = value; }
        }
		[XmlIgnore]public int Height{
            get { return _height; }
            set { _height = value; }
        }
		[XmlIgnore]public Size Size{
            get { return new Size(Width, Height); }
            set
            {
                Width = value.Width;
                Height = value.Height;
            }
        }
		[XmlIgnore]public Point Position{
			get { return new Point(X, Y); }
			set
			{
				X = value.X;
				Y = value.Y;
			}
		}
		[XmlIgnore]public Point TopLeft{
            set
            {
                X = value.X;
                Y = value.Y;
            }
            get { return new Point(X, Y); }
        }
		[XmlIgnore]public Point TopRight{
            get { return new Point(Right, Y); }
        }
		[XmlIgnore]public Point BottomLeft{
            get { return new Point(X, Bottom); }
        }
		[XmlIgnore]public Point BottomRight{
            get { return new Point(Right, Bottom); }
        }
		[XmlIgnore]public Point Center
        {
            get { return new Point(Left + Width / 2, Top + Height / 2); }
        }
		#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 bool ContainsOrIsEqual(Point p)
        {
            return (p.X >= X && p.X <= X + Width && p.Y >= Y && p.Y <= Y + Height) ?
                true : false;
        }
        public bool ContainsOrIsEqual(Rectangle r)
        {
            return r.TopLeft >= this.TopLeft && r.BottomRight <= this.BottomRight ? true : false;
        }
        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) ?
				true : false;
        }
        public Rectangle Intersection(Rectangle r)
        {
            Rectangle result = new Rectangle();
            
            if (r.Left >= this.Left)
                result.Left = r.Left;
            else
                result.TopLeft = this.TopLeft;

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

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

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

            return result;
        }
		#endregion

        #region operators
        public static implicit operator Rectangle(System.Drawing.Rectangle r)
        {
            return new Rectangle(r.X, r.Y, r.Width, r.Height);
        }
        public static implicit operator System.Drawing.Rectangle(Rectangle r)
        {
            return new System.Drawing.Rectangle(r.X, r.Y, r.Width, r.Height);
        }
        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)
		{
			return new Rectangle(r.X + p.X, r.Y + p.Y, r.Width, r.Height);
		}
		public static Rectangle operator -(Rectangle r, Point p)
		{
			return new Rectangle(r.X - p.X, r.Y - p.Y, r.Width, r.Height);
		}
		public static bool operator ==(Rectangle r1, Rectangle r2)
        {
            return r1.TopLeft == r2.TopLeft && r1.Size == r2.Size ? true : false;
        }
        public static bool operator !=(Rectangle r1, Rectangle r2)
        {
            return r1.TopLeft == r2.TopLeft && r1.Size == r2.Size ? false : true;
        }
        #endregion

        public RectanglesRelations test(Rectangle r)
        {
            if (r == this)
                return RectanglesRelations.Equal;

            int nbrPtIncluded = 0;

            if (this.ContainsOrIsEqual(r.TopLeft))
                nbrPtIncluded++;
            if (this.ContainsOrIsEqual(r.TopRight))
                nbrPtIncluded++;
            if (this.ContainsOrIsEqual(r.BottomLeft))
                nbrPtIncluded++;
            if (this.ContainsOrIsEqual(r.BottomRight))
                nbrPtIncluded++;

            switch (nbrPtIncluded)
            {
                case 0:
                    return RectanglesRelations.NoRelation;
                case 4:
                    return RectanglesRelations.Contains;
                default:
                    return RectanglesRelations.Intersect;
            }
        }

		public static readonly Rectangle Zero = new Rectangle(0, 0, 0, 0);
        public static Rectangle Empty
        {
            get { return Zero; }
        }
        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3}", 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]));
        }
		public override int GetHashCode ()
		{
			unchecked // Overflow is fine, just wrap
			{
				int hash = 17;
				// Suitable nullity checks etc, of course :)
				hash = hash * 23 + _x.GetHashCode();
				hash = hash * 23 + _y.GetHashCode();
				hash = hash * 23 + _width.GetHashCode();
				hash = hash * 23 + _height.GetHashCode();
				return hash;
			}
		}
		public override bool Equals (object obj)
		{
			return (obj == null || obj.GetType() != typeof(Rectangle)) ?
				false :
				this == (Rectangle)obj;
		}
    }
}