fn centroid(&self) -> Option<Point> {

        if self.exterior.points.is_empty() {
            return None;
        } else if self.exterior.points.len() == 1 {
            return Some(self.exterior.points[0]);
        }

        let external_centroid = simple_polygon_centroid(&self.exterior)?;

        if !self.interiors.is_empty() {
            let external_area = simple_polygon_area(&self.exterior).abs();
            // accumulate interior Polygons
            let (totals_x, totals_y, internal_area) = self
                .interiors
                .iter()
                .filter_map(|ring| {
                    let area = simple_polygon_area(ring).abs();
                    let centroid = simple_polygon_centroid(ring)?;
                    Some((centroid.x * area, centroid.y * area, area))
                })
                .fold((0.0_f64, 0.0_f64, 0.0_f64), |accum, val| {
                    (accum.0 + val.0, accum.1 + val.1, accum.2 + val.2)
                });

            return Some(Point {
                x: ((external_centroid.x * external_area) - totals_x) / (external_area - internal_area),
                y: ((external_centroid.y * external_area) - totals_y) / (external_area - internal_area),
            });
        }

        Some(external_centroid)
    }
// Calculate the centroid of a Polygon.
// We distinguish between a simple polygon, which has no interior rings (holes),
// and a complex polygon, which has one or more interior rings.
// A complex polygon's centroid is the weighted average of its
// exterior shell centroid and the centroids of the interior ring(s).
// Both the shell and the ring(s) are considered simple polygons for the purposes of
// this calculation.
//
// See here for a formula: http://math.stackexchange.com/a/623849
// See here for detail on alternative methods: https://fotino.me/calculating-centroids/
Calculate the centroid of a Polygon.
We distinguish between a simple polygon, which has no interior rings (holes),
and a complex polygon, which has one or more interior rings.
A complex polygon's centroid is the weighted average of its
exterior shell centroid and the centroids of the interior ring(s).
Both the shell and the ring(s) are considered simple polygons for the purposes of
this calculation.


Calculate the centroid of a Polygon . We distinguish between a simple polygon which has no interior rings ( holes ) and a complex polygon which has one or more interior rings . A complex polygon ' s centroid is the weighted average of its exterior shell centroid and the centroids of the interior ring ( s ) . Both the shell and the ring ( s ) are considered simple polygons for the purposes of this calculation .
fn centroid(&self) -> Option<Point> {
        if self.exterior.points.is_empty() {
            return None;
        } else if self.exterior.points.len() == 1 {
            return Some(self.exterior.points[0]);
        }
        let external_centroid = simple_polygon_centroid(&self.exterior)?;
        if !self.interiors.is_empty() {
            let external_area = simple_polygon_area(&self.exterior).abs();
            // accumulate interior Polygons
            let (totals_x, totals_y, internal_area) = self
                .interiors
                .iter()
                .filter_map(|ring| {
                    let area = simple_polygon_area(ring).abs();
                    let centroid = simple_polygon_centroid(ring)?;
                    Some((centroid.x * area, centroid.y * area, area))
                })
                .fold((0.0_f64, 0.0_f64, 0.0_f64), |accum, val| {
                    (accum.0 + val.0, accum.1 + val.1, accum.2 + val.2)
                });
            return Some(Point {
                x: ((external_centroid.x * external_area) - totals_x) / (external_area - internal_area),
                y: ((external_centroid.y * external_area) - totals_y) / (external_area - internal_area),
            });
        }
        Some(external_centroid)
    }
fn centroid ( & self ) -> Option < Point > { if self . exterior . points . is_empty ( ) { return None ; } else if self . exterior . points . len ( ) == 1 { return Some ( self . exterior . points [ 0 ] ) ; } let external_centroid = simple_polygon_centroid ( & self . exterior ) ? ; if ! self . interiors . is_empty ( ) { let external_area = simple_polygon_area ( & self . exterior ) . abs ( ) ; // accumulate interior Polygons let ( totals_x , totals_y , internal_area ) = self . interiors . iter ( ) . filter_map ( | ring | { let area = simple_polygon_area ( ring ) . abs ( ) ; let centroid = simple_polygon_centroid ( ring ) ? ; Some ( ( centroid . x * area , centroid . y * area , area ) ) } ) . fold ( ( 0.0_f64 , 0.0_f64 , 0.0_f64 ) , | accum , val | { ( accum . 0 + val . 0 , accum . 1 + val . 1 , accum . 2 + val . 2 ) } ) ; return Some ( Point { x : ( ( external_centroid . x * external_area ) - totals_x ) / ( external_area - internal_area ) , y : ( ( external_centroid . y * external_area ) - totals_y ) / ( external_area - internal_area ) , } ) ; } Some ( external_centroid ) }
Calculate the centroid of a Polygon.
Calculate the centroid of a Polygon .
&self
&self