Skip to main content

irox_geometry/
geometry.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2025 IROX Contributors
3//
4
5use crate::rectangle::Rectangle;
6use crate::{LineSegment, Point, Polygon};
7use irox_tools::FloatIsh;
8
9#[derive(Debug, Clone, PartialEq)]
10pub enum GeometryType<T: FloatIsh> {
11    Point(Point<T>),
12    Line(LineSegment<T>),
13    Polygon(Polygon<T>),
14    Rectangle(Rectangle<T>),
15}
16impl<T: FloatIsh> Centroid<T> for GeometryType<T> {
17    fn centroid(&self) -> Point<T> {
18        todo!()
19    }
20}
21impl<T: FloatIsh> Geometry<T> for GeometryType<T> {
22    fn contains(&self, point: &Point<T>) -> bool {
23        match self {
24            GeometryType::Point(p) => p.contains(point),
25            GeometryType::Line(l) => l.contains(point),
26            GeometryType::Polygon(p) => p.contains(point),
27            GeometryType::Rectangle(r) => r.contains(point),
28        }
29    }
30
31    fn distance_to(&self, point: &Point<T>) -> T {
32        match self {
33            GeometryType::Point(p) => p.distance_to(point),
34            GeometryType::Line(l) => l.distance_to(point),
35            GeometryType::Polygon(p) => p.distance_to(point),
36            GeometryType::Rectangle(r) => r.distance_to(point),
37        }
38    }
39
40    fn intersects(&self, point: &Point<T>) -> bool {
41        match self {
42            GeometryType::Point(p) => p.intersects(point),
43            GeometryType::Line(l) => l.intersects(point),
44            GeometryType::Polygon(p) => p.intersects(point),
45            GeometryType::Rectangle(r) => r.intersects(point),
46        }
47    }
48
49    fn bounding_rectangle(&self) -> Rectangle<T> {
50        match self {
51            GeometryType::Point(p) => p.bounding_rectangle(),
52            GeometryType::Line(l) => l.bounding_rectangle(),
53            GeometryType::Polygon(p) => p.bounding_rectangle(),
54            GeometryType::Rectangle(r) => r.bounding_rectangle(),
55        }
56    }
57}
58
59pub trait Geometry<T: FloatIsh>: Centroid<T> {
60    fn contains(&self, point: &Point<T>) -> bool;
61    fn distance_to(&self, point: &Point<T>) -> T;
62    fn intersects(&self, point: &Point<T>) -> bool;
63    fn bounding_rectangle(&self) -> Rectangle<T>;
64}
65
66pub trait Centroid<T: FloatIsh> {
67    fn centroid(&self) -> Point<T>;
68}