summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Rectangle.cs
blob: ac5460cb37228fd20c466a4f6e1e1d301d2687dc (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
// 
// Rectangle.cs
//  
// Author:
//       Lluis Sanchez <lluis@xamarin.com>
// 
// Copyright (c) 2011 Xamarin Inc
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Diagnostics;
using System.Globalization;

namespace Xamarin.Forms
{
	[DebuggerDisplay("X={X}, Y={Y}, Width={Width}, Height={Height}")]
	[TypeConverter(typeof(RectangleTypeConverter))]
	public struct Rectangle
	{
		public double X { get; set; }

		public double Y { get; set; }

		public double Width { get; set; }

		public double Height { get; set; }

		public static Rectangle Zero = new Rectangle();

		public override string ToString()
		{
			return string.Format("{{X={0} Y={1} Width={2} Height={3}}}", X.ToString(CultureInfo.InvariantCulture), Y.ToString(CultureInfo.InvariantCulture), Width.ToString(CultureInfo.InvariantCulture),
				Height.ToString(CultureInfo.InvariantCulture));
		}

		// constructors
		public Rectangle(double x, double y, double width, double height) : this()
		{
			X = x;
			Y = y;
			Width = width;
			Height = height;
		}

		public Rectangle(Point loc, Size sz) : this(loc.X, loc.Y, sz.Width, sz.Height)
		{
		}

		public static Rectangle FromLTRB(double left, double top, double right, double bottom)
		{
			return new Rectangle(left, top, right - left, bottom - top);
		}

		public bool Equals(Rectangle other)
		{
			return X.Equals(other.X) && Y.Equals(other.Y) && Width.Equals(other.Width) && Height.Equals(other.Height);
		}

		public override bool Equals(object obj)
		{
			if (ReferenceEquals(null, obj))
				return false;
			return obj is Rectangle && Equals((Rectangle)obj);
		}

		public override int GetHashCode()
		{
			unchecked
			{
				int hashCode = X.GetHashCode();
				hashCode = (hashCode * 397) ^ Y.GetHashCode();
				hashCode = (hashCode * 397) ^ Width.GetHashCode();
				hashCode = (hashCode * 397) ^ Height.GetHashCode();
				return hashCode;
			}
		}

		public static bool operator ==(Rectangle r1, Rectangle r2)
		{
			return (r1.Location == r2.Location) && (r1.Size == r2.Size);
		}

		public static bool operator !=(Rectangle r1, Rectangle r2)
		{
			return !(r1 == r2);
		}

		// Hit Testing / Intersection / Union
		public bool Contains(Rectangle rect)
		{
			return X <= rect.X && Right >= rect.Right && Y <= rect.Y && Bottom >= rect.Bottom;
		}

		public bool Contains(Point pt)
		{
			return Contains(pt.X, pt.Y);
		}

		public bool Contains(double x, double y)
		{
			return (x >= Left) && (x < Right) && (y >= Top) && (y < Bottom);
		}

		public bool IntersectsWith(Rectangle r)
		{
			return !((Left >= r.Right) || (Right <= r.Left) || (Top >= r.Bottom) || (Bottom <= r.Top));
		}

		public Rectangle Union(Rectangle r)
		{
			return Union(this, r);
		}

		public static Rectangle Union(Rectangle r1, Rectangle r2)
		{
			return FromLTRB(Math.Min(r1.Left, r2.Left), Math.Min(r1.Top, r2.Top), Math.Max(r1.Right, r2.Right), Math.Max(r1.Bottom, r2.Bottom));
		}

		public Rectangle Intersect(Rectangle r)
		{
			return Intersect(this, r);
		}

		public static Rectangle Intersect(Rectangle r1, Rectangle r2)
		{
			double x = Math.Max(r1.X, r2.X);
			double y = Math.Max(r1.Y, r2.Y);
			double width = Math.Min(r1.Right, r2.Right) - x;
			double height = Math.Min(r1.Bottom, r2.Bottom) - y;

			if (width < 0 || height < 0)
			{
				return Zero;
			}
			return new Rectangle(x, y, width, height);
		}

		// Position/Size
		public double Top
		{
			get { return Y; }
			set { Y = value; }
		}

		public double Bottom
		{
			get { return Y + Height; }
			set { Height = value - Y; }
		}

		public double Right
		{
			get { return X + Width; }
			set { Width = value - X; }
		}

		public double Left
		{
			get { return X; }
			set { X = value; }
		}

		public bool IsEmpty
		{
			get { return (Width <= 0) || (Height <= 0); }
		}

		public Size Size
		{
			get { return new Size(Width, Height); }
			set
			{
				Width = value.Width;
				Height = value.Height;
			}
		}

		public Point Location
		{
			get { return new Point(X, Y); }
			set
			{
				X = value.X;
				Y = value.Y;
			}
		}

		public Point Center
		{
			get { return new Point(X + Width / 2, Y + Height / 2); }
		}

		// Inflate and Offset
		public Rectangle Inflate(Size sz)
		{
			return Inflate(sz.Width, sz.Height);
		}

		public Rectangle Inflate(double width, double height)
		{
			Rectangle r = this;
			r.X -= width;
			r.Y -= height;
			r.Width += width * 2;
			r.Height += height * 2;
			return r;
		}

		public Rectangle Offset(double dx, double dy)
		{
			Rectangle r = this;
			r.X += dx;
			r.Y += dy;
			return r;
		}

		public Rectangle Offset(Point dr)
		{
			return Offset(dr.X, dr.Y);
		}

		public Rectangle Round()
		{
			return new Rectangle(Math.Round(X), Math.Round(Y), Math.Round(Width), Math.Round(Height));
		}
	}
}