~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/Rectangle.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// Rectangle.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Collections;
 
29
using System.Globalization;
 
30
 
 
31
namespace Xwt
 
32
{
 
33
        [Serializable]
 
34
        public struct Rectangle
 
35
        {
 
36
                public double X { get; set; }
 
37
                public double Y { get; set; }
 
38
                public double Width { get; set; }
 
39
                public double Height { get; set; }
 
40
 
 
41
                public static Rectangle Zero = new Rectangle ();
 
42
                
 
43
                public override string ToString ()
 
44
                {
 
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));
 
46
                }
 
47
                
 
48
                // constructors
 
49
                public Rectangle (double x, double y, double width, double height): this ()
 
50
                {
 
51
                        X = x;
 
52
                        Y = y;
 
53
                        Width = width;
 
54
                        Height = height;
 
55
                }
 
56
                
 
57
                public Rectangle (Point loc, Size sz) : this (loc.X, loc.Y, sz.Width, sz.Height) {}
 
58
                
 
59
                public static Rectangle FromLTRB (double left, double top, double right, double bottom)
 
60
                {
 
61
                        return new Rectangle (left, top, right - left, bottom - top);
 
62
                }
 
63
                
 
64
                // Equality
 
65
                public override bool Equals (object o)
 
66
                {
 
67
                        if (!(o is Rectangle))
 
68
                                return false;
 
69
                
 
70
                        return (this == (Rectangle) o);
 
71
                }
 
72
                
 
73
                public override int GetHashCode ()
 
74
                {
 
75
                        return ((int)Height + (int)Width) ^ (int)X + (int)Y;
 
76
                }
 
77
                
 
78
                public static bool operator == (Rectangle r1, Rectangle r2)
 
79
                {
 
80
                        return ((r1.Location == r2.Location) && (r1.Size == r2.Size));
 
81
                }
 
82
                
 
83
                public static bool operator != (Rectangle r1, Rectangle r2)
 
84
                {
 
85
                        return !(r1 == r2);
 
86
                }
 
87
                
 
88
                // Hit Testing / Intersection / Union
 
89
                public bool Contains (Rectangle rect)
 
90
                {
 
91
                        return (rect == Intersect (this, rect));
 
92
                }
 
93
                
 
94
                public bool Contains (Point pt)
 
95
                {
 
96
                        return Contains (pt.X, pt.Y);
 
97
                }
 
98
                
 
99
                public bool Contains (double x, double y)
 
100
                {
 
101
                        return ((x >= Left) && (x < Right) && 
 
102
                                (y >= Top) && (y < Bottom));
 
103
                }
 
104
                
 
105
                public bool IntersectsWith (Rectangle r)
 
106
                {
 
107
                        return !((Left >= r.Right) || (Right <= r.Left) ||
 
108
                                        (Top >= r.Bottom) || (Bottom <= r.Top));
 
109
                }
 
110
                
 
111
                public Rectangle Union (Rectangle r)
 
112
                {
 
113
                        return Union (this, r);
 
114
                }
 
115
                
 
116
                public static Rectangle Union (Rectangle r1, Rectangle r2)
 
117
                {
 
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));
 
122
                }
 
123
                
 
124
                public Rectangle Intersect (Rectangle r)
 
125
                {
 
126
                        return Intersect (this, r);
 
127
                }
 
128
 
 
129
                public static Rectangle Intersect (Rectangle r1, Rectangle r2)
 
130
                {
 
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;
 
135
 
 
136
                        if (width < 0 || height < 0) 
 
137
                        {
 
138
                                return Rectangle.Zero;
 
139
                        }
 
140
                        return new Rectangle (x, y, width, height);
 
141
                }
 
142
                
 
143
                // Position/Size
 
144
                public double Top {
 
145
                        get { return Y; }
 
146
                        set { Y = value; }
 
147
                }
 
148
                public double Bottom {
 
149
                        get { return Y + Height; }
 
150
                        set { Height = value - Y; }
 
151
                }
 
152
                public double Right {
 
153
                        get { return X + Width; }
 
154
                        set { Width = value - X; }
 
155
                }
 
156
                public double Left {
 
157
                        get { return X; }
 
158
                        set { X = value; }
 
159
                }
 
160
                
 
161
                public bool IsEmpty {
 
162
                        get { return (Width <= 0) || (Height <= 0); }
 
163
                }
 
164
                
 
165
                public Size Size {
 
166
                        get {
 
167
                                return new Size (Width, Height);
 
168
                        }
 
169
                        set {
 
170
                                Width = value.Width;
 
171
                                Height = value.Height;
 
172
                        }
 
173
                }
 
174
                
 
175
                public Point Location {
 
176
                        get {
 
177
                                return new Point (X, Y);
 
178
                        }
 
179
                        set {
 
180
                                X = value.X;
 
181
                                Y = value.Y;
 
182
                        }
 
183
                }
 
184
                
 
185
                public Point Center {
 
186
                        get {
 
187
                                return new Point (X + Width / 2, Y + Height / 2);
 
188
                        }
 
189
                }
 
190
                
 
191
                // Inflate and Offset
 
192
                public Rectangle Inflate (Size sz)
 
193
                {
 
194
                        return Inflate (sz.Width, sz.Height);
 
195
                }
 
196
                
 
197
                public Rectangle Inflate (double width, double height)
 
198
                {
 
199
                        Rectangle r = this;
 
200
                        r.X -= width;
 
201
                        r.Y -= height;
 
202
                        r.Width += width * 2;
 
203
                        r.Height += height * 2;
 
204
                        return r;
 
205
                }
 
206
                
 
207
                public Rectangle Offset (double dx, double dy)
 
208
                {
 
209
                        Rectangle r = this;
 
210
                        r.X += dx;
 
211
                        r.Y += dy;
 
212
                        return r;
 
213
                }
 
214
                
 
215
                public Rectangle Offset (Point dr)
 
216
                {
 
217
                        return Offset (dr.X, dr.Y);
 
218
                }
 
219
 
 
220
                public Rectangle Round ()
 
221
                {
 
222
                        return new Rectangle (
 
223
                                Math.Round (X),
 
224
                                Math.Round (Y),
 
225
                                Math.Round (Width),
 
226
                                Math.Round (Height)
 
227
                        );
 
228
                }
 
229
        }
 
230
}