Skip to main content

convex_hull_2d

Function convex_hull_2d 

Source
pub fn convex_hull_2d(points: &Matrix) -> Result<ConvexHull>
Expand description

The convex hull of a set of points in the plane, by the Graham scan.

points must have two columns (x and y) and one point per row. The corners are reported in order along the boundary; collinear points on the sides are left out. Degenerate inputs are fine: one point gives a one-corner hull, collinear points give the two extremes, no points an empty hull.

Binds igraph_convex_hull_2d. Time complexity: O(n log n).

See also Graph::layout_circle and the other layouts of crate::layout, whose coordinate matrices can be passed directly.

§Errors

ErrorKind::InvalidValue if points does not have exactly two columns, or has NaN or infinite coordinates.

§Examples

use igraph::{misc, prelude::*};
let pts = Matrix::from_rows(&[[0.0, 0.0], [2.0, 0.0], [1.0, 1.0], [2.0, 2.0], [0.0, 2.0]])?;
let hull = misc::convex_hull_2d(&pts)?;
let mut corners = hull.vertices.clone();
corners.sort();
assert_eq!(corners, vec![0, 1, 3, 4]); // the center point (row 2) is inside
assert_eq!(hull.area(), 4.0);
assert_eq!(hull.perimeter(), 8.0);