author | alfadur |
Thu, 01 Nov 2018 03:38:13 +0300 | |
changeset 14054 | 3185fb34f3b5 |
parent 14052 | 9c817b2eedae |
child 14059 | c6745a1c827a |
permissions | -rw-r--r-- |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
1 |
use std::cmp; |
13941 | 2 |
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
3 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
4 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
5 |
pub struct Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
6 |
pub x: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
7 |
pub y: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
8 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
9 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
10 |
impl Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
11 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
12 |
pub fn new(x: i32, y: i32) -> Self { |
13941 | 13 |
Self { x, y } |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
14 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
15 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
16 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
17 |
pub fn zero() -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
18 |
Self::new(0, 0) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
19 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
20 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
21 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
22 |
pub fn signum(self) -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
23 |
Self::new(self.x.signum(), self.y.signum()) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
24 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
25 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
26 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
27 |
pub fn abs(self) -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
28 |
Self::new(self.x.abs(), self.y.abs()) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
29 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
30 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
31 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
32 |
pub fn dot(self, other: Point) -> i32 { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
33 |
self.x * other.x + self.y * other.y |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
34 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
35 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
36 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
37 |
pub fn max_norm(self) -> i32 { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
38 |
std::cmp::max(self.x.abs(), self.y.abs()) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
39 |
} |
14031 | 40 |
|
41 |
#[inline] |
|
42 |
pub fn transform(self, matrix: &[i32; 4]) -> Self { |
|
43 |
Point::new(matrix[0] * self.x + matrix[1] * self.y, |
|
44 |
matrix[2] * self.x + matrix[3] * self.y) |
|
45 |
} |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
46 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
47 |
|
14032 | 48 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
49 |
pub struct Size { |
|
50 |
pub width: usize, |
|
51 |
pub height: usize, |
|
52 |
} |
|
53 |
||
54 |
impl Size { |
|
55 |
#[inline] |
|
56 |
pub fn new(width: usize, height: usize) -> Self { |
|
57 |
Size { width, height } |
|
58 |
} |
|
59 |
||
60 |
#[inline] |
|
61 |
pub fn square(size: usize) -> Self { |
|
62 |
Size { width: size, height: size } |
|
63 |
} |
|
64 |
||
65 |
#[inline] |
|
66 |
pub fn area(&self) -> usize { |
|
67 |
self.width * self.height |
|
68 |
} |
|
69 |
||
70 |
#[inline] |
|
71 |
pub fn linear_index(&self, x: usize, y: usize) -> usize { |
|
72 |
y * self.width + x |
|
73 |
} |
|
74 |
||
75 |
#[inline] |
|
76 |
pub fn is_power_of_two(&self) -> bool { |
|
77 |
self.width.is_power_of_two() && self.height.is_power_of_two() |
|
78 |
} |
|
79 |
||
80 |
#[inline] |
|
14052 | 81 |
pub fn next_power_of_two(&self) -> Self { |
82 |
Self { |
|
83 |
width: self.width.next_power_of_two(), |
|
84 |
height: self.height.next_power_of_two() |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
#[inline] |
|
14032 | 89 |
pub fn to_mask(&self) -> SizeMask { |
90 |
SizeMask::new(*self) |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
pub struct SizeMask{ size: Size } |
|
95 |
||
96 |
impl SizeMask { |
|
97 |
#[inline] |
|
98 |
pub fn new(size: Size) -> Self { |
|
99 |
assert!(size.is_power_of_two()); |
|
100 |
let size = Size { |
|
101 |
width: !(size.width - 1), |
|
102 |
height: !(size.height - 1) |
|
103 |
}; |
|
104 |
SizeMask { size } |
|
105 |
} |
|
106 |
||
107 |
#[inline] |
|
108 |
pub fn contains_x<T: Into<usize>>(&self, x: T) -> bool { |
|
109 |
(self.size.width & x.into()) == 0 |
|
110 |
} |
|
111 |
||
112 |
#[inline] |
|
113 |
pub fn contains_y<T: Into<usize>>(&self, y: T) -> bool { |
|
114 |
(self.size.height & y.into()) == 0 |
|
115 |
} |
|
116 |
||
117 |
#[inline] |
|
118 |
pub fn contains(&self, point: Point) -> bool { |
|
119 |
self.contains_x(point.x as usize) && self.contains_y(point.y as usize) |
|
120 |
} |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
121 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
122 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
123 |
macro_rules! bin_op_impl { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
124 |
($op: ty, $name: tt) => { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
125 |
impl $op for Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
126 |
type Output = Self; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
127 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
128 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
129 |
fn $name(self, rhs: Self) -> Self::Output { |
13941 | 130 |
Self::new(self.x.$name(rhs.x), self.y.$name(rhs.y)) |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
131 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
132 |
} |
13941 | 133 |
}; |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
134 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
135 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
136 |
macro_rules! bin_assign_op_impl { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
137 |
($op: ty, $name: tt) => { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
138 |
impl $op for Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
139 |
#[inline] |
13941 | 140 |
fn $name(&mut self, rhs: Self) { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
141 |
self.x.$name(rhs.x); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
142 |
self.y.$name(rhs.y); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
143 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
144 |
} |
13941 | 145 |
}; |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
146 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
147 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
148 |
bin_op_impl!(Add, add); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
149 |
bin_op_impl!(Sub, sub); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
150 |
bin_op_impl!(Mul, mul); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
151 |
bin_op_impl!(Div, div); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
152 |
bin_assign_op_impl!(AddAssign, add_assign); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
153 |
bin_assign_op_impl!(SubAssign, sub_assign); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
154 |
bin_assign_op_impl!(MulAssign, mul_assign); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
155 |
bin_assign_op_impl!(DivAssign, div_assign); |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
156 |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
157 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
158 |
pub struct Rect { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
159 |
pub x: i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
160 |
pub y: i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
161 |
pub width: u32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
162 |
pub height: u32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
163 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
164 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
165 |
impl Rect { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
166 |
#[inline] |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
167 |
pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
168 |
Self { x, y, width, height } |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
169 |
} |
14054
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
170 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
171 |
#[inline] |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
172 |
pub fn size(&self) -> Size { |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
173 |
Size::new(self.width as usize, self.height as usize) |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
174 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
175 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
176 |
#[inline] |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
177 |
pub fn area(&self) -> usize { |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
178 |
self.size().area() |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
179 |
} |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
180 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
181 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
182 |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
183 |
pub struct LinePoints { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
184 |
accumulator: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
185 |
direction: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
186 |
sign: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
187 |
current: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
188 |
total_steps: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
189 |
step: i32, |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
190 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
191 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
192 |
impl LinePoints { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
193 |
pub fn new(from: Point, to: Point) -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
194 |
let dir = to - from; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
195 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
196 |
Self { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
197 |
accumulator: Point::zero(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
198 |
direction: dir.abs(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
199 |
sign: dir.signum(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
200 |
current: from, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
201 |
total_steps: dir.max_norm(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
202 |
step: 0, |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
203 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
204 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
205 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
206 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
207 |
impl Iterator for LinePoints { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
208 |
type Item = Point; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
209 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
210 |
fn next(&mut self) -> Option<Self::Item> { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
211 |
if self.step <= self.total_steps { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
212 |
self.accumulator += self.direction; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
213 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
214 |
if self.accumulator.x > self.total_steps { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
215 |
self.accumulator.x -= self.total_steps; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
216 |
self.current.x += self.sign.x; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
217 |
} |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
218 |
if self.accumulator.y > self.total_steps { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
219 |
self.accumulator.y -= self.total_steps; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
220 |
self.current.y += self.sign.y; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
221 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
222 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
223 |
self.step += 1; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
224 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
225 |
Some(self.current) |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
226 |
} else { |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
227 |
None |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
228 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
229 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
230 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
231 |
|
13941 | 232 |
pub struct ArcPoints { |
233 |
point: Point, |
|
234 |
step: i32, |
|
235 |
} |
|
236 |
||
237 |
impl ArcPoints { |
|
238 |
pub fn new(radius: i32) -> Self { |
|
239 |
Self { |
|
240 |
point: Point::new(0, radius), |
|
241 |
step: 3 - 2 * radius, |
|
242 |
} |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
impl Iterator for ArcPoints { |
|
247 |
type Item = Point; |
|
248 |
||
249 |
fn next(&mut self) -> Option<Self::Item> { |
|
250 |
if self.point.x < self.point.y { |
|
251 |
let result = self.point; |
|
252 |
||
253 |
if self.step < 0 { |
|
254 |
self.step += self.point.x * 4 + 6; |
|
255 |
} else { |
|
256 |
self.step += (self.point.x - self.point.y) * 4 + 10; |
|
257 |
self.point.y -= 1; |
|
258 |
} |
|
259 |
||
260 |
self.point.x += 1; |
|
261 |
||
262 |
Some(result) |
|
263 |
} else if self.point.x == self.point.y { |
|
13947 | 264 |
self.point.x += 1; |
13950 | 265 |
|
13941 | 266 |
Some(self.point) |
267 |
} else { |
|
268 |
None |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
||
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
273 |
pub struct EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
274 |
vector: Point, |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
275 |
iteration: u8, |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
276 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
277 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
278 |
impl EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
279 |
pub fn new(vector: Point) -> Self { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
280 |
Self { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
281 |
vector, |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
282 |
iteration: if vector.x == vector.y { 4 } else { 8 }, |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
283 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
284 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
285 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
286 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
287 |
impl Iterator for EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
288 |
type Item = Point; |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
289 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
290 |
fn next(&mut self) -> Option<Self::Item> { |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
291 |
if self.iteration > 0 { |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
292 |
self.vector.x = -self.vector.x; |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
293 |
if self.iteration & 1 == 0 { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
294 |
self.vector.y = -self.vector.y; |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
295 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
296 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
297 |
if self.iteration == 4 { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
298 |
std::mem::swap(&mut self.vector.x, &mut self.vector.y); |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
299 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
300 |
|
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
301 |
self.iteration -= 1; |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
302 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
303 |
Some(self.vector) |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
304 |
} else { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
305 |
None |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
306 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
307 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
308 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
309 |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
310 |
#[cfg(test)] |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
311 |
mod tests { |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
312 |
use super::*; |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
313 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
314 |
fn get_points(coords: &[(i32, i32)]) -> Vec<Point> { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
315 |
coords.iter().map(|(x, y)| Point::new(*x, *y)).collect() |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
316 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
317 |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
318 |
#[test] |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
319 |
fn line_basic() { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
320 |
let line = LinePoints::new(Point::new(0, 0), Point::new(3, 3)); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
321 |
let v = get_points(&[(0, 0), (1, 1), (2, 2), (3, 3), (123, 456)]); |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
322 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
323 |
for (&a, b) in v.iter().zip(line) { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
324 |
assert_eq!(a, b); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
325 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
326 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
327 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
328 |
#[test] |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
329 |
fn line_skewed() { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
330 |
let line = LinePoints::new(Point::new(0, 0), Point::new(5, -7)); |
13941 | 331 |
let v = get_points(&[ |
332 |
(0, 0), |
|
333 |
(1, -1), |
|
334 |
(2, -2), |
|
335 |
(2, -3), |
|
336 |
(3, -4), |
|
337 |
(4, -5), |
|
338 |
(4, -6), |
|
339 |
(5, -7), |
|
340 |
]); |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
341 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
342 |
for (&a, b) in v.iter().zip(line) { |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
343 |
assert_eq!(a, b); |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
344 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
345 |
} |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
346 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
347 |
#[test] |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
348 |
fn equidistant_full() { |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
349 |
let n = EquidistantPoints::new(Point::new(1, 3)); |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
350 |
let v = get_points(&[ |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
351 |
(-1, -3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
352 |
(1, -3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
353 |
(-1, 3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
354 |
(1, 3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
355 |
(-3, -1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
356 |
(3, -1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
357 |
(-3, 1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
358 |
(3, 1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
359 |
(123, 456), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
360 |
]); |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
361 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
362 |
for (&a, b) in v.iter().zip(n) { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
363 |
assert_eq!(a, b); |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
364 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
365 |
} |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
366 |
|
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
367 |
#[test] |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
368 |
fn equidistant_half() { |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
369 |
let n = EquidistantPoints::new(Point::new(2, 2)); |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
370 |
let v = get_points(&[(-2, -2), (2, -2), (-2, 2), (2, 2), (123, 456)]); |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
371 |
|
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
372 |
for (&a, b) in v.iter().zip(n) { |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
373 |
assert_eq!(a, b); |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
374 |
} |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
375 |
} |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
376 |
} |