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
use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl Point {
pub fn new(x: i32, y: i32) -> Point {
assert!(x >= 0);
assert!(y >= 0);
Point{
x: x,
y: y
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Size {
pub width: i32,
pub height: i32,
}
impl Size {
pub fn new(width: i32, height: i32) -> Size {
assert!(width >= 0);
assert!(height >= 0);
Size{
width: width,
height: height,
}
}
}
impl fmt::Display for Size {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}x{}", self.width, self.height)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Rect {
pub top_left: Point,
pub top_right: Point,
pub bottom_right: Point,
pub bottom_left: Point,
pub size: Size,
}
impl Rect {
pub fn from_size(origin: Point, size: Size) -> Rect {
let top_right = Point::new(origin.x + size.width, origin.y);
let bottom_left = Point::new(origin.x, origin.y + size.height);
let bottom_right = Point::new(top_right.x, bottom_left.y);
Rect{
top_left: origin,
top_right: top_right,
bottom_left: bottom_left,
bottom_right: bottom_right,
size: size
}
}
pub fn from_points(top_left: Point, bottom_right: Point) -> Rect {
assert!(bottom_right.x >= top_left.x);
assert!(bottom_right.y >= top_left.y);
let size = Size::new(bottom_right.x - top_left.x, bottom_right.y - top_left.y);
Rect::from_size(top_left, size)
}
pub fn from_values(x: i32, y: i32, width: i32, height: i32) -> Rect {
let origin = Point::new(x, y);
let size = Size::new(width, height);
Rect::from_size(origin, size)
}
pub fn from_point_values(top_left_x: i32, top_left_y: i32, bottom_right_x: i32, bottom_right_y: i32) -> Rect {
let top_left = Point::new(top_left_x, top_left_y);
let bottom_right = Point::new(bottom_right_x, bottom_right_y);
Rect::from_points(top_left, bottom_right)
}
}