1use super::image::CoordinateIterator;
2
3#[derive(Debug, Clone)]
4pub struct Scissor {
5 pub x: usize,
6 pub y: usize,
7
8 pub width: usize,
9 pub height: usize,
10}
11
12impl Scissor {
13 pub fn coordinates(&self) -> CoordinateIterator {
14 CoordinateIterator::new(self.x, self.y, self.width, self.height)
15 }
16
17 pub fn contains(&self, x: usize, y: usize) -> bool {
18 let x1 = self.x + self.width;
19 let y1 = self.y + self.height;
20
21 x >= self.x && x < x1 && y >= self.y && y < y1
22 }
23
24 pub fn intersect_with(&self, other: &Scissor) -> Option<Scissor> {
25 let x0 = self.x.max(other.x);
26 let y0 = self.y.max(other.y);
27
28 let x1 = (self.x + self.width).min(other.x + other.width);
29 let y1 = (self.y + self.height).min(other.y + other.height);
30
31 if x1 <= x0 || y1 <= y0 {
32 None
33 } else {
34 Some(Scissor {
35 x: x0,
36 y: y0,
37
38 width: x1 - x0,
39 height: y1 - y0,
40 })
41 }
42 }
43}