-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
241 lines (201 loc) · 5.46 KB
/
lib.rs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use std::ops;
use std::slice::Iter;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Color(pub u32, pub u32, pub u32);
impl Color {
pub fn lighter(&self, percentage: f64) -> Color {
if percentage > 1.0 {
panic!("Percentage should be less than 1!");
}
Self (
(self.0 as f64 * (1.0 + percentage)).floor() as u32,
(self.1 as f64 * (1.0 + percentage)).floor() as u32,
(self.2 as f64 * (1.0 + percentage)).floor() as u32,
)
}
pub fn darker(&self, percentage: f64) -> Color {
if percentage > 1.0 {
panic!("Percentage should be less than 1!");
}
Self (
(self.0 as f64 * percentage).floor() as u32,
(self.1 as f64 * percentage).floor() as u32,
(self.2 as f64 * percentage).floor() as u32,
)
}
}
pub struct PPM {
width: i32,
height: i32,
bounds: Rect,
buf: Vec<Color>,
}
impl PPM {
pub fn new(width: i32, height: i32, init_color: Color) -> Self {
Self {
width,
height,
bounds: Rect(0, 0, width, height),
buf: vec![init_color; (width * height) as usize],
}
}
pub fn set(
&mut self,
&Vec2D(i, j): &Vec2D,
color: Color,
) {
if !self.bounds.contains(Vec2D(i, j)) {
panic!("Point ({}, {}) would be out of bounds ({:?})!", i, j, self.bounds);
}
self.buf[(j * self.width + i) as usize] = color;
}
pub fn get(
&self,
&Vec2D(i, j): &Vec2D,
) -> Color {
self.buf[(j * self.width + i) as usize]
}
pub fn draw_rectangle(
&mut self,
&rect: &Rect,
color: Color,
) {
for v in rect.into_iter() {
self.set(&v, color);
}
}
pub fn print(&self) {
println!("P3\n{} {}\n255\n", self.width, self.height);
for j in 0..self.height {
for i in 0..self.width {
let Color(r, g, b) = self.get(&Vec2D(i, j));
println!("{} {} {}", r, g, b);
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Vec2D(pub i32, pub i32);
impl ops::Mul<i32> for Vec2D {
type Output = Vec2D;
fn mul(self, t: i32) -> Vec2D {
let Vec2D(x, y) = self;
Vec2D(x * t, y * t)
}
}
impl ops::Mul<Vec2D> for i32 {
type Output = Vec2D;
fn mul(self, Vec2D(x, y): Vec2D) -> Vec2D {
Vec2D(x * self, y * self)
}
}
impl ops::Add<Vec2D> for Vec2D {
type Output = Vec2D;
fn add(self, Vec2D(x2, y2): Vec2D) -> Vec2D {
let Vec2D(x1, y1) = self;
Vec2D(x1 + x2, y1 + y2)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {N, S, E, W}
impl Direction {
pub fn iterator() -> Iter<'static, Direction> {
static DIRECTIONS: [Direction; 4] =
[Direction::N, Direction::S, Direction::E, Direction::W];
DIRECTIONS.iter()
}
pub fn dir(&self) -> Vec2D {
match self {
Direction::N => Vec2D(0, -1),
Direction::S => Vec2D(0, 1),
Direction::E => Vec2D(1, 0),
Direction::W => Vec2D(-1, 0),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Rect(pub i32, pub i32, pub i32, pub i32);
impl Rect {
pub fn distance_to(&self, &Rect(x2, y2, w2, h2): &Rect) -> Option<i32> {
let &Rect(x1, y1, w1, h1) = self;
vec![x2 - (x1 + w1), y2 - (y1 + h1), x1 - (x2 + w2), y1 - (y2 + h2)]
.iter()
.filter(|&&d| d >= 0)
.map(|&d| d)
.min()
}
pub fn contains(&self, Vec2D(i, j): Vec2D) -> bool {
let &Rect(x, y, w, h) = self;
x <= i && i < x + w && y <= j && j < y + h
}
pub fn x(&self) -> i32 {
self.0
}
pub fn y(&self) -> i32 {
self.1
}
pub fn width(&self) -> i32 {
self.2
}
pub fn height(&self) -> i32 {
self.3
}
}
impl IntoIterator for Rect {
type Item = Vec2D;
type IntoIter = RectIntoIterator;
fn into_iter(self) -> Self::IntoIter {
RectIntoIterator {
rect: self,
cur: Vec2D(self.0, self.1),
}
}
}
pub struct RectIntoIterator {
rect: Rect,
cur: Vec2D,
}
impl Iterator for RectIntoIterator {
type Item = Vec2D;
fn next(&mut self) -> Option<Vec2D> {
let &mut RectIntoIterator{ rect: Rect(x, y, w, h), cur: Vec2D(i, j) } = self;
if j == y + h {
None
} else if i == x + w - 1 {
self.cur = Vec2D(x, j + 1);
Some(Vec2D(i, j))
} else {
self.cur = Vec2D(i + 1, j);
Some(Vec2D(i, j))
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_square_rect() {
let points: Vec<Vec2D> = Rect(1, 1, 2, 2).into_iter().collect();
assert_eq!(points, vec![
Vec2D(1, 1),
Vec2D(2, 1),
Vec2D(1, 2),
Vec2D(2, 2),
]);
}
#[test]
fn test_rect_distance() {
let distance = Rect(1, 1, 2, 2).distance_to(&Rect(4, 2, 3, 2));
assert_eq!(distance, Some(1));
}
#[test]
fn test_rect_distance_same_rect() {
let distance = Rect(1, 1, 2, 2).distance_to(&Rect(1, 1, 2, 2));
assert_eq!(distance, None);
}
#[test]
fn test_rect_distance_overlap() {
let distance = Rect(1, 1, 3, 3).distance_to(&Rect(3, 3, 1, 1));
assert_eq!(distance, None);
}
}