1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::{
    primitives::{Line, Point},
    Vector,
};
use std::iter::FromIterator;

/// Find the location on an object which is closest to a target point.
pub trait ClosestPoint {
    /// Calculate the closest point to `target`.
    fn closest_point(&self, target: Vector) -> Closest;
}

impl<'c, C: ClosestPoint + ?Sized> ClosestPoint for &'c C {
    fn closest_point(&self, target: Vector) -> Closest {
        (*self).closest_point(target)
    }
}

impl ClosestPoint for Vector {
    fn closest_point(&self, _target: Vector) -> Closest { Closest::One(*self) }
}

impl ClosestPoint for Point {
    fn closest_point(&self, _target: Vector) -> Closest {
        Closest::One(self.location)
    }
}

impl ClosestPoint for Line {
    fn closest_point(&self, target: Vector) -> Closest {
        if self.length() == 0.0 {
            return Closest::One(self.start);
        }

        let start = self.start;
        let displacement = self.displacement();

        // equation of the line: start + t * displacement, where 0 <= t <= 1

        let t = Vector::dot(target - start, displacement)
            / (self.length() * self.length());

        Closest::One(if t <= 0.0 {
            self.start
        } else if t >= 1.0 {
            self.end
        } else {
            start + t * displacement
        })
    }
}

/// An enum containing the different possible solutions for
/// [`ClosestPoint::closest_point()`].
#[derive(Debug, Clone, PartialEq)]
pub enum Closest {
    /// There are infinitely solutions.
    Infinite,
    One(Vector),
    Many(Vec<Vector>),
}

impl Closest {
    pub fn is_infinite(&self) -> bool {
        match self {
            Closest::Infinite => true,
            _ => false,
        }
    }

    pub fn points(&self) -> &[Vector] {
        match self {
            Closest::Infinite => &[],
            Closest::One(item) => std::slice::from_ref(item),
            Closest::Many(items) => &items,
        }
    }
}

impl FromIterator<Vector> for Closest {
    fn from_iter<I: IntoIterator<Item = Vector>>(iter: I) -> Closest {
        let items = Vec::from_iter(iter);

        match items.len() {
            0 => Closest::Infinite,
            1 => Closest::One(items[0]),
            _ => Closest::Many(items),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn on_the_line() {
        let start = Vector::new(1.0, 2.0);
        let end = Vector::new(3.0, 10.0);
        let line = Line::new(start, end);
        let midpoint = (start + end) / 2.0;

        let got = line.closest_point(midpoint);

        assert_eq!(got, Closest::One(midpoint));
    }

    #[test]
    fn closest_point_to_zero_length_line() {
        let start = Vector::new(1.0, 2.0);
        let line = Line::new(start, start);
        assert_eq!(line.length(), 0.0);
        let target = Vector::new(10.0, 0.0);

        let got = line.closest_point(target);

        assert_eq!(got, Closest::One(start));
    }

    #[test]
    fn away_from_the_line() {
        let start = Vector::new(0.0, 0.0);
        let end = Vector::new(10.0, 0.0);
        let line = Line::new(start, end);

        let got = line.closest_point(Vector::new(5.0, 5.0));

        assert_eq!(got, Closest::One(Vector::new(5.0, 0.0)));
    }

    #[test]
    fn past_the_end_of_the_line() {
        let start = Vector::new(0.0, 0.0);
        let end = Vector::new(10.0, 0.0);
        let line = Line::new(start, end);

        let got = line.closest_point(Vector::new(15.0, 5.0));

        assert_eq!(got, Closest::One(end));
    }

    #[test]
    fn before_the_start_of_the_line() {
        let start = Vector::new(0.0, 0.0);
        let end = Vector::new(10.0, 0.0);
        let line = Line::new(start, end);

        let got = line.closest_point(Vector::new(-5.0, 5.0));

        assert_eq!(got, Closest::One(start));
    }
}