logo
pub trait Centroid {
    type Output;

    fn centroid(&self) -> Self::Output;
}
Expand description

Calculation of the centroid. The centroid is the arithmetic mean position of all points in the shape. Informally, it is the point at which a cutout of the shape could be perfectly balanced on the tip of a pin. The geometric centroid of a convex object always lies in the object. A non-convex object might have a centroid that is outside the object itself.

Examples

use geo::Centroid;
use geo::{point, polygon};

// rhombus shaped polygon
let polygon = polygon![
    (x: -2., y: 1.),
    (x: 1., y: 3.),
    (x: 4., y: 1.),
    (x: 1., y: -1.),
    (x: -2., y: 1.),
];

assert_eq!(
    Some(point!(x: 1., y: 1.)),
    polygon.centroid(),
);

Required Associated Types

Required Methods

See: https://en.wikipedia.org/wiki/Centroid

Examples
use geo::Centroid;
use geo::{line_string, point};

let line_string = line_string![
    (x: 40.02f64, y: 116.34),
    (x: 40.02f64, y: 118.23),
];

assert_eq!(
    Some(point!(x: 40.02, y: 117.285)),
    line_string.centroid(),
);

Implementors

use geo::Centroid;
use geo::{MultiPoint, Point};

let empty: Vec<Point> = Vec::new();
let empty_multi_points: MultiPoint<_> = empty.into();
assert_eq!(empty_multi_points.centroid(), None);

let points: MultiPoint<_> = vec![(5., 1.), (1., 3.), (3., 2.)].into();
assert_eq!(points.centroid(), Some(Point::new(3., 2.)));