Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2287 - 1
 
2
namespace GMap.NET
3
{
4
   using System;
5
   using System.Globalization;
6
 
7
   /// <summary>
8
   /// the rect
9
   /// </summary>
10
   public struct GRect
11
   {
12
      public static readonly GRect Empty = new GRect();
13
 
14
      private long x;
15
      private long y;
16
      private long width;
17
      private long height;
18
 
19
      public GRect(long x, long y, long width, long height)
20
      {
21
         this.x = x;
22
         this.y = y;
23
         this.width = width;
24
         this.height = height;
25
      }
26
 
27
      public GRect(GPoint location, GSize size)
28
      {
29
         this.x = location.X;
30
         this.y = location.Y;
31
         this.width = size.Width;
32
         this.height = size.Height;
33
      }
34
 
35
      public static GRect FromLTRB(int left, int top, int right, int bottom)
36
      {
37
         return new GRect(left,
38
                              top,
39
                              right - left,
40
                              bottom - top);
41
      }
42
 
43
      public GPoint Location
44
      {
45
         get
46
         {
47
            return new GPoint(X, Y);
48
         }
49
         set
50
         {
51
            X = value.X;
52
            Y = value.Y;
53
         }
54
      }
55
 
56
      public GPoint RightBottom
57
      {
58
         get
59
         {
60
            return new GPoint(Right, Bottom);
61
         }
62
      }
63
 
64
      public GPoint RightTop
65
      {
66
         get
67
         {
68
            return new GPoint(Right, Top);
69
         }
70
      }
71
 
72
      public GPoint LeftBottom
73
      {
74
         get
75
         {
76
            return new GPoint(Left, Bottom);
77
         }
78
      }
79
 
80
      public GSize Size
81
      {
82
         get
83
         {
84
            return new GSize(Width, Height);
85
         }
86
         set
87
         {
88
            this.Width = value.Width;
89
            this.Height = value.Height;
90
         }
91
      }
92
 
93
      public long X
94
      {
95
         get
96
         {
97
            return x;
98
         }
99
         set
100
         {
101
            x = value;
102
         }
103
      }
104
 
105
      public long Y
106
      {
107
         get
108
         {
109
            return y;
110
         }
111
         set
112
         {
113
            y = value;
114
         }
115
      }
116
 
117
      public long Width
118
      {
119
         get
120
         {
121
            return width;
122
         }
123
         set
124
         {
125
            width = value;
126
         }
127
      }
128
 
129
      public long Height
130
      {
131
         get
132
         {
133
            return height;
134
         }
135
         set
136
         {
137
            height = value;
138
         }
139
      }
140
 
141
      public long Left
142
      {
143
         get
144
         {
145
            return X;
146
         }
147
      }
148
 
149
      public long Top
150
      {
151
         get
152
         {
153
            return Y;
154
         }
155
      }
156
 
157
      public long Right
158
      {
159
         get
160
         {
161
            return X + Width;
162
         }
163
      }
164
 
165
      public long Bottom
166
      {
167
         get
168
         {
169
            return Y + Height;
170
         }
171
      }
172
 
173
      public bool IsEmpty
174
      {
175
         get
176
         {
177
            return height == 0 && width == 0 && x == 0 && y == 0;
178
         }
179
      }
180
 
181
      public override bool Equals(object obj)
182
      {
183
         if(!(obj is GRect))
184
            return false;
185
 
186
         GRect comp = (GRect) obj;
187
 
188
         return (comp.X == this.X) &&
189
            (comp.Y == this.Y) &&
190
            (comp.Width == this.Width) &&
191
            (comp.Height == this.Height);
192
      }
193
 
194
      public static bool operator==(GRect left, GRect right)
195
      {
196
         return (left.X == right.X
197
                    && left.Y == right.Y
198
                    && left.Width == right.Width
199
                    && left.Height == right.Height);
200
      }
201
 
202
      public static bool operator!=(GRect left, GRect right)
203
      {
204
         return !(left == right);
205
      }
206
 
207
      public bool Contains(long x, long y)
208
      {
209
         return this.X <= x &&
210
            x < this.X + this.Width &&
211
            this.Y <= y &&
212
            y < this.Y + this.Height;
213
      }
214
 
215
      public bool Contains(GPoint pt)
216
      {
217
         return Contains(pt.X, pt.Y);
218
      }
219
 
220
      public bool Contains(GRect rect)
221
      {
222
         return (this.X <= rect.X) &&
223
            ((rect.X + rect.Width) <= (this.X + this.Width)) &&
224
            (this.Y <= rect.Y) &&
225
            ((rect.Y + rect.Height) <= (this.Y + this.Height));
226
      }
227
 
228
      public override int GetHashCode()
229
      {
230
         if(this.IsEmpty)
231
         {
232
            return 0;
233
         }
234
         return (int)(((this.X ^ ((this.Y << 13) | (this.Y >> 0x13))) ^ ((this.Width << 0x1a) | (this.Width >> 6))) ^ ((this.Height << 7) | (this.Height >> 0x19)));
235
      }
236
 
237
      public void Inflate(long width, long height)
238
      {
239
         this.X -= width;
240
         this.Y -= height;
241
         this.Width += 2*width;
242
         this.Height += 2*height;
243
      }
244
 
245
      public void Inflate(GSize size)
246
      {    
247
         Inflate(size.Width, size.Height);
248
      }
249
 
250
      public static GRect Inflate(GRect rect, long x, long y)
251
      {
252
         GRect r = rect;
253
         r.Inflate(x, y);
254
         return r;
255
      }
256
 
257
      public void Intersect(GRect rect)
258
      {
259
         GRect result = GRect.Intersect(rect, this);
260
 
261
         this.X = result.X;
262
         this.Y = result.Y;
263
         this.Width = result.Width;
264
         this.Height = result.Height;
265
      }
266
 
267
      public static GRect Intersect(GRect a, GRect b)
268
      {
269
         long x1 = Math.Max(a.X, b.X);
270
         long x2 = Math.Min(a.X + a.Width, b.X + b.Width);
271
         long y1 = Math.Max(a.Y, b.Y);
272
         long y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);
273
 
274
         if(x2 >= x1
275
                && y2 >= y1)
276
         {
277
 
278
            return new GRect(x1, y1, x2 - x1, y2 - y1);
279
         }
280
         return GRect.Empty;
281
      }
282
 
283
      public bool IntersectsWith(GRect rect)
284
      {
285
         return (rect.X < this.X + this.Width) &&
286
            (this.X < (rect.X + rect.Width)) &&
287
            (rect.Y < this.Y + this.Height) &&
288
            (this.Y < rect.Y + rect.Height);
289
      }
290
 
291
      public static GRect Union(GRect a, GRect b)
292
      {
293
         long x1 = Math.Min(a.X, b.X);
294
         long x2 = Math.Max(a.X + a.Width, b.X + b.Width);
295
         long y1 = Math.Min(a.Y, b.Y);
296
         long y2 = Math.Max(a.Y + a.Height, b.Y + b.Height);
297
 
298
         return new GRect(x1, y1, x2 - x1, y2 - y1);
299
      }
300
 
301
      public void Offset(GPoint pos)
302
      {
303
         Offset(pos.X, pos.Y);
304
      }
305
 
306
      public void OffsetNegative(GPoint pos)
307
      {
308
         Offset(-pos.X, -pos.Y);
309
      }
310
 
311
      public void Offset(long x, long y)
312
      {
313
         this.X += x;
314
         this.Y += y;
315
      }
316
 
317
      public override string ToString()
318
      {
319
         return "{X=" + X.ToString(CultureInfo.CurrentCulture) + ",Y=" + Y.ToString(CultureInfo.CurrentCulture) +
320
            ",Width=" + Width.ToString(CultureInfo.CurrentCulture) +
321
            ",Height=" + Height.ToString(CultureInfo.CurrentCulture) + "}";
322
      }
323
   }
324
}