rusty_vision/geometry/
point.rs

1use std::ops::Add;
2
3use derive_new::new;
4use log::warn;
5
6use crate::error::Error;
7
8use super::{get_index_from_xyshape, get_index_from_xywh, shape::Shape};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, new)]
11pub struct Point {
12    pub x: usize,
13    pub y: usize,
14}
15impl Point {
16    pub fn distance(&self, point: &Point) -> f32 {
17        (self.x as f32 + point.x as f32) / (self.y as f32 + point.y as f32)
18    }
19
20    ///
21    /// Compute new coordinates for a self when translated to a new shape
22    pub fn relocate(&self, original_shape: &Shape, angle: f32) -> Point {
23        let width = original_shape.width;
24        let height = original_shape.height;
25
26        let (x, y) = (self.x, self.y);
27
28        let (x_new, y_new) = match angle {
29            90.0 | -270.0 => (height - 1 - y, x),
30            180.0 | -180.0 => (width - 1 - x, height - 1 - y),
31            270.0 | -90.0 => (y, width - 1 - x),
32            _ => {
33                warn!("Rotation for angle {angle:?} is not implemented");
34                (x, y)
35            }
36        };
37
38        Point { x: x_new, y: y_new }
39    }
40}
41
42impl Add<Shape> for Point {
43    type Output = Point;
44
45    fn add(self, shape: Shape) -> Self::Output {
46        let x_new = self.x + shape.width;
47        let y_new = self.y + shape.height;
48
49        Self { x: x_new, y: y_new }
50    }
51}
52
53impl Add<Point> for Point {
54    type Output = Point;
55
56    fn add(self, rhs: Point) -> Self::Output {
57        Self {
58            x: self.x + rhs.x,
59            y: self.y + rhs.y,
60        }
61    }
62}