1- use crate :: Position ;
21use leaflet:: LatLng ;
32use leaflet:: LatLngBounds ;
43
4+ use crate :: Position ;
5+
6+ /// Represents a geographical area defined by its northeast and southwest corners.
7+ ///
8+ /// The `Bounds` struct is used to define rectangular areas on a map. It provides methods to calculate
9+ /// the center, size, and check for containment or intersection with other bounds.
10+ ///
11+ /// # Fields
12+ ///
13+ /// - `ne_corner`: The northeast corner of the bounds.
14+ /// - `sw_corner`: The southwest corner of the bounds.
515#[ derive( Debug , Default , Clone , Copy , PartialEq ) ]
616pub struct Bounds {
717 pub ne_corner : Position ,
818 pub sw_corner : Position ,
919}
1020
1121impl Bounds {
22+ /// Creates a new `Bounds` instance with the given northeast and southwest corners.
23+ ///
24+ /// # Arguments
25+ ///
26+ /// - `ne_corner`: The northeast corner of the bounds.
27+ /// - `sw_corner`: The southwest corner of the bounds.
1228 pub fn new ( ne_corner : Position , sw_corner : Position ) -> Self {
1329 Self {
1430 ne_corner,
1531 sw_corner,
1632 }
1733 }
1834
35+ /// Gets the center of the bounds.
1936 pub fn get_center ( & self ) -> Position {
2037 Position {
2138 lat : ( self . ne_corner . lat + self . sw_corner . lat ) / 2.0 ,
2239 lng : ( self . ne_corner . lng + self . sw_corner . lng ) / 2.0 ,
2340 }
2441 }
2542
43+ /// Gets the southwest corner of the bounds.
2644 pub fn get_bottom_left ( & self ) -> Position {
2745 Position :: new ( self . sw_corner . lat , self . sw_corner . lng )
2846 }
2947
48+ /// Gets the northeast corner of the bounds.
3049 pub fn get_top_right ( & self ) -> Position {
3150 Position :: new ( self . ne_corner . lat , self . ne_corner . lng )
3251 }
3352
53+ /// Gets the northwest corner of the bounds.
3454 pub fn get_top_left ( & self ) -> Position {
3555 Position :: new ( self . ne_corner . lat , self . sw_corner . lng )
3656 }
3757
58+ /// Gets the southeast corner of the bounds.
3859 pub fn get_bottom_right ( & self ) -> Position {
3960 Position :: new ( self . sw_corner . lat , self . ne_corner . lng )
4061 }
4162
63+ /// Gets the size of the bounds.
4264 pub fn get_size ( & self ) -> Position {
4365 Position {
4466 lat : ( self . ne_corner . lat - self . sw_corner . lat ) . abs ( ) ,
4567 lng : ( self . ne_corner . lng - self . sw_corner . lng ) . abs ( ) ,
4668 }
4769 }
4870
71+ /// Returns true if the rectangle contains the given bounds.
72+ /// A rectangle contains another bounds if it contains all of its points.
73+ ///
74+ /// # Arguments
75+ ///
76+ /// - `position`: The position to check for containment.
4977 pub fn contains ( & self , position : Position ) -> bool {
5078 self . sw_corner . lat <= position. lat
5179 && self . ne_corner . lat >= position. lat
5280 && self . sw_corner . lng <= position. lng
5381 && self . ne_corner . lng >= position. lng
5482 }
5583
56- // Returns true if the rectangle intersects the given bounds. Two bounds intersect if they have at least one point in common.
84+ /// Returns true if the rectangle intersects the given bounds.
85+ /// Two bounds intersect if they have at least one point in common.
86+ ///
87+ /// # Arguments
88+ ///
89+ /// - `other`: The bounds to check for intersection.
5790 pub fn intersects ( & self , other : Bounds ) -> bool {
5891 let lat_overlap =
5992 self . ne_corner . lat >= other. sw_corner . lat && self . sw_corner . lat <= other. ne_corner . lat ;
@@ -63,7 +96,12 @@ impl Bounds {
6396 lat_overlap && lng_overlap
6497 }
6598
66- // Returns true if the rectangle overlaps the given bounds. Two bounds overlap if their intersection is an area.
99+ /// Returns true if the rectangle overlaps the given bounds.
100+ /// Two bounds overlap if their intersection is an area.
101+ ///
102+ /// # Arguments
103+ ///
104+ /// - `other`: The bounds to check for overlap.
67105 pub fn overlaps ( & self , other : Bounds ) -> bool {
68106 let lat_overlap =
69107 self . ne_corner . lat > other. sw_corner . lat && self . sw_corner . lat < other. ne_corner . lat ;
@@ -73,6 +111,7 @@ impl Bounds {
73111 lat_overlap && lng_overlap
74112 }
75113
114+ /// Returns true if the bounds are valid.
76115 pub fn is_valid ( & self ) -> bool {
77116 self . ne_corner . lat <= 90.0
78117 && self . ne_corner . lat >= -90.0
@@ -86,6 +125,7 @@ impl Bounds {
86125 && self . ne_corner . lng >= self . sw_corner . lng
87126 }
88127
128+ /// Returns a new bounds padded by the given ratio.
89129 pub fn pad ( & self , buffer_ratio : f64 ) -> Bounds {
90130 let lat_diff = self . ne_corner . lat - self . sw_corner . lat ;
91131 let lng_diff = self . ne_corner . lng - self . sw_corner . lng ;
@@ -97,6 +137,7 @@ impl Bounds {
97137 }
98138 }
99139
140+ /// Checks if the bounds are equal to the given bounds.
100141 pub fn equals ( & self , other : Bounds ) -> bool {
101142 self . ne_corner == other. ne_corner && self . sw_corner == other. sw_corner
102143 }
0 commit comments