-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRectangleD.cs
914 lines (794 loc) · 33.5 KB
/
RectangleD.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
using System;
using System.Drawing;
using System.Globalization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
#if !PocketPC || DesignTime
using System.ComponentModel;
#endif
namespace GeoFramework
{
/// <summary>Represents a highly-precise rectangle.</summary>
/// <remarks>
/// <para>This class functions similar to the <strong>RectangleF</strong> class in the
/// <strong>System.Drawing</strong> namespace, except that it uses
/// double-floating-point precision and is also supported on the Compact Framework
/// edition of the <strong>GeoFramework</strong>.</para>
/// <para>Instances of this class are guaranteed to be thread-safe because it is
/// immutable (its properties can only be changed during constructors).</para>
/// </remarks>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.RectangleDConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=3ed3cdf4fdda3400")]
#endif
public struct RectangleD : IFormattable, IEquatable<RectangleD>, IXmlSerializable
{
private readonly double _Top;
private readonly double _Bottom;
private readonly double _Left;
private readonly double _Right;
#region Fields
/// <summary>
/// Represents a RectangleD having no size.
/// </summary>
public static readonly RectangleD Empty = new RectangleD(0.0, 0.0, 0.0, 0.0);
#endregion
#region Constructors
/// <summary>
/// Creates a new instance using the specified location, width, and height.
/// </summary>
/// <param name="location"></param>
public RectangleD(PointD location, SizeD size)
: this(location.X, location.Y, location.X + size.Width, location.Y + size.Height)
{}
/// <summary>
/// Creates a new instance using the specified location, width, and height.
/// </summary>
/// <param name="location"></param>
/// <param name="width"></param>
/// <param name="height"></param>
public RectangleD(PointD location, double width, double height)
: this(location.X, location.Y, location.X + width, location.Y + height)
{}
/// <summary>
/// Creates a new instance using the specified upper-left and lower-right coordinates.
/// </summary>
public RectangleD(PointD upperLeft, PointD lowerRight)
: this(upperLeft.X, upperLeft.Y, lowerRight.X, lowerRight.Y)
{}
/// <summary>
/// Creates a new instance using the specified latitudes and longitudes.
/// </summary>
public RectangleD(double left, double top, double right, double bottom)
{
_Top = top < bottom ? top : bottom;
_Left = left < right ? left : right;
_Bottom = bottom > top ? bottom : top;
_Right = right > left ? right : left;
}
public RectangleD(string value)
: this(value, CultureInfo.CurrentCulture)
{}
public RectangleD(string value, CultureInfo culture)
{
// Split the string into words
string[] Values = value.Split(culture.TextInfo.ListSeparator.ToCharArray());
// How many words are there?
if (Values.Length == 4)
{
// Extract each item
_Top = double.Parse(Values[0], culture);
_Left = double.Parse(Values[1], culture);
_Bottom = double.Parse(Values[2], culture);
_Right = double.Parse(Values[3], culture);
}
else
{
throw new FormatException(Properties.Resources.RectangleD_InvalidFormat);
}
}
public RectangleD(XmlReader reader)
{
_Top = double.Parse(
reader.GetAttribute("Top"), CultureInfo.InvariantCulture);
_Bottom = double.Parse(
reader.GetAttribute("Bottom"), CultureInfo.InvariantCulture);
_Left = double.Parse(
reader.GetAttribute("Left"), CultureInfo.InvariantCulture);
_Right = double.Parse(
reader.GetAttribute("Right"), CultureInfo.InvariantCulture);
}
#endregion
#region Public Properties
/// <summary>Returns the top side of the rectangle.</summary>
/// <value>A <see cref="Latitude"></see> object marking the southern-most latitude.</value>
public double Top
{
get
{
return _Top;
}
}
/// <summary>Returns the bottom side of the rectangle.</summary>
/// <value>A <see cref="Latitude"></see> object marking the southern-most latitude.</value>
public double Bottom
{
get
{
return _Bottom;
}
}
/// <summary>Returns the left side of the rectangle.</summary>
public double Left
{
get
{
return _Left;
}
}
/// <summary>Returns the right side of the rectangle.</summary>
public double Right
{
get
{
return _Right;
}
}
/// <summary>Returns the top-to-bottom size of the rectangle.</summary>
public double Height
{
get
{
return _Bottom - _Top;
}
}
/// <summary>Returns the left-to-right size of the rectangle.</summary>
public double Width
{
get
{
return _Right - _Left;
}
}
/// <summary>Returns the width and height of the rectangle.</summary>
public SizeD Size
{
get
{
return new SizeD(Width, Height);
}
}
/// <summary>Returns the point at the center of the rectangle.</summary>
public PointD Center
{
get
{
return new PointD(_Left + Width * 0.5, _Top + Height * 0.5);
}
}
/// <summary>Returns the point at the upper-left corner of the rectangle.</summary>
public PointD UpperLeft
{
get
{
return new PointD(_Left, _Top);
}
}
/// <summary>Returns the point at the upper-right corner of the rectangle.</summary>
public PointD UpperRight
{
get
{
return new PointD(_Right, _Top);
}
}
/// <summary>Returns the point at the lower-left corner of the rectangle.</summary>
public PointD LowerLeft
{
get
{
return new PointD(_Left, _Bottom);
}
}
/// <summary>Returns the point at the lower-right corner of the rectangle.</summary>
public PointD LowerRight
{
get
{
return new PointD(_Right, _Bottom);
}
}
/// <summary>Returns the ratio of the rectangle's width to its height.</summary>
/// <remarks>
/// This property returns the ratio of the RectangleDs width to its height (width / height). This
/// property gives an indication of the RectangleD's shape. An aspect ratio of one indicates
/// a square, whereas an aspect ratio of two indicates a RectangleD which is twice as wide as
/// it is high.
/// </remarks>
public double AspectRatio
{
get
{
return Width / Height;
}
}
/// <summary>Indicates if the rectangle has any value.</summary>
public bool IsEmpty
{
get
{
return (_Top == 0 && _Bottom == 0 && _Left == 0 && _Right == 0);
}
}
#endregion
#region Public Methods
/// <summary>
/// Changes the size and shape of the RectangleD to match the aspect ratio of the specified RectangleD.
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
/// <remarks>This method will expand a RectangleD outward, from its center point, until
/// the ratio of its width to its height matches the specified value.</remarks>
public RectangleD ToAspectRatio(SizeD size)
{
// Calculate the aspect ratio
return ToAspectRatio(size.Width / size.Height);
}
/// <summary>
/// Returns whether the specified rectangle is not overlapping the current
/// instance.
/// </summary>
public bool IsDisjointedFrom(RectangleD rectangle)
{
return !IsOverlapping(rectangle);
}
/// <summary>
/// Indicates if the specified RectangleD is entirely within the current RectangleD.
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public bool IsEnclosing(RectangleD rectangle)
{
return !(rectangle.Left < _Left || rectangle.Right > _Right || rectangle.Top < _Top || rectangle.Bottom > _Bottom);
}
public bool IsEnclosing(PointD point)
{
return !(point.X < _Left || point.Y < _Top || point.X > _Right || point.Y > _Bottom);
}
/// <summary>Moves the rectangle so that the specified point is at its center.</summary>
public RectangleD CenterOn(PointD point)
{
return new RectangleD(new PointD(point.X - (Width * 0.5), point.Y - (Height * 0.5)), Size);
}
public RectangleD Inflate(SizeD size)
{
return Inflate(size.Width, size.Height);
}
/// <summary>
/// Returns a copy of the current instance.
/// </summary>
/// <returns></returns>
public RectangleD Clone()
{
return new RectangleD(_Top, _Left, _Bottom, _Right);
}
/// <summary>
/// Expands the edges of the RectangleD to contain the specified position.
/// </summary>
/// <param name="point">A <strong>PointD</strong> object to surround.</param>
/// <returns>A <strong>RectangleD</strong> which contains the specified position.</returns>
/// <remarks>If the specified position is already enclosed, the current instance will be returned.</remarks>
public RectangleD UnionWith(PointD point)
{
// Does the box already contain the position? If so, do nothing
if (IsEnclosing(point)) return this;
// Return the expanded box
return new RectangleD(
point.Y < _Top ? point.Y : _Top,
point.X < _Left ? point.X : _Left,
point.Y > _Bottom ? point.Y : _Bottom,
point.X > _Right ? point.X : _Right);
}
/// <summary>
/// Returns a rectangle enclosing the corner points of the current instance, rotated
/// by the specified amount around a specific point.
/// </summary>
/// <returns>A new <strong>RectangleD</strong> containing the rotated rectangle.</returns>
/// <remarks><para>When a rectangle is rotated, the total width and height it occupies may be larger
/// than the rectangle's own width and height. This method calculates the smallest rectangle
/// which encloses the rotated rectangle.</para>
/// <para>[TODO: Include before and after picture; this is confusing.]</para>
/// </remarks>
public RectangleD RotateAt(Angle angle, PointD center)
{
if (angle.IsEmpty) return this;
// Rotate each corner point
PointD[] Points = new PointD[] {UpperLeft.RotateAt(angle, center),
UpperRight.RotateAt(angle, center),
LowerRight.RotateAt(angle, center),
LowerLeft.RotateAt(angle, center)};
// Now return the smallest rectangle which encloses these points
return RectangleD.FromArray(Points);
}
/// <summary>
/// Returns a rectangle enclosing the corner points of the current instance, rotated
/// by the specified amount.
/// </summary>
public RectangleD Rotate(Angle angle)
{
if (angle.IsEmpty) return this;
// Rotate each corner point
PointD[] Points = new PointD[] {UpperLeft.RotateAt(angle, Center),
UpperRight.RotateAt(angle, Center),
LowerRight.RotateAt(angle, Center),
LowerLeft.RotateAt(angle, Center)};
// Now return the smallest rectangle which encloses these points
return RectangleD.FromArray(Points);
}
/// <summary>Returns the corner points of the rectangle as an array.</summary>
public PointD[] ToArray()
{
return new PointD[] { UpperLeft, UpperRight, LowerRight, LowerLeft, UpperLeft };
}
/// <summary>
/// Changes the size and shape of the RectangleD to match the specified aspect ratio.
/// </summary>
/// <param name="aspectRatio"></param>
/// <returns></returns>
/// <remarks>This method will expand a RectangleD outward, from its center point, until
/// the ratio of its width to its height matches the specified value.</remarks>
public RectangleD ToAspectRatio(double aspectRatio)
{
double CurrentAspect = AspectRatio;
// Do the values already match?
if (CurrentAspect == aspectRatio) return this;
// Is the new ratio higher or lower?
if (aspectRatio > CurrentAspect)
{
// Inflate the RectangleD to the new height minus the current height
// TESTS OK
return Inflate(aspectRatio * Height - Width, 0);
}
else
{
// Inflate the RectangleD to the new height minus the current height
return Inflate(0, Width / aspectRatio - Height);
}
}
public RectangleD Translate(PointD offset)
{
return new RectangleD(UpperLeft.Add(offset), Size);
}
public RectangleD Translate(double offsetX, double offsetY)
{
return new RectangleD(UpperLeft.Add(offsetX, offsetY), Size);
}
/// <summary>
/// Changes the size and shape of the RectangleD to match the aspect ratio of the specified RectangleD.
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
/// <remarks>This method will expand a RectangleD outward, from its center point, until
/// the ratio of its width to its height matches the specified value.</remarks>
public RectangleD ToAspectRatio(RectangleD rectangle)
{
// Calculate the aspect ratio
return ToAspectRatio(rectangle.Width / rectangle.Height);
}
public RectangleD Inflate(double widthOffset, double heightOffset)
{
widthOffset *= .5;
heightOffset *= .5;
double top = this._Top - heightOffset;
double bottom = this._Bottom + heightOffset;
double left = this._Left - widthOffset;
double right = this._Right + widthOffset;
if (top > bottom || right < left)
return this;
else
return new RectangleD(left, top, right, bottom);
}
public RectangleD IntersectionOf(RectangleD rectangle)
{
// Return nothing if no intersection is possible
if (!IsIntersectingWith(rectangle)) return RectangleD.Empty;
// Return the rectangle representing the intersection
double NewLeft = (rectangle.Left > _Left ? rectangle.Left : _Left);
double NewTop = (rectangle.Top > _Top ? rectangle.Top : _Top);
double NewRight = (rectangle.Right < _Right ? rectangle.Right : _Right);
double NewBottom = (rectangle.Bottom < _Bottom ? rectangle.Bottom : _Bottom);
return new RectangleD(NewTop, NewLeft, NewBottom, NewRight);
}
/// <summary>Returns whether the current instance overlaps the specified rectangle.</summary>
public bool IsIntersectingWith(RectangleD rectangle)
{
if (rectangle.Left >= _Left && rectangle.Left <= _Right)
if (rectangle.Top >= _Top && rectangle.Top <= _Bottom)
return true;
else if (rectangle.Bottom >= _Top && rectangle.Bottom <= _Bottom)
return true;
else if (rectangle.Right >= _Left && rectangle.Right <= _Right)
if (rectangle.Top >= _Top && rectangle.Top <= _Bottom)
return true;
else if (rectangle.Bottom >= _Top && rectangle.Bottom <= _Bottom)
return true;
return false;
//
// return ((rectangle.Left >= Left ) && (rectangle.Left <= Right) && (rectangle.Latitude >= Latitude) && (rectangle.Latitude <= Bottom))
// || ((rectangle.Left + rectangle.Width >= Left ) && (rectangle.Left + rectangle.Width <= Left + Width) && (rectangle.Latitude >= Latitude) && (rectangle.Latitude <= Latitude + Height))
// || ((rectangle.Left >= Left ) && (rectangle.Left <= Left + Width) && (rectangle.Latitude + rectangle.Height >= Latitude) && (rectangle.Latitude + rectangle.Height <= Latitude + Height))
// || ((rectangle.Left + rectangle.Width >= Left ) && (rectangle.Left + rectangle.Width <= Left + Width) && (rectangle.Latitude + rectangle.Height >= Latitude) && (rectangle.Latitude + rectangle.Height <= Latitude + Height));
}
/// <summary>
/// Indicates if the specified RectangleD shares any of the same 2D space as the current instance.
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public bool IsOverlapping(RectangleD rectangle)
{
return !((rectangle.Top > _Bottom)
|| (rectangle.Bottom < _Top)
|| (rectangle.Left > _Right)
|| (rectangle.Right < _Left));
//return true;
// if(rectangle.Top < Top)
// if(rectangle.Bottom < Top) return false;
// if(rectangle.Bottom > Bottom)
// if(rectangle.Top > Bottom) return false;
// if(rectangle.Left < Left)
// if(rectangle.Right < Left) return false;
// if(rectangle.Right > Right)
// if(rectangle.Left > Right) return false;
// return true;
}
/// <summary>
/// Returns whether the current instance is surrounded on all sides by the specified
/// rectangle.
/// </summary>
public bool IsInsideOf(RectangleD rectangle)
{
return (Left > rectangle.Left
&& Right < rectangle.Right
&& _Top > rectangle.Top
&& _Bottom < rectangle.Bottom);
}
/// <summary>
/// Returns whether the current instance surrounds the center point of the specified
/// rectangle.
/// </summary>
public bool IsEnclosingCenter(RectangleD rectangle)
{
return (rectangle.Left <= Center.X
&& rectangle.Right >= Center.X
&& rectangle.Top <= Center.Y
&& rectangle.Bottom >= Center.Y);
}
/// <summary>
/// Returns whether the specified rectangle shares a side with the specified
/// rectangle.
/// </summary>
/// <returns>A <strong>Boolean</strong>, true if the specified rectangle shares one
/// (and only one) side.</returns>
/// <remarks>The method will return false if the specified rectangle intersects with the
/// current instance.</remarks>
public bool IsAdjacentTo(RectangleD rectangle)
{
if (rectangle.Top == _Top)
if (rectangle.Left >= _Left && rectangle.Left <= _Right)
return true;
else if (rectangle.Right >= _Left && rectangle.Right <= _Right)
return true;
else if (rectangle.Bottom == _Bottom)
if (rectangle.Left >= _Left && rectangle.Left <= _Right)
return true;
else if (rectangle.Right >= _Left && rectangle.Right <= _Right)
return true;
else if (rectangle.Left == _Left)
if (rectangle.Top >= _Top && rectangle.Top <= _Bottom)
return true;
else if (rectangle.Bottom >= _Top && rectangle.Bottom <= _Bottom)
return true;
else if (rectangle.Right == _Right)
if (rectangle.Top >= _Top && rectangle.Top <= _Bottom)
return true;
else if (rectangle.Bottom >= _Top && rectangle.Bottom <= _Bottom)
return true;
return false;
}
public bool IsOverlapping(PointD point)
{
if (point.X < _Left) return false;
if (point.X > _Right) return false;
if (point.Y > _Top) return false;
if (point.Y < _Bottom) return false;
return true;
}
public RectangleD UnionWith(RectangleD rectangle)
{
// Build a new rectangle from these rectangles
//Console.WriteLine("rectangle #1: " + ToString());
//Console.WriteLine("rectangle #2: " + rectangle.ToString());
return new RectangleD(rectangle.Top < _Top ? rectangle.Top : _Top,
rectangle.Left < _Left ? rectangle.Left : _Left,
rectangle.Bottom > _Bottom ? rectangle.Bottom : _Bottom,
rectangle.Right > _Right ? rectangle.Right : _Right);
//Console.WriteLine("Result: " + Result.ToString());
//return Result;
}
public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
#endregion
#region Overrides
public override bool Equals(object obj)
{
// Return false if the value is null
if (obj is RectangleD)
return this.Equals((RectangleD)obj);
return false;
}
/// <summary>Returns a unique code for this instance used in hash tables.</summary>
public override int GetHashCode()
{
return _Left.GetHashCode() ^ _Right.GetHashCode() ^ _Top.GetHashCode() ^ _Bottom.GetHashCode();
}
public override string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
}
#endregion
#region Static Methods
public static RectangleD FromLTRB(double left, double top, double right, double bottom)
{
return new RectangleD(new PointD(left, top), new PointD(right, bottom));
}
/// <summary>
/// Parses a string into a RectangleD object.
/// </summary>
/// <param name="value">A <string>String</string> specifying geographic coordinates defining a rectangle.</param>
/// <returns>A <strong>RectangleD</strong> object using the specified coordinates.</returns>
/// <remarks>This powerful method will convert points defining a rectangle in the form of a string into
/// a RectangleD object. The string can be </remarks>
public static RectangleD Parse(string value)
{
return new RectangleD(value);
}
public static RectangleD Parse(string value, CultureInfo culture)
{
return new RectangleD(value, culture);
}
/// <summary>
/// Returns the smallest possible RectangleD containing both specified RectangleDs.
/// </summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
public static RectangleD UnionWith(RectangleD first, RectangleD second)
{
return first.UnionWith(second);
}
/// <summary>
/// Returns a rectangle which encloses the specified points.
/// </summary>
/// <param name="points">An array of PointD objects to enclose.</param>
/// <returns>A <strong>RectangleD</strong> object enclosing the specified points.</returns>
/// <remarks>This method is typically used to calculate a rectangle surrounding
/// points which have been rotated. For example, if a rectangle is rotated by 45°, the
/// total width it occupies is greater than it's own width.</remarks>
public static RectangleD FromArray(PointD[] points)
{
// Start with the first point
double top = points[0].Y;
double left = points[0].X;
double bottom = points[0].Y;
double right = points[0].X;
// Expand the points outward as limits are breached
for (int index = 1; index < points.Length; index++)
{
PointD point = points[index];
if (point.X < left)
left = point.X;
else if (point.X > right)
right = point.X;
if (point.Y < top)
top = point.Y;
else if (point.Y > bottom)
bottom = point.Y;
}
// Build a new rectangle
return new RectangleD(left, top, right, bottom);
}
/// <summary>
/// Returns the RectangleD formed by the intersection of the two specified RectangleDs.
/// </summary>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
public static RectangleD IntersectionOf(RectangleD first, RectangleD second)
{
return first.IntersectionOf(second);
}
#endregion
#region Operators
/// <summary>
/// Increases the size of the rectangle by the specified amount.
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
public static RectangleD operator +(RectangleD left, SizeD size)
{
return new RectangleD(left.UpperLeft, left.Size.Add(size));
}
public static RectangleD operator +(RectangleD left, PointD location)
{
return new RectangleD(left.UpperLeft.Add(location), left.Size);
}
public static RectangleD operator -(RectangleD left, SizeD size)
{
return new RectangleD(left.UpperLeft, left.Size.Subtract(size));
}
public static RectangleD operator -(RectangleD left, PointD location)
{
return new RectangleD(left.UpperLeft.Subtract(location), left.Size);
}
public static RectangleD operator *(RectangleD left, SizeD size)
{
return new RectangleD(left.UpperLeft, left.Size.Multiply(size));
}
public static RectangleD operator *(RectangleD left, PointD location)
{
return new RectangleD(left.UpperLeft.Multiply(location), left.Size);
}
public static RectangleD operator /(RectangleD left, SizeD size)
{
return new RectangleD(left.UpperLeft, left.Size.Divide(size));
}
public static RectangleD operator /(RectangleD left, PointD location)
{
return new RectangleD(left.UpperLeft.Divide(location), left.Size);
}
public static bool operator ==(RectangleD left, RectangleD right)
{
return left.Equals(right);
}
public static bool operator !=(RectangleD left, RectangleD right)
{
return !left.Equals(right);
}
public RectangleD Add(SizeD size)
{
return new RectangleD(UpperLeft, Size.Add(size));
}
public RectangleD Subtract(SizeD size)
{
return new RectangleD(UpperLeft, Size.Subtract(size));
}
public RectangleD Multiply(SizeD size)
{
return new RectangleD(UpperLeft, Size.Multiply(size));
}
public RectangleD Divide(SizeD size)
{
return new RectangleD(UpperLeft, Size.Divide(size));
}
public RectangleD Add(PointD position)
{
return new RectangleD(UpperLeft.Add(position), Size);
}
public RectangleD Subtract(PointD position)
{
return new RectangleD(UpperLeft.Subtract(position), Size);
}
public RectangleD Multiply(PointD position)
{
return new RectangleD(UpperLeft.Multiply(position), Size);
}
public RectangleD Divide(PointD position)
{
return new RectangleD(UpperLeft.Divide(position), Size);
}
#endregion
#region Conversions
public static explicit operator RectangleD(Rectangle value)
{
return new RectangleD((double)value.Top, (double)value.Left, (double)value.Bottom, (double)value.Right);
}
public static explicit operator RectangleD(RectangleF value)
{
return new RectangleD((double)value.Top, (double)value.Left, (double)value.Bottom, (double)value.Right);
}
public static explicit operator Rectangle(RectangleD value)
{
return new Rectangle((int)value.Top, (int)value.Left, (int)(value.Bottom - value.Top), (int)(value.Right - value.Left));
}
public static explicit operator RectangleF(RectangleD value)
{
return new RectangleF((float)value.Left, (float)value.Top, (float)(value.Width), (float)(value.Height));
}
#endregion
#region IEquatable<RectangleD> Members
public bool Equals(RectangleD other)
{
// The objects are equivalent if their bounds are equivalent
return _Left == other.Left
&& _Right == other.Right
&& _Top == other.Top
&& _Bottom == other.Bottom;
}
#endregion
#region IFormattable Members
public string ToString(string format, IFormatProvider formatProvider)
{
CultureInfo culture = (CultureInfo)formatProvider;
return _Left.ToString(format, formatProvider)
+ culture.TextInfo.ListSeparator
+ _Top.ToString(format, formatProvider)
+ culture.TextInfo.ListSeparator
+ Width.ToString(format, formatProvider)
+ culture.TextInfo.ListSeparator
+ Height.ToString(format, formatProvider);
}
#endregion
#region IXmlSerializable Members
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("Top",
_Top.ToString("G17", CultureInfo.InvariantCulture));
writer.WriteAttributeString("Bottom",
_Bottom.ToString("G17", CultureInfo.InvariantCulture));
writer.WriteAttributeString("Left",
_Left.ToString("G17", CultureInfo.InvariantCulture));
writer.WriteAttributeString("Right",
_Right.ToString("G17", CultureInfo.InvariantCulture));
}
void IXmlSerializable.ReadXml(XmlReader reader)
{
throw new InvalidOperationException("Use the RectangleD(XmlReader) constructor to create a new instance instead of calling ReadXml.");
}
#endregion
#region Unused Code (Commented Out)
// /// <summary>
// /// Changes the size and shape of the RectangleD to match the aspect ratio of the specified RectangleD.
// /// </summary>
// /// <param name="aspectRatio"></param>
// /// <returns></returns>
// /// <remarks>This method will expand a RectangleD outward, from its center point, until
// /// the ratio of its width to its height matches the specified value.</remarks>
// public RectangleD ToAspectRatio(System.Drawing.Rectangle rectangle)
// {
// // Calculate the aspect ratio
// return ToAspectRatio((double)RectangleD.Width / (double)RectangleD.Height);
// }
//
// /// <summary>
// /// Changes the size and shape of the RectangleD to match the aspect ratio of the specified RectangleD.
// /// </summary>
// /// <param name="aspectRatio"></param>
// /// <returns></returns>
// /// <remarks>This method will expand a RectangleD outward, from its center point, until
// /// the ratio of its width to its height matches the specified value.</remarks>
// public RectangleD ToAspectRatio(System.Drawing.RectangleF RectangleD)
// {
// // Calculate the aspect ratio
// return ToAspectRatio((double)RectangleD.Width / (double)RectangleD.Height);
// }
// /// <summary>
// /// Moves the entire RectangleD in the specified direction by the specified distance.
// /// </summary>
// /// <param name="direction"></param>
// /// <param name="distance"></param>
// /// <returns></returns>
// ////[CLSCompliant(false)]
// public RectangleD TranslateTo(Angle direction, double distance)
// {
// // Find the new translation point
// return new RectangleD(UpperLeft.TranslateTo(direction, distance), Size);
// }
#endregion
}
}