5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections;
29
using System.Globalization;
34
public struct Rectangle
36
public double X { get; set; }
37
public double Y { get; set; }
38
public double Width { get; set; }
39
public double Height { get; set; }
41
public static Rectangle Zero = new Rectangle ();
43
public override string ToString ()
45
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));
49
public Rectangle (double x, double y, double width, double height): this ()
57
public Rectangle (Point loc, Size sz) : this (loc.X, loc.Y, sz.Width, sz.Height) {}
59
public static Rectangle FromLTRB (double left, double top, double right, double bottom)
61
return new Rectangle (left, top, right - left, bottom - top);
65
public override bool Equals (object o)
67
if (!(o is Rectangle))
70
return (this == (Rectangle) o);
73
public override int GetHashCode ()
75
return ((int)Height + (int)Width) ^ (int)X + (int)Y;
78
public static bool operator == (Rectangle r1, Rectangle r2)
80
return ((r1.Location == r2.Location) && (r1.Size == r2.Size));
83
public static bool operator != (Rectangle r1, Rectangle r2)
88
// Hit Testing / Intersection / Union
89
public bool Contains (Rectangle rect)
91
return (rect == Intersect (this, rect));
94
public bool Contains (Point pt)
96
return Contains (pt.X, pt.Y);
99
public bool Contains (double x, double y)
101
return ((x >= Left) && (x < Right) &&
102
(y >= Top) && (y < Bottom));
105
public bool IntersectsWith (Rectangle r)
107
return !((Left >= r.Right) || (Right <= r.Left) ||
108
(Top >= r.Bottom) || (Bottom <= r.Top));
111
public Rectangle Union (Rectangle r)
113
return Union (this, r);
116
public static Rectangle Union (Rectangle r1, Rectangle r2)
118
return FromLTRB (Math.Min (r1.Left, r2.Left),
119
Math.Min (r1.Top, r2.Top),
120
Math.Max (r1.Right, r2.Right),
121
Math.Max (r1.Bottom, r2.Bottom));
124
public Rectangle Intersect (Rectangle r)
126
return Intersect (this, r);
129
public static Rectangle Intersect (Rectangle r1, Rectangle r2)
131
var x = Math.Max (r1.X, r2.X);
132
var y = Math.Max (r1.Y, r2.Y);
133
var width = Math.Min (r1.Right, r2.Right) - x;
134
var height = Math.Min (r1.Bottom, r2.Bottom) - y;
136
if (width < 0 || height < 0)
138
return Rectangle.Zero;
140
return new Rectangle (x, y, width, height);
148
public double Bottom {
149
get { return Y + Height; }
150
set { Height = value - Y; }
152
public double Right {
153
get { return X + Width; }
154
set { Width = value - X; }
161
public bool IsEmpty {
162
get { return (Width <= 0) || (Height <= 0); }
167
return new Size (Width, Height);
171
Height = value.Height;
175
public Point Location {
177
return new Point (X, Y);
185
public Point Center {
187
return new Point (X + Width / 2, Y + Height / 2);
191
// Inflate and Offset
192
public Rectangle Inflate (Size sz)
194
return Inflate (sz.Width, sz.Height);
197
public Rectangle Inflate (double width, double height)
202
r.Width += width * 2;
203
r.Height += height * 2;
207
public Rectangle Offset (double dx, double dy)
215
public Rectangle Offset (Point dr)
217
return Offset (dr.X, dr.Y);
220
public Rectangle Round ()
222
return new Rectangle (