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
use alloc::vec::Vec;
use core::ops::{Add, Div, Mul, Sub};

use num_traits::Signed;

use super::greiner_hormann::Polygon;

#[inline]
pub fn difference<T>(subject: &[[T; 2]], clip: &[[T; 2]]) -> Vec<Vec<[T; 2]>>
where
    T: Clone + Signed + PartialEq + PartialOrd,
    for<'a, 'b> &'a T: Div<&'b T, Output = T>
        + Sub<&'b T, Output = T>
        + Add<&'b T, Output = T>
        + Mul<&'b T, Output = T>,
{
    let mut subject_polygon = Polygon::from(subject);
    let mut clip_polygon = Polygon::from(clip);
    subject_polygon.difference(&mut clip_polygon)
}

#[test]
fn difference_test() {
    let subject = [[1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]];
    let clip = [[0.5, -0.5], [0.5, 0.5], [-0.5, 0.5], [-0.5, -0.5]];
    assert_eq!(
        difference(&subject, &clip),
        [[
            [0.0, 0.5],
            [0.5, 0.5],
            [0.5, 0.0],
            [1.0, 0.0],
            [1.0, 1.0],
            [0.0, 1.0]
        ]]
    );
}