forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGRect.cs
324 lines (280 loc) · 6.58 KB
/
GRect.cs
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
namespace GMap.NET
{
using System;
using System.Globalization;
/// <summary>
/// the rect
/// </summary>
public struct GRect
{
public static readonly GRect Empty = new GRect();
private long x;
private long y;
private long width;
private long height;
public GRect(long x, long y, long width, long height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public GRect(GPoint location, GSize size)
{
this.x = location.X;
this.y = location.Y;
this.width = size.Width;
this.height = size.Height;
}
public static GRect FromLTRB(int left, int top, int right, int bottom)
{
return new GRect(left,
top,
right - left,
bottom - top);
}
public GPoint Location
{
get
{
return new GPoint(X, Y);
}
set
{
X = value.X;
Y = value.Y;
}
}
public GPoint RightBottom
{
get
{
return new GPoint(Right, Bottom);
}
}
public GPoint RightTop
{
get
{
return new GPoint(Right, Top);
}
}
public GPoint LeftBottom
{
get
{
return new GPoint(Left, Bottom);
}
}
public GSize Size
{
get
{
return new GSize(Width, Height);
}
set
{
this.Width = value.Width;
this.Height = value.Height;
}
}
public long X
{
get
{
return x;
}
set
{
x = value;
}
}
public long Y
{
get
{
return y;
}
set
{
y = value;
}
}
public long Width
{
get
{
return width;
}
set
{
width = value;
}
}
public long Height
{
get
{
return height;
}
set
{
height = value;
}
}
public long Left
{
get
{
return X;
}
}
public long Top
{
get
{
return Y;
}
}
public long Right
{
get
{
return X + Width;
}
}
public long Bottom
{
get
{
return Y + Height;
}
}
public bool IsEmpty
{
get
{
return height == 0 && width == 0 && x == 0 && y == 0;
}
}
public override bool Equals(object obj)
{
if(!(obj is GRect))
return false;
GRect comp = (GRect) obj;
return (comp.X == this.X) &&
(comp.Y == this.Y) &&
(comp.Width == this.Width) &&
(comp.Height == this.Height);
}
public static bool operator==(GRect left, GRect right)
{
return (left.X == right.X
&& left.Y == right.Y
&& left.Width == right.Width
&& left.Height == right.Height);
}
public static bool operator!=(GRect left, GRect right)
{
return !(left == right);
}
public bool Contains(long x, long y)
{
return this.X <= x &&
x < this.X + this.Width &&
this.Y <= y &&
y < this.Y + this.Height;
}
public bool Contains(GPoint pt)
{
return Contains(pt.X, pt.Y);
}
public bool Contains(GRect rect)
{
return (this.X <= rect.X) &&
((rect.X + rect.Width) <= (this.X + this.Width)) &&
(this.Y <= rect.Y) &&
((rect.Y + rect.Height) <= (this.Y + this.Height));
}
public override int GetHashCode()
{
if(this.IsEmpty)
{
return 0;
}
return (int)(((this.X ^ ((this.Y << 13) | (this.Y >> 0x13))) ^ ((this.Width << 0x1a) | (this.Width >> 6))) ^ ((this.Height << 7) | (this.Height >> 0x19)));
}
public void Inflate(long width, long height)
{
this.X -= width;
this.Y -= height;
this.Width += 2*width;
this.Height += 2*height;
}
public void Inflate(GSize size)
{
Inflate(size.Width, size.Height);
}
public static GRect Inflate(GRect rect, long x, long y)
{
GRect r = rect;
r.Inflate(x, y);
return r;
}
public void Intersect(GRect rect)
{
GRect result = GRect.Intersect(rect, this);
this.X = result.X;
this.Y = result.Y;
this.Width = result.Width;
this.Height = result.Height;
}
public static GRect Intersect(GRect a, GRect b)
{
long x1 = Math.Max(a.X, b.X);
long x2 = Math.Min(a.X + a.Width, b.X + b.Width);
long y1 = Math.Max(a.Y, b.Y);
long y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);
if(x2 >= x1
&& y2 >= y1)
{
return new GRect(x1, y1, x2 - x1, y2 - y1);
}
return GRect.Empty;
}
public bool IntersectsWith(GRect rect)
{
return (rect.X < this.X + this.Width) &&
(this.X < (rect.X + rect.Width)) &&
(rect.Y < this.Y + this.Height) &&
(this.Y < rect.Y + rect.Height);
}
public static GRect Union(GRect a, GRect b)
{
long x1 = Math.Min(a.X, b.X);
long x2 = Math.Max(a.X + a.Width, b.X + b.Width);
long y1 = Math.Min(a.Y, b.Y);
long y2 = Math.Max(a.Y + a.Height, b.Y + b.Height);
return new GRect(x1, y1, x2 - x1, y2 - y1);
}
public void Offset(GPoint pos)
{
Offset(pos.X, pos.Y);
}
public void OffsetNegative(GPoint pos)
{
Offset(-pos.X, -pos.Y);
}
public void Offset(long x, long y)
{
this.X += x;
this.Y += y;
}
public override string ToString()
{
return "{X=" + X.ToString(CultureInfo.CurrentCulture) + ",Y=" + Y.ToString(CultureInfo.CurrentCulture) +
",Width=" + Width.ToString(CultureInfo.CurrentCulture) +
",Height=" + Height.ToString(CultureInfo.CurrentCulture) + "}";
}
}
}