Skip to main content

polygon_perimeter

Function polygon_perimeter 

Source
pub fn polygon_perimeter<T>(polygon: &Polygon<T>) -> T
where T: Clone + Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Zero + Num + AddAssign + Ord,
Expand description

Computes the perimeter of a polygon

Sums the Manhattan distance between consecutive vertices (including wrap-around):

$$P = \sum_{i=0}^{n-1} (|x_{i+1} - x_i| + |y_{i+1} - y_i|)$$

where vertex $n$ wraps to vertex $0$.

§Arguments

  • polygon - The polygon

§Returns

The perimeter length

§Examples

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

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

let perimeter = polygon_perimeter(&polygon);
assert_eq!(perimeter, 14); // 3 + 4 + 3 + 4