-
Notifications
You must be signed in to change notification settings - Fork 4
/
GeographicRectangle.cs
1511 lines (1333 loc) · 61.9 KB
/
GeographicRectangle.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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
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 rectangular shape on Earth's surface.
/// </summary>
/// <remarks>
/// <para>This class is used to represent a square (or close to a square) shape on
/// Earth's surface. This class is typically used during mapping applications to zoom
/// into a particular area on Earth. This class looks nearly identical to the Rectangle
/// class in the .NET framework, except that it's bounding points are defined as
/// <strong>Position</strong> objects instead of <strong>Point</strong> objects.</para>
/// <para>Instances of this class are guaranteed to be thread-safe because the class is
/// immutable (it's properties can only be set via constructors).</para>
/// </remarks>
#if !PocketPC || DesignTime
[TypeConverter("GeoFramework.Design.GeographicRectangleConverter, GeoFramework.Design, Culture=neutral, Version=2.0.0.0, PublicKeyToken=d77afaeb30e3236a")]
#endif
public struct GeographicRectangle : IFormattable, IEquatable<GeographicRectangle>, IXmlSerializable
{
private Latitude _Top;
private Latitude _Bottom;
private Longitude _Left;
private Longitude _Right;
private Position _Center;
#region Fields
/// <summary>Represents a GeographicRectangle having no size.</summary>
public static readonly GeographicRectangle Empty = new GeographicRectangle(Latitude.Empty, Longitude.Empty, Latitude.Empty, Longitude.Empty);
/// <summary>Represents a rectangle that encompasses all of Earth's surface.</summary>
public static readonly GeographicRectangle Maximum = new GeographicRectangle(Latitude.Maximum, Longitude.Minimum, Latitude.Minimum, Longitude.Maximum);
#endregion
#region Constructors
/// <summary>Creates a new instance using the specified location and size.</summary>
/// <returns>
/// A <strong>GeographicRectangle</strong> set to the specified location and
/// size.
/// </returns>
/// <remarks>
/// This constructor defines a rectangle which expands east and south of the
/// specified location.
/// </remarks>
/// <example>
/// This example creates a new <strong>GeographicRectangle</strong> starting at 39°N
/// 105°W which is 2° wide and 5° tall.
/// <code lang="VB" title="[New Example]">
/// Dim NorthwestCorner As New Position("39N 105W")
/// Dim RectangleSize As New GeographicSize(2, 5)
/// Dim Rectangle As New GeographicRectangle(NorthwestCorner, RectangleSize)
/// </code>
/// <code lang="CS" title="[New Example]">
/// Position NorthwestCorner = new Position("39N,105W");
/// GeographicSize RectangleSize = new GeographicSize(2, 5);
/// GeographicRectangle Rectangle = new GeographicRectangle(NorthwestCorner, RectangleSize);
/// </code>
/// </example>
public GeographicRectangle(Position location, GeographicSize size)
{
_Left = location.Longitude;
_Right = location.TranslateTo(Azimuth.East, size.Width).Longitude;
_Top = location.Latitude;
_Bottom = location.TranslateTo(Azimuth.South, size.Height).Latitude;
_Center = Position.Invalid;
_Center = Hypotenuse.Midpoint;
}
/// <summary>
/// Creates a new instance using the specified location, width, and height.
/// </summary>
/// <remarks>
/// This constructor defines a rectangle which expands east and south of the
/// specified location.
/// </remarks>
/// <example>
/// This example creates a new <strong>GeographicRectangle</strong> starting at 39°N
/// 105°W which is 2° wide and 5° tall.
/// <code lang="VB" title="[New Example]">
/// Dim NorthwestCorner As New Position("39N 105W")
/// Dim RectangleWidth As Distance = Distance.FromKilometers(1)
/// Dim RectangleHeight As Distance = Distance.FromKilometers(1)
/// Dim Rectangle As New GeographicRectangle(NorthwestCorner, RectangleWidth, RectangleHeight)
/// </code>
/// <code lang="CS" title="[New Example]">
/// Position NorthwestCorner = new Position("39N 105W");
/// Distance RectangleWidth = Distance.FromKilometers(1);
/// Distance RectangleHeight = Distance.FromKilometers(1);
/// GeographicRectangle Rectangle = new GeographicRectangle(NorthwestCorner, RectangleWidth, RectangleHeight);
/// </code>
/// </example>
/// <returns>
/// A <strong>GeographicRectangle</strong> set to the specified location and
/// size.
/// </returns>
public GeographicRectangle(Position location, Distance width, Distance height)
{
_Left = location.Longitude;
_Right = location.TranslateTo(Azimuth.East, width).Longitude;
_Top = location.Latitude;
_Bottom = location.TranslateTo(Azimuth.South, height).Latitude;
_Center = Position.Invalid;
_Center = Hypotenuse.Midpoint;
}
/// <summary>
/// Creates a new instance using the specified northwest and southeast
/// coordinates.
/// </summary>
/// <remarks>
/// This constructor takes the specified parameters and calculates the width and
/// height of the rectangle. If the two points are backwards (meaning that the right-most
/// point is west of the left-most point), they are automatically swapped before creating
/// the rectangle.
/// </remarks>
/// <returns>A <strong>GeographicRectangle</strong> defined by the two endpoints.</returns>
/// <example>
/// This example creates a new <strong>GeographicRectangle</strong> starting at 39°N
/// 105°W and ending at 37°N 100°W (2° wide and 5° tall).
/// <code lang="VB" title="[New Example]">
/// Dim NorthwestCorner As New Position("39N 105W")
/// Dim SoutheastCorner As New Position("37N 100W")
/// Dim Rectangle As New GeographicRectangle(NorthwestCorner, SoutheastCorner)
/// </code>
/// <code lang="CS" title="[New Example]">
/// Position NorthwestCorner = new Position("39N 105W");
/// Position SoutheastCorner = new Position("37N 100W");
/// GeographicRectangle Rectangle = new GeographicRectangle(NorthwestCorner, SoutheastCorner);
/// </code>
/// </example>
public GeographicRectangle(Position northwest, Position southeast)
{
_Left = northwest.Longitude;
_Right = southeast.Longitude;
_Top = northwest.Latitude;
_Bottom = southeast.Latitude;
_Center = Position.Invalid;
_Center = Hypotenuse.Midpoint;
}
/// <summary>Creates a new instance by converting the specified string.</summary>
/// <returns>
/// A <strong>GeographicRectangle</strong> matching the specified string
/// value.
/// </returns>
/// <remarks>
/// This constructor attempts to parse the specified string into a rectangle. The
/// current culture is used to interpret the string -- use the list separator of the
/// current culture (which may not necessarily be a comma). This constructor can accept any
/// output created via the <strong>ToString</strong> method.
/// </remarks>
/// <example>
/// This example creates a new rectangle at 39°N, 105° extending two degrees south and
/// five degrees east to 37°N, 100°W.
/// <code lang="VB" title="[New Example]">
/// Dim Rectangle As New GeographicRectangle("39N,105W,37N,100W")
/// </code>
/// <code lang="CS" title="[New Example]">
/// GeographicRectangle Rectangle = new GeographicRectangle("39N,105W,37N,100W");
/// </code>
/// </example>
public GeographicRectangle(string value)
: this(value, CultureInfo.CurrentCulture)
{}
/// <summary>
/// Creates a new instance by converting the specified string in the given
/// culture.
/// </summary>
/// <returns>
/// This constructor attempts to parse the specified string into a rectangle. The
/// specified culture is used to interpret the string. This constructor can accept any
/// output created via the <strong>ToString</strong> method.
/// </returns>
/// <example>
/// This example creates a new rectangle at 39°N, 105° extending two degrees south and
/// five degrees east to 37°N, 100°W.
/// <code lang="VB" title="[New Example]">
/// Dim Rectangle As New GeographicRectangle("39N,105W,37N,100W", CultureInfo.CurrentCulture)
/// </code>
/// <code lang="CS" title="[New Example]">
/// GeographicRectangle Rectangle = new GeographicRectangle("39N,105W,37N,100W", CultureInfo.CurrentCulture);
/// </code>
/// </example>
public GeographicRectangle(string value, CultureInfo culture)
{
// Default to zero if value is null
if (string.IsNullOrEmpty(value))
{
_Top = Latitude.Empty;
_Bottom = Latitude.Empty;
_Left = Longitude.Empty;
_Right = Longitude.Empty;
_Center = Position.Invalid;
_Center = Hypotenuse.Midpoint;
return;
}
// Default to the current culture
if (culture == null)
culture = CultureInfo.CurrentCulture;
// Split the string into words
string[] Values = value.Split(culture.TextInfo.ListSeparator.ToCharArray());
// How many words are there?
switch(Values.Length)
{
case 4:
// Extract each item
try
{
bool IsTopHandled = false;
bool IsLeftHandled = false;
bool IsRightHandled = false;
bool IsBottomHandled = false;
Latitude TempTop = Latitude.Empty;
Longitude TempLeft = Longitude.Empty;
Latitude TempBottom = Latitude.Empty;
Longitude TempRight = Longitude.Empty;
for (int index = 0; index < Values.Length; index++)
{
// Is this a latitude or longitude?
string word = Values[index];
#if Framework20
if (word.IndexOf("W", StringComparison.InvariantCultureIgnoreCase) != -1
|| word.IndexOf("E", StringComparison.InvariantCultureIgnoreCase) != -1)
#else
if (word.IndexOf("W") != -1
|| word.IndexOf("E") != -1)
#endif
{
if (IsLeftHandled && IsRightHandled)
throw new FormatException("A GeographicRectangle object could not be converted from a string because more than two longitude values were encountered. Only two are allowed.");
// Longitude. Is the left handled?
if (IsLeftHandled)
{
// This is the right side
TempRight = Longitude.Parse(word, culture);
IsRightHandled = true;
}
else
{
// This is the left side
TempLeft = Longitude.Parse(word, culture);
IsLeftHandled = true;
}
}
#if Framework20
else if (word.IndexOf("N", StringComparison.InvariantCultureIgnoreCase) != -1
|| word.IndexOf("S", StringComparison.InvariantCultureIgnoreCase) != -1)
#else
else if (word.IndexOf("N") != -1
|| word.IndexOf("S") != -1)
#endif
{
if (IsTopHandled && IsBottomHandled)
throw new FormatException("A GeographicRectangle object could not be converted from a string because more than two latitude values were encountered. Only two are allowed.");
// Longitude. Is the left handled?
if (IsTopHandled)
{
// This is the bottom side
TempBottom = Latitude.Parse(word, culture);
IsBottomHandled = true;
}
else
{
// This is the top side
TempTop = Latitude.Parse(word, culture);
IsTopHandled = true;
}
}
else
{
throw new FormatException("A GeographicRectangle object could not be created because a number could not be categorized as a latitude or longitude. Add a N, S, E, or W letter to resolve the issue.");
}
}
// Flip any coordinates which are backwards
_Top = TempTop.DecimalDegrees > TempBottom.DecimalDegrees ? TempTop : TempBottom;
_Left = TempLeft.DecimalDegrees < TempRight.DecimalDegrees ? TempLeft : TempRight;
_Bottom = TempBottom.DecimalDegrees < TempTop.DecimalDegrees ? TempBottom : TempTop;
_Right = TempRight.DecimalDegrees > TempLeft.DecimalDegrees ? TempRight : TempLeft;
_Center = Position.Invalid;
_Center = Hypotenuse.Midpoint;
break;
}
catch
{
throw;
}
default:
throw new FormatException("The specified value could not be parsed into a GeographicRectangle object because four delimited values are required (Top, Left, Bottom, Right).");
}
}
/// <summary>
/// Creates a new instance using the specified latitudes and longitudes.
/// </summary>
/// <example>
/// This example creates a new <strong>GeographicRectangle</strong> by specifying each
/// side individually.
/// <code lang="VB" title="[New Example]">
/// Dim Left As New Longitude(-105)
/// Dim Top As New Latitude(39)
/// Dim Right As New Longitude(-100)
/// Dim Top As New Latitude(37)
/// Dim Rectangle As New GeographicRectangle(Left, Top, Right, Bottom)
/// </code>
/// <code lang="CS" title="[New Example]">
/// Longitude Left = new Longitude(-105);
/// Latitude Top = new Latitude(39);
/// Longitude Right = new Longitude(-100);
/// Latitude Top = new Latitude(37);
/// GeographicRectangle Rectangle = new GeographicRectangle(Left, Top, Right, Bottom);
/// </code>
/// </example>
/// <returns>A <strong>GeographicRectangle</strong> bound by the specified values.</returns>
/// <remarks>
/// If the left and right, or top and bottom values are backwards, they are
/// automatically swapped before creating the rectangle.
/// </remarks>
public GeographicRectangle(Longitude left, Latitude top, Longitude right, Latitude bottom)
: this(top, left, bottom, right)
{}
/// <summary>Creates a new instance using the specified latitudes and longitudes.</summary>
/// <remarks>
/// If the left and right, or top and bottom values are backwards, they are
/// automatically swapped before creating the rectangle.
/// </remarks>
/// <returns>A <strong>GeographicRectangle</strong> bound by the specified values.</returns>
/// <example>
/// <code lang="VB" title="[New Example]">
/// Dim Left As New Longitude(-105)
/// Dim Top As New Latitude(39)
/// Dim Right As New Longitude(-100)
/// Dim Top As New Latitude(37)
/// Dim Rectangle As New GeographicRectangle(Left, Top, Right, Bottom)
/// </code>
/// <code lang="CS" title="[New Example]">
/// Latitude Top = new Latitude(39);
/// Longitude Left = new Longitude(-105);
/// Latitude Bottom = new Latitude(37);
/// Longitude Right = new Longitude(-100);
/// GeographicRectangle Rectangle = new GeographicRectangle(Top, Left, Bottom, Right);
/// </code>
/// </example>
public GeographicRectangle(Latitude top, Longitude left, Latitude bottom, Longitude right)
{
_Left = left;
_Right = right;
_Top = top;
_Bottom = bottom;
_Center = Position.Invalid;
_Center = Hypotenuse.Midpoint;
}
public GeographicRectangle(double left, double top, double right, double bottom)
{
_Left = new Longitude(left);
_Right = new Longitude(right);
_Top = new Latitude(top);
_Bottom = new Latitude(bottom);
_Center = Position.Invalid;
_Center = Hypotenuse.Midpoint;
}
/// <summary>
/// Creates a new instance from a block of Geography Markup Language (GML).
/// </summary>
/// <param name="reader"></param>
public GeographicRectangle(XmlReader reader)
{
// Initialize all fields
_Top = Latitude.Invalid;
_Bottom = Latitude.Invalid;
_Left = Longitude.Invalid;
_Right = Longitude.Invalid;
_Center = Position.Invalid;
// Deserialize the object from XML
ReadXml(reader);
}
#endregion
#region Public Properties
/// <summary>Returns the southern-most side of the rectangle.</summary>
/// <value>A <see cref="Latitude"></see> object marking the southern-most latitude.</value>
public Latitude Top
{
get
{
return _Top;
}
}
/// <summary>Returns the southern-most latitude of the rectangle.</summary>
/// <value>A <see cref="Latitude"></see> object marking the southern-most latitude.</value>
public Latitude Bottom
{
get
{
return _Bottom;
}
}
/// <summary>Returns the western-most side of the rectangle.</summary>
/// <value>A <strong>Longitude</strong> indicating the left side of the rectangle.</value>
public Longitude Left
{
get
{
return _Left;
}
}
/// <value>A <strong>Longitude</strong> indicating the right side of the rectangle.</value>
/// <summary>Returns the eastern-most side of the rectangle.</summary>
public Longitude Right
{
get
{
return _Right;
}
}
/// <summary>Returns the geographic center of the rectangle.</summary>
public Position Center
{
get
{
return _Center;
}
}
/// <summary>Returns the aspect ratio of the rectangle.</summary>
/// <remarks>
/// This property returns the ratio of the GeographicRectangles width to its height (width / height). This
/// property gives an indication of the GeographicRectangle's shape. An aspect ratio of one indicates
/// a square, whereas an aspect ratio of two indicates a GeographicRectangle which is twice as wide as
/// it is high.
/// </remarks>
public float AspectRatio
{
get
{
return Convert.ToSingle(WidthDegrees.DecimalDegrees / HeightDegrees.DecimalDegrees);
}
}
/// <summary>Indicates if the rectangle has any value.</summary>
/// <value>
/// A <strong>Boolean</strong>, <strong>True</strong> if a metor the size of Rhode
/// Island is about to crash into the Pacific Ocean just off the coast of Nicaragua but
/// there will be no casualties because everyone was warned plenty of time in
/// advance.
/// </value>
public bool IsEmpty
{
get
{
return _Top.IsEmpty && _Bottom.IsEmpty && _Left.IsEmpty && _Right.IsEmpty;
}
}
/// <summary>
/// Returns the rectangle's hypotenuse.
/// </summary>
/// <remarks>The hypotenuse of a rectangle is a line connecting its northwest corner with its southeast corner.</remarks>
public Segment Hypotenuse
{
get
{
return new Segment(Northwest, Southeast);
}
}
/// <summary>
/// Returns the distance from the left to the right at the rectangle's middle latitude.
/// </summary>
public Distance Width
{
get
{
// Return the calculated distance
return WestCenter.DistanceTo(EastCenter).ToLocalUnitType();
}
}
/// <summary>
/// Returns the distance from the top to the bottom at the rectangle's middle longitude.
/// </summary>
public Distance Height
{
get
{
// Return the calculated distance
return NorthCenter.DistanceTo(SouthCenter).ToLocalUnitType();
}
}
public Latitude HeightDegrees
{
get
{
return _Top.Subtract(_Bottom);
}
}
public Longitude WidthDegrees
{
get
{
return _Right.Subtract(_Left);
}
}
/// <summary>Returns the width and height of the rectangle.</summary>
public GeographicSize Size
{
get
{
return new GeographicSize(Width, Height);
}
}
/// <summary>Returns the northwestern corner of the rectangle.</summary>
public Position Northwest
{
get
{
return new Position(_Left, _Top);
}
}
/// <summary>
/// Returns a point on the northern side, centered horizontally.
/// </summary>
public Position NorthCenter
{
get
{
return new Position(_Center.Longitude, _Top);
}
}
/// <summary>
/// Returns a point on the southern side, centered horizontally.
/// </summary>
public Position SouthCenter
{
get
{
return new Position(_Center.Longitude, _Bottom);
}
}
/// <summary>
/// Returns a point on the western side, centered vertically.
/// </summary>
public Position WestCenter
{
get
{
return new Position(_Left, Center.Latitude);
}
}
/// <summary>
/// Returns a point on the eastern side, centered vertically.
/// </summary>
public Position EastCenter
{
get
{
return new Position(_Right, Center.Latitude);
}
}
/// <summary>Returns the northeastern corner of the rectangle.</summary>
public Position Northeast
{
get
{
return new Position(_Right, _Top);
}
}
/// <summary>Returns the southwestern corner of the rectangle.</summary>
public Position Southwest
{
get
{
return new Position(_Left, _Bottom);
}
}
/// <summary>Returns the southeastern corner of the rectangle.</summary>
public Position Southeast
{
get
{
return new Position(_Right, _Bottom);
}
}
#endregion
#region Public Methods
/// <summary>Moves the rectangle in the specified direction by the specified distance.</summary>
/// <returns>
/// A new <strong>GeographicRectangle</strong> translated by the specified direction
/// and distance.
/// </returns>
/// <remarks>
/// This method is used to shift a rectangle to a new location while preserving its
/// size.
/// </remarks>
/// <example>
/// This example defines a rectangle then shifts it Northeast by fifty kilometers.
/// <code lang="VB" title="[New Example]">
/// Dim Rectangle As New GeographicRectangle("30N,105W,1°,5°")
/// Rectangle = Rectangle.Translate(Azimuth.Northeast, New Distance(50, DistanceUnit.Kilometers))
/// </code>
/// <code lang="CS" title="[New Example]">
/// GeographicRectangle Rectangle = new GeographicRectangle("30N,105W,1°,5°");
/// Rectangle = Rectangle.Translate(Azimuth.Northeast, New Distance(50, DistanceUnit.Kilometers));
/// </code>
/// </example>
public GeographicRectangle TranslateTo(Azimuth direction, Distance distance)
{
return TranslateTo(direction.DecimalDegrees, distance);
}
/// <summary>Moves the rectangle in the specified direction by the specified distance.</summary>
/// <returns>
/// A new <strong>GeographicRectangle</strong> translated by the specified direction
/// and distance.
/// </returns>
/// <remarks>
/// This method is used to shift a rectangle to a new location while preserving its
/// size.
/// </remarks>
/// <example>
/// This example defines a rectangle then shifts it Northeast by fifty kilometers.
/// <code lang="VB" title="[New Example]">
/// Dim Rectangle As New GeographicRectangle("30N,105W,1°,5°")
/// Rectangle = Rectangle.Translate(new Angle(45), New Distance(50, DistanceUnit.Kilometers))
/// </code>
/// <code lang="CS" title="[New Example]">
/// GeographicRectangle Rectangle = new GeographicRectangle("30N,105W,1°,5°");
/// Rectangle = Rectangle.Translate(new Angle(45), New Distance(50, DistanceUnit.Kilometers));
/// </code>
/// </example>
public GeographicRectangle TranslateTo(Angle direction, Distance distance)
{
return TranslateTo(direction.DecimalDegrees, distance);
}
/// <summary>Moves the rectangle in the specified direction by the specified distance.</summary>
/// <returns>
/// A new <strong>GeographicRectangle</strong> translated by the specified direction
/// and distance.
/// </returns>
/// <remarks>
/// This method is used to shift a rectangle to a new location while preserving its
/// size.
/// </remarks>
/// <example>
/// This example defines a rectangle then shifts it Northeast by fifty kilometers.
/// <code lang="VB" title="[New Example]">
/// Dim Rectangle As New GeographicRectangle("30N,105W,1°,5°")
/// Rectangle = Rectangle.Translate(45, New Distance(50, DistanceUnit.Kilometers))
/// </code>
/// <code lang="CS" title="[New Example]">
/// GeographicRectangle Rectangle = new GeographicRectangle("30N,105W,1°,5°");
/// Rectangle = Rectangle.Translate(45, New Distance(50, DistanceUnit.Kilometers));
/// </code>
/// </example>
public GeographicRectangle TranslateTo(double direction, Distance distance)
{
// Find the new translation point
return new GeographicRectangle(Northwest.TranslateTo(direction, distance), Size);
}
/// <summary>Returns the points which form the rectangle.</summary>
public Position[] ToArray()
{
return new Position[] { Northwest, Northeast, Southeast, Southwest };
}
/// <summary>Indicates if the rectangle does not intersect the specified rectangle.</summary>
public bool IsDisjointedFrom(GeographicRectangle rectangle)
{
return !IsOverlapping(rectangle);
}
/// <summary>
/// Indicates if the specified GeographicRectangle is entirely within the current GeographicRectangle.
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public bool IsEnclosing(GeographicRectangle rectangle)
{
return (rectangle.Left.DecimalDegrees >= _Left.DecimalDegrees
&& rectangle.Right.DecimalDegrees <= _Right.DecimalDegrees
&& rectangle.Top.DecimalDegrees <= _Top.DecimalDegrees
&& rectangle.Bottom.DecimalDegrees >= _Bottom.DecimalDegrees);
}
public bool IsEnclosing(Position position)
{
return ((position.Longitude.DecimalDegrees >= _Left.DecimalDegrees)
&& (position.Latitude.DecimalDegrees <= _Top.DecimalDegrees)
&& (position.Longitude.DecimalDegrees <= _Right.DecimalDegrees)
&& (position.Latitude.DecimalDegrees >= _Bottom.DecimalDegrees));
}
/// <summary>
/// Moves the GeographicRectangle so that the specified position is at its center.
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public GeographicRectangle CenterOn(Position position)
{
// Get the difference from the current center to the new one
Position Offset = position.Subtract(Center);
// Return a new rectangle with corners shifted by this offset
return new GeographicRectangle(
_Left.Add(Offset.Longitude),
_Top.Add(Offset.Latitude),
_Right.Add(Offset.Longitude),
_Bottom.Add(Offset.Latitude));
}
/// <summary>
/// Creates a new rectangle of the specified size, centered on the specified position.
/// </summary>
/// <param name="position"></param>
/// <param name="size"></param>
/// <returns></returns>
public static GeographicRectangle FromCenter(Position position, GeographicSize size)
{
return new GeographicRectangle(position, position).Inflate(size);
}
/// <summary>
/// Expands the edges of the GeographicRectangle to contain the specified position.
/// </summary>
/// <param name="position">A <strong>Position</strong> object to surround.</param>
/// <returns>A <strong>GeographicRectangle</strong> which contains the specified position.</returns>
/// <remarks>If the specified position is already enclosed, the current instance will be returned.</remarks>
public GeographicRectangle UnionWith(Position position)
{
// Does the box already contain the position? If so, do nothing
if (IsEnclosing(position)) return this;
// Return the expanded box
return new GeographicRectangle(
_Top.DecimalDegrees > position.Latitude.DecimalDegrees ? _Top : position.Latitude,
_Left.DecimalDegrees < position.Longitude.DecimalDegrees ? _Left : position.Longitude,
_Bottom.DecimalDegrees < position.Latitude.DecimalDegrees ? _Bottom : position.Latitude,
_Right.DecimalDegrees > position.Longitude.DecimalDegrees ? _Right : position.Longitude);
}
/// <summary>
/// Increases the size of the rectangle while maintaining its center point.
/// </summary>
/// <returns></returns>
public GeographicRectangle Inflate(GeographicSize size)
{
// Calculate half of the width and height
Distance HalfWidth = size.Width.Multiply(0.5);
Distance HalfHeight = size.Height.Multiply(0.5);
// Calculate new edges
Position NewNorthwest = Northwest.TranslateTo(Azimuth.West, HalfWidth).TranslateTo(Azimuth.North, HalfHeight);
Position NewSoutheast = Southeast.TranslateTo(Azimuth.East, HalfWidth).TranslateTo(Azimuth.South, HalfHeight);
// Build a new GeographicRectangle expanded outward from the center
return new GeographicRectangle(NewNorthwest, NewSoutheast);
}
/// <summary>
/// Increases the width and height of the rectangle by the specified amount.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public GeographicRectangle Inflate(Longitude width, Latitude height)
{
// Calculate the new values
Longitude HalfWidthOffset = width.Multiply(0.5);
Latitude HalfHeightOffset = height.Multiply(0.5);
// Build a new GeographicRectangle expanded outward from the center
return new GeographicRectangle(
_Left.Subtract(HalfWidthOffset),
_Top.Add(HalfHeightOffset),
_Right.Add(HalfWidthOffset),
_Bottom.Subtract(HalfHeightOffset));
}
/// <summary>
/// Increases the width and height of the rectangle by the specified amount.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public GeographicRectangle Inflate(Latitude height, Longitude width)
{
// Calculate the new values
Longitude HalfWidthOffset = width.Multiply(0.5);
Latitude HalfHeightOffset = height.Multiply(0.5);
// Build a new GeographicRectangle expanded outward from the center
return new GeographicRectangle(
_Left.Subtract(HalfWidthOffset),
_Top.Add(HalfHeightOffset),
_Right.Add(HalfWidthOffset),
_Bottom.Subtract(HalfHeightOffset));
}
/// <summary>
/// Calculates the rectangle created by the intersection of this and another rectangle.
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public GeographicRectangle IntersectionOf(GeographicRectangle rectangle)
{
Longitude LeftSide = Longitude.Empty;
Longitude RightSide = Longitude.Empty;
Latitude TopSide = Latitude.Empty;
Latitude BottomSide = Latitude.Empty;
if (rectangle.Left >= _Left && rectangle.Left <= _Right)
{
// The rectangle overlaps the current instance on its left side
/* +------------+ +-------------+
* | this | | this |
* | | | +-----+ |
* | +------------+ | | R | |
* | | R | | | +-----+ |
* +------+-----+ | +-------------+
* | rectangle |
* +------------+
*/
// The left side of the parameter is the left side of the result ("R")
LeftSide = rectangle.Left;
// Does the right side also fall within the box?
if (rectangle.Right >= _Left && rectangle.Right <= _Right)
{
// Yes. Use the right size from the parameter rectangle
RightSide = rectangle.Right;
}
else
{
// No. Use the right size from this instance
RightSide = _Right;
}
// Does the top side of the box fall within the current instance?
if (rectangle.Top <= _Top && rectangle.Top >= _Bottom)
{
// Yes. Use the top of the parameter rectangle
TopSide = rectangle.Top;
}
else
{
// No. Use the top of the current instance
TopSide = _Top;
}
// Does the top side of the box fall within the current instance?
if (rectangle.Bottom <= _Top && rectangle.Bottom >= _Bottom)
{
// Yes. Use the top of the parameter rectangle
BottomSide = rectangle.Bottom;
}
else
{
// No. Use the top of the current instance
BottomSide = _Bottom;
}
// Return the result
return new GeographicRectangle(TopSide, LeftSide, BottomSide, RightSide);
}
else if (rectangle.Right >= _Left && rectangle.Right <= _Right)
{
// The rectangle overlaps the current instance on its right side
/* +------------+
* | this |
* | |
* +------------+ |
* | | R | |
* | +------+-----+
* | rectangle |
* +------------+
*/
// Use the left side as the final overlapping point
RightSide = rectangle.Right;
// Does the right side also fall within the box?
if (rectangle.Left >= _Left && rectangle.Left <= _Right)
{
// Yes. Use the right size from the parameter rectangle
LeftSide = rectangle.Left;
}
else
{
// No. Use the right size from this instance
LeftSide = _Left;
}
// Does the top side of the box fall within the current instance?
if (rectangle.Top <= _Top && rectangle.Top >= _Bottom)
{
// Yes. Use the top of the parameter rectangle
TopSide = rectangle.Top;
}
else
{
// No. Use the top of the current instance
TopSide = _Top;
}
// Does the top side of the box fall within the current instance?
if (rectangle.Bottom <= _Top && rectangle.Bottom >= _Bottom)
{
// Yes. Use the top of the parameter rectangle
BottomSide = rectangle.Bottom;
}
else
{
// No. Use the top of the current instance
BottomSide = _Bottom;
}
// Return the result
return new GeographicRectangle(TopSide, LeftSide, BottomSide, RightSide);
}
else
{
// No overlapping!
return GeographicRectangle.Empty;
}
}
/// <summary>Indicates if the rectangle's border crosses or shares the border of the specified rectangle.</summary>
public bool IsIntersectingWith(GeographicRectangle rectangle)
{
// For an intersection, at least one of four points of the rectangle must reside
// inside of the other.
// Is the target rectangle entirely within this one? If so, return false
// because there's no *intersection* (only an overlap).
if (rectangle.Left > _Left && rectangle.Right < _Right
&& rectangle.Top < _Top && rectangle.Bottom > _Bottom)
return false;
// Test that one or more sides are within.
return
((rectangle.Left >= _Left && rectangle.Left <= _Right)
|| (rectangle.Right >= _Left && rectangle.Right <= _Right))
&&
((rectangle.Top <= _Top && rectangle.Top >= _Bottom)