Skip to main content

use_hull/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use use_point::Point2;
5
6/// Common convex hull algorithm families.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum HullAlgorithm {
9    /// The monotone-chain algorithm family.
10    MonotoneChain,
11    /// The gift-wrapping algorithm family.
12    GiftWrapping,
13    /// The quickhull algorithm family.
14    QuickHull,
15}
16
17impl HullAlgorithm {
18    /// Returns a stable algorithm name.
19    #[must_use]
20    pub const fn name(self) -> &'static str {
21        match self {
22            Self::MonotoneChain => "monotone-chain",
23            Self::GiftWrapping => "gift-wrapping",
24            Self::QuickHull => "quickhull",
25        }
26    }
27}
28
29/// A 2D convex hull point set descriptor.
30#[derive(Debug, Clone, PartialEq)]
31pub struct ConvexHull2 {
32    points: Vec<Point2>,
33}
34
35impl ConvexHull2 {
36    /// Creates a hull descriptor from input points.
37    #[must_use]
38    pub const fn new(points: Vec<Point2>) -> Self {
39        Self { points }
40    }
41
42    /// Returns the input points.
43    #[must_use]
44    pub fn points(&self) -> &[Point2] {
45        &self.points
46    }
47
48    /// Returns the number of input points.
49    #[must_use]
50    pub fn point_count(&self) -> usize {
51        self.points.len()
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::{ConvexHull2, HullAlgorithm};
58    use use_point::Point2;
59
60    #[test]
61    fn stores_hull_inputs() {
62        let hull = ConvexHull2::new(vec![Point2::origin(), Point2::new(1.0, 0.0)]);
63
64        assert_eq!(hull.point_count(), 2);
65        assert_eq!(hull.points()[1], Point2::new(1.0, 0.0));
66        assert_eq!(HullAlgorithm::MonotoneChain.name(), "monotone-chain");
67    }
68}