Struct geographiclib_rs::PolygonArea
source · pub struct PolygonArea<'a> { /* private fields */ }
Expand description
Compute the perimeter and area of a polygon on a Geodesic.
Implementations§
source§impl<'a> PolygonArea<'a>
impl<'a> PolygonArea<'a>
PolygonArea can be used to compute the perimeter and area of a polygon on a Geodesic.
§Example
use geographiclib_rs::{Geodesic, PolygonArea, Winding};
let g = Geodesic::wgs84();
let mut pa = PolygonArea::new(&g, Winding::CounterClockwise);
pa.add_point(0.0, 0.0);
pa.add_point(0.0, 1.0);
pa.add_point(1.0, 1.0);
pa.add_point(1.0, 0.0);
let (perimeter, area, _num) = pa.compute(false);
use approx::assert_relative_eq;
assert_relative_eq!(perimeter, 443770.917248302);
assert_relative_eq!(area, 12308778361.469452);
sourcepub fn new(geoid: &'a Geodesic, winding: Winding) -> PolygonArea<'_>
pub fn new(geoid: &'a Geodesic, winding: Winding) -> PolygonArea<'_>
Create a new PolygonArea using a Geodesic.
sourcepub fn add_edge(&mut self, azimuth: f64, distance: f64)
pub fn add_edge(&mut self, azimuth: f64, distance: f64)
Add an edge to the polygon using an azimuth (in degrees) and a distance (in meters). This can only be called after at least one point has been added.
§Panics
Panics if no points have been added yet.
sourcepub fn compute(self, sign: bool) -> (f64, f64, usize)
pub fn compute(self, sign: bool) -> (f64, f64, usize)
Consumes the PolygonArea and returns the following tuple:
- 0: Perimeter in (meters) of the polygon.
- 1: Area (meters²) of the polygon.
- 2: Number of points added to the polygon.
§Parameters
sign
: Whether to allow negative values for the area.true
: Interpret an inversely wound polygon to be a “negative” area. This will produce incorrect results if the polygon covers over half the geodesic. See “Interpreting negative area values” below.false
: Always return a positive area. Inversely wound polygons are treated as if they are always wound the same way as the winding specified during creation of the PolygonArea. This is useful if you are dealing with very large polygons that might cover over half the geodesic, or if you are certain of your winding.
§Interpreting negative area values
A negative value can mean one of two things:
- The winding of the polygon is opposite the winding specified during creation of the PolygonArea.
- The polygon is larger than half the planet. In this case, to get the final area of the polygon, add the area of the planet to the result. If you expect to be dealing with polygons of this size pass
signed = false
tocompute()
to get the correct result.
§Large polygon example
use geographiclib_rs::{Geodesic, PolygonArea, Winding};
let g = Geodesic::wgs84();
// Describe a polygon that covers all of the earth EXCEPT this small square.
// The outside of the polygon is in this square, the inside of the polygon is the rest of the earth.
let mut pa = PolygonArea::new(&g, Winding::CounterClockwise);
pa.add_point(0.0, 0.0);
pa.add_point(1.0, 0.0);
pa.add_point(1.0, 1.0);
pa.add_point(0.0, 1.0);
let (_perimeter, area, _count) = pa.compute(false);
// Over 5 trillion square meters!
assert_eq!(area, 510053312945726.94);
Trait Implementations§
source§impl<'a> Clone for PolygonArea<'a>
impl<'a> Clone for PolygonArea<'a>
source§fn clone(&self) -> PolygonArea<'a>
fn clone(&self) -> PolygonArea<'a>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl<'a> RefUnwindSafe for PolygonArea<'a>
impl<'a> Send for PolygonArea<'a>
impl<'a> Sync for PolygonArea<'a>
impl<'a> Unpin for PolygonArea<'a>
impl<'a> UnwindSafe for PolygonArea<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more