-
Notifications
You must be signed in to change notification settings - Fork 4
/
RectangleFHelper.cs
426 lines (382 loc) · 17.8 KB
/
RectangleFHelper.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using GeoFramework.Projections;
namespace GeoFramework
{
/// <summary>
/// Provides additional functionality for the RectangleF structure.
/// </summary>
public static class RectangleFHelper
{
/// <summary>
/// Returns the point at the center of the specified rectangle.
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public static PointF Center(RectangleF rectangle)
{
return new PointF((rectangle.Width * .5f) + rectangle.X, (rectangle.Height * .5f) + rectangle.Y);
}
/// <summary>
/// Increases the height or broadens the width of a rectangle to match the specified aspect ratio.
/// </summary>
/// <param name="rectangle"></param>
/// <param name="aspectRatio"></param>
/// <returns></returns>
public static RectangleF ToAspectRatio(RectangleF rectangle, float aspectRatio)
{
float projectedAspect = ((float)rectangle.Width / (float)rectangle.Height);
if (aspectRatio > projectedAspect)
{
#if !PocketPC
rectangle.Inflate((aspectRatio * rectangle.Height - rectangle.Width) * .5f, 0);
#else
return RectangleFHelper.Inflate(rectangle, (aspectRatio * rectangle.Height - rectangle.Width) * .5f, 0);
#endif
}
else if (aspectRatio < projectedAspect)
{
#if !PocketPC
rectangle.Inflate(0, (rectangle.Width / aspectRatio - rectangle.Height) * .5f);
#else
return RectangleFHelper.Inflate(rectangle, 0, (rectangle.Width / aspectRatio - rectangle.Height) * .5f);
#endif
}
return rectangle;
}
/// <summary>
/// Shortens the height or narrows the width of a rectangle to match the specified aspect ratio.
/// </summary>
/// <param name="rectangle"></param>
/// <param name="aspectRatio"></param>
/// <returns></returns>
public static RectangleF ToAspectRatioB(RectangleF rectangle, float aspectRatio)
{
float projectedAspect = ((float)rectangle.Width / (float)rectangle.Height);
if (aspectRatio > projectedAspect)
{
#if !PocketPC
rectangle.Inflate(0, (rectangle.Width / aspectRatio - rectangle.Height) * .5f);
#else
return RectangleFHelper.Inflate(rectangle, 0, (rectangle.Width / aspectRatio - rectangle.Height) * .5f);
#endif
}
else if (aspectRatio < projectedAspect)
{
#if !PocketPC
rectangle.Inflate((aspectRatio * rectangle.Height - rectangle.Width) * .5f, 0);
#else
return RectangleFHelper.Inflate(rectangle, (aspectRatio * rectangle.Height - rectangle.Width) * .5f, 0);
#endif
}
return rectangle;
}
/// <summary>
/// Returns the corners of a rectangle
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public static PointF[] Corners(RectangleF rectangle)
{
return new PointF[4]
{
new PointF(rectangle.Left, rectangle.Top),
new PointF(rectangle.Right, rectangle.Top),
new PointF(rectangle.Right, rectangle.Bottom),
new PointF(rectangle.Left, rectangle.Bottom)
};
}
/// <summary>
/// Calculates the bounding rectangle for the supplied points.
/// </summary>
/// <returns></returns>
public static RectangleF ComputeBoundingBox(PointF[] projectedPoints)
{
// ffs
if (projectedPoints.Length == 0) return RectangleF.Empty;
// Now figure out which points represent the maximum bounds, starting
// with the first point
float projectedLeft = projectedPoints[0].X;
float projectedRight = projectedPoints[0].X;
float projectedTop = projectedPoints[0].Y;
float projectedBottom = projectedPoints[0].Y;
// Now consider all other points
int Limit = projectedPoints.Length;
for (int index = 1; index < Limit; index++)
{
// Get the current projected point
PointF projectedPoint = projectedPoints[index];
// Now see if it exceeds the current bounds
if (projectedPoint.X < projectedLeft)
projectedLeft = projectedPoint.X;
if (projectedPoint.X > projectedRight)
projectedRight = projectedPoint.X;
if (projectedPoint.Y < projectedTop)
projectedTop = projectedPoint.Y;
if (projectedPoint.Y > projectedBottom)
projectedBottom = projectedPoint.Y;
}
// finally, return a rectangle with these bounds
#if PocketPC
return RectangleFHelper.FromLTRB(projectedLeft, projectedTop, projectedRight, projectedBottom);
#else
return RectangleF.FromLTRB(projectedLeft, projectedTop, projectedRight, projectedBottom);
#endif
}
/// <summary>
/// Returns the length of the hypotenuse of the specified rectangle.
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public static float Hypotenuse(RectangleF rectangle)
{
return (float)Math.Sqrt(Math.Pow(rectangle.Width, 2) + Math.Pow(rectangle.Height, 2));
}
/// <summary>
/// Centers the rectangle on a specific point.
/// </summary>
/// <param name="rectangle">The rectangle to translate</param>
/// <param name="point">The point on which to center the reactangle</param>
/// <returns>The new rectangle centered on the specified point</returns>
public static RectangleF CenterOn(RectangleF rectangle, PointF point)
{
PointF center = RectangleFHelper.Center(rectangle);
#if !PocketPC
rectangle.Offset(point.X - center.X, point.Y - center.Y);
return rectangle;
#else
return RectangleFHelper.Offset(rectangle, point.X - center.X, point.Y - center.Y);
#endif
}
/// <summary>
/// Returns whether any one of a rectangle's sides is a NaN (not a number).
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public static bool IsNaN(RectangleF rectangle)
{
return double.IsNaN(rectangle.X * rectangle.Y * rectangle.Right * rectangle.Bottom);
}
/// <summary>
/// Rotates a rectangle around its center
/// </summary>
/// <param name="rectangle">The rectangle to apply the rotation</param>
/// <param name="angle">The clockwise angle of the rotation</param>
/// <returns>The minimum bounding rectangle (MBR) of the rotated rectangle</returns>
public static RectangleF Rotate(RectangleF rectangle, Angle angle)
{
return RectangleFHelper.RotateAt(rectangle, (float)angle.DecimalDegrees, RectangleFHelper.Center(rectangle));
}
/// <summary>
/// Rotates a rectangle around its center
/// </summary>
/// <param name="rectangle">The rectangle to apply the rotation</param>
/// <param name="angle">The clockwise angle of the rotation</param>
/// <returns>The minimum bounding rectangle (MBR) of the rotated rectangle</returns>
public static RectangleF Rotate(RectangleF rectangle, float angle)
{
return RectangleFHelper.RotateAt(rectangle, angle, RectangleFHelper.Center(rectangle));
}
/// <summary>
/// Rotates a rectangle around a coordinate
/// </summary>
/// <param name="rectangle">The rectangle to apply the rotation</param>
/// <param name="angle">The clockwise angle of the rotation</param>
/// <returns>The minimum bounding rectangle (MBR) of the rotated rectangle</returns>
public static RectangleF RotateAt(RectangleF rectangle, Angle angle, PointF center)
{
return RectangleFHelper.RotateAt(rectangle, (float)angle.DecimalDegrees, center);
}
/// <summary>
/// Rotates a rectangle around a coordinate
/// </summary>
/// <param name="rectangle">The rectangle to apply the rotation</param>
/// <param name="angle">The clockwise angle of the rotation</param>
/// <returns>The minimum bounding rectangle (MBR) of the rotated rectangle</returns>
public static RectangleF RotateAt(RectangleF rectangle, float angle, PointF center)
{
// The graphics transform method only accepts arrays :P
PointF[] points = new PointF[4]
{
new PointF(rectangle.Left, rectangle.Top),
new PointF(rectangle.Right, rectangle.Top),
new PointF(rectangle.Right, rectangle.Bottom),
new PointF(rectangle.Left, rectangle.Bottom)
};
Matrix rotationMatrix = new Matrix();
rotationMatrix.RotateAt((float)angle, center);
rotationMatrix.TransformPoints(points);
rotationMatrix.Dispose();
// Return the result
return RectangleFHelper.ComputeBoundingBox(points);
}
/// <summary>
/// Rotates a coordinate around a coordinate
/// </summary>
/// <param name="point">The point to apply the rotation</param>
/// <param name="angle">The clockwise angle of the rotation</param>
/// <returns>The rectangle resulting from the rotation of the upperleft and lower rightt corners of the input rectangle</returns>
public static PointF RotatePointF(PointF point, Angle angle, PointF center)
{
return RectangleFHelper.RotatePointF(point, (float)angle.DecimalDegrees, center);
}
/// <summary>
/// Rotates a coordinate around a coordinate
/// </summary>
/// <param name="point">The point to apply the rotation</param>
/// <param name="angle">The clockwise angle of the rotation</param>
/// <returns>The rectangle resulting from the rotation of the upperleft and lower rightt corners of the input rectangle</returns>
public static PointF RotatePointF(PointF point, float angle, PointF center)
{
// The graphics transform method only accepts arrays :P
PointF[] points = new PointF[1]
{
point,
};
Matrix rotationMatrix = new Matrix();
rotationMatrix.RotateAt((float)angle, center);
rotationMatrix.TransformPoints(points);
rotationMatrix.Dispose();
// Return the result
return (points[0]);
}
public static bool IsRectangle(PointF[] points)
{
if (points.Length != 4) return false;
// The rectangle could be rotated, we need to check 2 orientations
return (
(
points[0].X == points[3].X &&
points[0].Y == points[1].Y &&
points[0].X != points[2].X &&
points[0].Y != points[2].Y
) ||
(
points[0].X == points[1].X &&
points[0].Y == points[3].Y &&
points[0].X != points[2].X &&
points[0].Y != points[2].Y
)
);
}
#if PocketPC
/// <summary>
/// Compares the imput rectangles and caculates the portion of the new rctangle not included in the old.
/// </summary>
/// <param name="current"> The current rectangle</param>
/// <param name="previous">The previous rectangle</param>
/// <returns>An array of rectangles describing the difference between the input rectangles.</returns>
/// <remarks>
/// This funtion is a liner exclusive OR on 2 rectangles. It is catagorized by specifying the order of the
/// rectangles in a linear fashion so that the xor'd intersection is directional. A natural XOR intersection
/// would include the portions of both rectangles not found the intersction of the two. A Linear XOR includes
/// only the portion of the current rectangle not found in the intersection of the two.
/// </remarks>
public static RectangleF[] GetRegionScans(RectangleF current, RectangleF previous)
{
// If the extents are equal, or one contains the other, or they're not intersecting there's nothing
// to do. Return the current rectangle.
if (
!current.Equals(previous) &&
!Contains(current, previous) &&
!Contains(previous, current) &&
IntersectsWith(current, previous))
{
// Get the horizontal rectangle, uncovered from a north or south pan
RectangleF h = RectangleFHelper.FromLTRB(
current.Left, //current.Left < previous.Left ? current.Left : previous.Left,
current.Top < previous.Top ? current.Top : previous.Bottom,
current.Right, //current.Left < previous.Left ? previous.Right : current.Right,
current.Top < previous.Top ? previous.Top : current.Bottom
);
// Get the vertical rectangle, uncovered from an east or west pan
RectangleF v = RectangleFHelper.FromLTRB(
current.Left < previous.Left ? current.Left : previous.Right,
current.Top < previous.Top ? previous.Top : current.Top,
current.Left < previous.Left ? previous.Left : current.Right,
current.Top < previous.Top ? current.Bottom : previous.Bottom
);
// Retangles with no width or height are excluded
if ((h.Height <= 0 || h.Width <= 0) && (v.Height <= 0 || v.Width <= 0))
return new RectangleF[] { current };
// Retangles with no width or height are excluded
if (h.Height <= 0 || h.Width <= 0)
return new RectangleF[] { v };
if (v.Height <= 0 || v.Width <= 0)
return new RectangleF[] { h };
return new RectangleF[] { h, v };
}
return new RectangleF[] { current };
}
public static RectangleF FromLTRB(float left, float top, float right, float bottom)
{
return new RectangleF(left, top, right - left, bottom - top);
}
public static RectangleF Intersect(RectangleF left, RectangleF right)
{
if (IntersectsWith(left, right))
{
float l = left.Left > right.Left ? left.Left : right.Left;
float r = left.Right < right.Right ? left.Right : right.Right;
float t = left.Top > right.Top ? left.Top : right.Top;
float b = left.Bottom < right.Bottom ? left.Bottom : right.Bottom;
return FromLTRB(l, t, r, b);
}
else
{
return RectangleF.Empty;
}
}
public static RectangleF Union(RectangleF left, RectangleF right)
{
return FromLTRB(
left.Left < right.Left ? left.Left : right.Left,
left.Top < right.Top ? left.Top : right.Top,
left.Right > right.Right ? left.Right : right.Right,
left.Bottom > right.Bottom ? left.Bottom : right.Bottom);
}
public static RectangleF Offset(RectangleF rectangle, float x, float y)
{
rectangle.X += x;
rectangle.Y += y;
return rectangle;
}
public static RectangleF Inflate(RectangleF rectangle, float x, float y)
{
return RectangleFHelper.FromLTRB(rectangle.X - x, rectangle.Y - y, rectangle.Right + x, rectangle.Bottom + y);
}
public static bool Contains(RectangleF rectangle, PointF point)
{
return
rectangle.Left <= point.X &&
rectangle.Right >= point.X &&
rectangle.Top <= point.Y &&
rectangle.Bottom >= point.Y;
}
public static bool Contains(RectangleF left, RectangleF right)
{
return
left.Left <= right.Left &&
left.Right >= right.Right &&
left.Top <= right.Top &&
left.Bottom >= right.Bottom;
}
public static bool IntersectsWith(RectangleF left, RectangleF right)
{
return
(
(left.Left <= right.Left && left.Right >= right.Left) ||
(left.Left <= right.Right && left.Right >= right.Right) ||
(left.Left <= right.Left && left.Right >= right.Right) ||
(left.Left >= right.Left && left.Right <= right.Right)
) && (
(left.Top <= right.Top && left.Bottom >= right.Top) ||
(left.Top <= right.Bottom && left.Bottom >= right.Bottom) ||
(left.Top <= right.Top && left.Bottom >= right.Bottom) ||
(left.Top >= right.Top && left.Bottom <= right.Bottom)
);
}
#endif
}
}