Skip to main content

polygon_centroid

Function polygon_centroid 

Source
pub fn polygon_centroid<T>(polygon: &Polygon<T>) -> Point<T, T>
where T: Clone + Copy + Add<Output = T> + Div<Output = T> + From<i32> + Zero + Num + AddAssign + Ord,
Expand description

Computes the centroid (geometric center) of a polygon

The centroid is the arithmetic mean of all vertex positions:

$$\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i,\qquad \bar{y} = \frac{1}{n}\sum_{i=1}^{n} y_i$$

§Arguments

  • polygon - The polygon

§Returns

The centroid point of the polygon

§Examples

use physdes::{Point, algorithms::polygon_centroid};
use physdes::Polygon;

let points = vec![
    Point::new(0, 0),
    Point::new(4, 0),
    Point::new(4, 3),
];
let polygon = Polygon::new(&points);

let centroid = polygon_centroid(&polygon);
// For a triangle, centroid is at the average of vertices