author | alfadur |
Fri, 02 Nov 2018 20:30:13 +0300 | |
changeset 14092 | 32a34bf70f65 |
parent 14088 | f483f844da98 |
child 14094 | d0b0d61b7d5e |
permissions | -rw-r--r-- |
14081 | 1 |
extern crate fpnum; |
2 |
||
3 |
use fpnum::distance; |
|
14092 | 4 |
use std::ops::{ |
5 |
Add, AddAssign, |
|
6 |
Div, DivAssign, |
|
7 |
Mul, MulAssign, |
|
8 |
Sub, SubAssign, |
|
9 |
RangeInclusive |
|
10 |
}; |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
11 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
12 |
#[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
|
13 |
pub struct Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
14 |
pub x: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
15 |
pub y: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
16 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
17 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
18 |
impl Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
19 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
20 |
pub fn new(x: i32, y: i32) -> Self { |
13941 | 21 |
Self { x, y } |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
22 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
23 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
24 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
25 |
pub fn zero() -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
26 |
Self::new(0, 0) |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
27 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
28 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
29 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
30 |
pub fn signum(self) -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
31 |
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
|
32 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
33 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
34 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
35 |
pub fn abs(self) -> Self { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
36 |
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
|
37 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
38 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
39 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
40 |
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
|
41 |
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
|
42 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
43 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
44 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
45 |
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
|
46 |
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
|
47 |
} |
14031 | 48 |
|
49 |
#[inline] |
|
14081 | 50 |
pub fn integral_norm(self) -> u32 { |
51 |
distance(self.x, self.y).abs_round() |
|
52 |
} |
|
53 |
||
54 |
#[inline] |
|
14031 | 55 |
pub fn transform(self, matrix: &[i32; 4]) -> Self { |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
56 |
Point::new( |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
57 |
matrix[0] * self.x + matrix[1] * self.y, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
58 |
matrix[2] * self.x + matrix[3] * self.y, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
59 |
) |
14031 | 60 |
} |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
61 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
62 |
|
14032 | 63 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
64 |
pub struct Size { |
|
65 |
pub width: usize, |
|
66 |
pub height: usize, |
|
67 |
} |
|
68 |
||
69 |
impl Size { |
|
70 |
#[inline] |
|
71 |
pub fn new(width: usize, height: usize) -> Self { |
|
72 |
Size { width, height } |
|
73 |
} |
|
74 |
||
75 |
#[inline] |
|
76 |
pub fn square(size: usize) -> Self { |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
77 |
Size { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
78 |
width: size, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
79 |
height: size, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
80 |
} |
14032 | 81 |
} |
82 |
||
83 |
#[inline] |
|
84 |
pub fn area(&self) -> usize { |
|
85 |
self.width * self.height |
|
86 |
} |
|
87 |
||
88 |
#[inline] |
|
89 |
pub fn linear_index(&self, x: usize, y: usize) -> usize { |
|
90 |
y * self.width + x |
|
91 |
} |
|
92 |
||
93 |
#[inline] |
|
94 |
pub fn is_power_of_two(&self) -> bool { |
|
95 |
self.width.is_power_of_two() && self.height.is_power_of_two() |
|
96 |
} |
|
97 |
||
98 |
#[inline] |
|
14052 | 99 |
pub fn next_power_of_two(&self) -> Self { |
100 |
Self { |
|
101 |
width: self.width.next_power_of_two(), |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
102 |
height: self.height.next_power_of_two(), |
14052 | 103 |
} |
104 |
} |
|
105 |
||
106 |
#[inline] |
|
14032 | 107 |
pub fn to_mask(&self) -> SizeMask { |
108 |
SizeMask::new(*self) |
|
109 |
} |
|
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
110 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
111 |
pub fn to_grid_index(&self) -> GridIndex { |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
112 |
GridIndex::new(*self) |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
113 |
} |
14032 | 114 |
} |
115 |
||
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
116 |
pub struct SizeMask { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
117 |
size: Size, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
118 |
} |
14032 | 119 |
|
120 |
impl SizeMask { |
|
121 |
#[inline] |
|
122 |
pub fn new(size: Size) -> Self { |
|
123 |
assert!(size.is_power_of_two()); |
|
124 |
let size = Size { |
|
125 |
width: !(size.width - 1), |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
126 |
height: !(size.height - 1), |
14032 | 127 |
}; |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
128 |
Self { size } |
14032 | 129 |
} |
130 |
||
131 |
#[inline] |
|
132 |
pub fn contains_x<T: Into<usize>>(&self, x: T) -> bool { |
|
133 |
(self.size.width & x.into()) == 0 |
|
134 |
} |
|
135 |
||
136 |
#[inline] |
|
137 |
pub fn contains_y<T: Into<usize>>(&self, y: T) -> bool { |
|
138 |
(self.size.height & y.into()) == 0 |
|
139 |
} |
|
140 |
||
141 |
#[inline] |
|
142 |
pub fn contains(&self, point: Point) -> bool { |
|
143 |
self.contains_x(point.x as usize) && self.contains_y(point.y as usize) |
|
144 |
} |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
145 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
146 |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
147 |
pub struct GridIndex { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
148 |
shift: Point, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
149 |
} |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
150 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
151 |
impl GridIndex { |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
152 |
pub fn new(size: Size) -> Self { |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
153 |
assert!(size.is_power_of_two()); |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
154 |
let shift = Point::new( |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
155 |
size.width.trailing_zeros() as i32, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
156 |
size.height.trailing_zeros() as i32, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
157 |
); |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
158 |
Self { shift } |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
159 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
160 |
|
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
161 |
pub fn map(&self, position: Point) -> Point { |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
162 |
Point::new(position.x >> self.shift.x, position.y >> self.shift.y) |
14059
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
163 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
164 |
} |
c6745a1c827a
start a physics engine to try out this data oriented thing everyone seems to be talking about
alfadur
parents:
14054
diff
changeset
|
165 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
166 |
macro_rules! bin_op_impl { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
167 |
($op: ty, $name: tt) => { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
168 |
impl $op for Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
169 |
type Output = Self; |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
170 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
171 |
#[inline] |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
172 |
fn $name(self, rhs: Self) -> Self::Output { |
13941 | 173 |
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
|
174 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
175 |
} |
13941 | 176 |
}; |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
177 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
178 |
|
14088
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
179 |
macro_rules! scalar_bin_op_impl { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
180 |
($($op: tt)::+, $name: tt) => { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
181 |
impl $($op)::+<i32> for Point { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
182 |
type Output = Self; |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
183 |
|
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
184 |
#[inline] |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
185 |
fn $name(self, rhs: i32) -> Self::Output { |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
186 |
Self::new(self.x.$name(rhs), self.y.$name(rhs)) |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
187 |
} |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
188 |
} |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
189 |
}; |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
190 |
} |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
191 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
192 |
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
|
193 |
($op: ty, $name: tt) => { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
194 |
impl $op for Point { |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
195 |
#[inline] |
13941 | 196 |
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
|
197 |
self.x.$name(rhs.x); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
198 |
self.y.$name(rhs.y); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
199 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
200 |
} |
13941 | 201 |
}; |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
202 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
203 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
204 |
bin_op_impl!(Add, add); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
205 |
bin_op_impl!(Sub, sub); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
206 |
bin_op_impl!(Mul, mul); |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
207 |
bin_op_impl!(Div, div); |
14088
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
208 |
scalar_bin_op_impl!(Mul, mul); |
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
209 |
scalar_bin_op_impl!(Div, div); |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
bin_assign_op_impl!(DivAssign, div_assign); |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
214 |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
215 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
216 |
pub struct Rect { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
217 |
pub x: i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
218 |
pub y: i32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
219 |
pub width: u32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
220 |
pub height: u32, |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
221 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
222 |
|
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
223 |
impl Rect { |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
224 |
#[inline] |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
225 |
pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self { |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
226 |
Self { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
227 |
x, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
228 |
y, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
229 |
width, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
230 |
height, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
231 |
} |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
232 |
} |
14054
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
233 |
|
14078 | 234 |
pub fn from_box(left: i32, right: i32, top: i32, bottom: i32) -> Self { |
235 |
assert!(left <= right); |
|
236 |
assert!(top <= bottom); |
|
237 |
||
238 |
Rect::new(left, top, (right - left) as u32, (bottom - top) as u32) |
|
239 |
} |
|
240 |
||
241 |
pub fn from_size(top_left: Point, size: Size) -> Self { |
|
14081 | 242 |
Rect::new( |
243 |
top_left.x, |
|
244 |
top_left.y, |
|
245 |
size.width as u32, |
|
246 |
size.height as u32, |
|
247 |
) |
|
14078 | 248 |
} |
249 |
||
14054
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
250 |
#[inline] |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
251 |
pub fn size(&self) -> Size { |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
252 |
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
|
253 |
} |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
254 |
|
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
255 |
#[inline] |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
256 |
pub fn area(&self) -> usize { |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
257 |
self.size().area() |
3185fb34f3b5
update theme editor to use new land generator implementation
alfadur
parents:
14052
diff
changeset
|
258 |
} |
14078 | 259 |
|
260 |
#[inline] |
|
261 |
pub fn left(&self) -> i32 { |
|
262 |
self.x |
|
263 |
} |
|
264 |
||
265 |
#[inline] |
|
266 |
pub fn top(&self) -> i32 { |
|
267 |
self.y |
|
268 |
} |
|
269 |
||
270 |
#[inline] |
|
271 |
pub fn right(&self) -> i32 { |
|
272 |
self.x + self.width as i32 |
|
273 |
} |
|
274 |
||
275 |
#[inline] |
|
276 |
pub fn bottom(&self) -> i32 { |
|
277 |
self.y + self.height as i32 |
|
278 |
} |
|
279 |
||
280 |
#[inline] |
|
281 |
pub fn top_left(&self) -> Point { |
|
282 |
Point::new(self.x, self.y) |
|
283 |
} |
|
284 |
||
285 |
#[inline] |
|
286 |
pub fn with_margin(&self, margin: i32) -> Self { |
|
287 |
Rect::from_box( |
|
288 |
self.left() + margin, |
|
289 |
self.right() - margin, |
|
290 |
self.top() + margin, |
|
291 |
self.bottom() - margin, |
|
292 |
) |
|
293 |
} |
|
14081 | 294 |
|
295 |
#[inline] |
|
14092 | 296 |
pub fn x_range(&self) -> RangeInclusive<i32> { |
297 |
self.x..=self.x + self.width as i32 |
|
298 |
} |
|
299 |
||
300 |
#[inline] |
|
301 |
pub fn y_range(&self) -> RangeInclusive<i32> { |
|
302 |
self.y..=self.y + self.height as i32 |
|
303 |
} |
|
304 |
||
305 |
/* requires #[feature(range_contains)] |
|
306 |
#[inline] |
|
307 |
pub fn contains(&self, point: Point) -> bool { |
|
308 |
x_range().contains(point.x) && y_range.contains(point.y) |
|
309 |
}*/ |
|
310 |
||
311 |
#[inline] |
|
14081 | 312 |
pub fn contains_inside(&self, point: Point) -> bool { |
313 |
point.x > self.left() |
|
314 |
&& point.x < self.right() |
|
315 |
&& point.y > self.top() |
|
316 |
&& point.y < self.bottom() |
|
317 |
} |
|
14092 | 318 |
|
319 |
#[inline] |
|
320 |
pub fn intersects(&self, other: &Rect) -> bool { |
|
321 |
self.left() <= self.right() |
|
322 |
&& self.right() >= other.left() |
|
323 |
&& self.top() <= other.bottom() |
|
324 |
&& self.bottom() >= other.top() |
|
325 |
} |
|
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
326 |
} |
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
327 |
|
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
328 |
#[derive(PartialEq, Eq, Clone, Copy, Debug)] |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
329 |
pub struct Line { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
330 |
pub start: Point, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
331 |
pub end: Point, |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
332 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
333 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
334 |
impl Line { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
335 |
#[inline] |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
336 |
pub fn new(start: Point, end: Point) -> Self { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
337 |
Self { start, end } |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
338 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
339 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
340 |
#[inline] |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
341 |
pub fn zero() -> Self { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
342 |
Self::new(Point::zero(), Point::zero()) |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
343 |
} |
14077 | 344 |
|
345 |
#[inline] |
|
346 |
pub fn center(&self) -> Point { |
|
14088
f483f844da98
component-wise division is actually useful sometimes
alfadur
parents:
14081
diff
changeset
|
347 |
(self.start + self.end) / 2 |
14077 | 348 |
} |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
349 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
350 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
351 |
impl IntoIterator for Line { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
352 |
type Item = Point; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
353 |
type IntoIter = LinePoints; |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
354 |
|
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
355 |
fn into_iter(self) -> Self::IntoIter { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
356 |
LinePoints::new(self) |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
357 |
} |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
358 |
} |
14051
8a0d69c16cad
Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents:
13950
diff
changeset
|
359 |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
360 |
pub struct LinePoints { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
361 |
accumulator: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
362 |
direction: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
363 |
sign: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
364 |
current: Point, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
365 |
total_steps: i32, |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
366 |
step: i32, |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
367 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
368 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
369 |
impl LinePoints { |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
370 |
pub fn new(line: Line) -> Self { |
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
371 |
let dir = line.end - line.start; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
372 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
373 |
Self { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
374 |
accumulator: Point::zero(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
375 |
direction: dir.abs(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
376 |
sign: dir.signum(), |
14076
e5904ead4864
Introduce OutlineSegmentsIterator, some refactoring
unC0Rr
parents:
14059
diff
changeset
|
377 |
current: line.start, |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
378 |
total_steps: dir.max_norm(), |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
379 |
step: 0, |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
380 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
381 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
382 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
383 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
384 |
impl Iterator for LinePoints { |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
385 |
type Item = Point; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
386 |
|
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
387 |
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
|
388 |
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
|
389 |
self.accumulator += self.direction; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
390 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
391 |
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
|
392 |
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
|
393 |
self.current.x += self.sign.x; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
394 |
} |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
395 |
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
|
396 |
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
|
397 |
self.current.y += self.sign.y; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
398 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
399 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
400 |
self.step += 1; |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
401 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
402 |
Some(self.current) |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
403 |
} else { |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
404 |
None |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
405 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
406 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
407 |
} |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
408 |
|
13941 | 409 |
pub struct ArcPoints { |
410 |
point: Point, |
|
411 |
step: i32, |
|
412 |
} |
|
413 |
||
414 |
impl ArcPoints { |
|
415 |
pub fn new(radius: i32) -> Self { |
|
416 |
Self { |
|
417 |
point: Point::new(0, radius), |
|
418 |
step: 3 - 2 * radius, |
|
419 |
} |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
impl Iterator for ArcPoints { |
|
424 |
type Item = Point; |
|
425 |
||
426 |
fn next(&mut self) -> Option<Self::Item> { |
|
427 |
if self.point.x < self.point.y { |
|
428 |
let result = self.point; |
|
429 |
||
430 |
if self.step < 0 { |
|
431 |
self.step += self.point.x * 4 + 6; |
|
432 |
} else { |
|
433 |
self.step += (self.point.x - self.point.y) * 4 + 10; |
|
434 |
self.point.y -= 1; |
|
435 |
} |
|
436 |
||
437 |
self.point.x += 1; |
|
438 |
||
439 |
Some(result) |
|
440 |
} else if self.point.x == self.point.y { |
|
13947 | 441 |
self.point.x += 1; |
13950 | 442 |
|
13941 | 443 |
Some(self.point) |
444 |
} else { |
|
445 |
None |
|
446 |
} |
|
447 |
} |
|
448 |
} |
|
449 |
||
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
450 |
pub struct EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
451 |
vector: Point, |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
452 |
iteration: u8, |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
453 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
454 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
455 |
impl EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
456 |
pub fn new(vector: Point) -> Self { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
457 |
Self { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
458 |
vector, |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
459 |
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
|
460 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
461 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
462 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
463 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
464 |
impl Iterator for EquidistantPoints { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
465 |
type Item = Point; |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
466 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
467 |
fn next(&mut self) -> Option<Self::Item> { |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
468 |
if self.iteration > 0 { |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
469 |
self.vector.x = -self.vector.x; |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
470 |
if self.iteration & 1 == 0 { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
471 |
self.vector.y = -self.vector.y; |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
472 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
473 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
474 |
if self.iteration == 4 { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
475 |
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
|
476 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
477 |
|
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
478 |
self.iteration -= 1; |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
479 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
480 |
Some(self.vector) |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
481 |
} else { |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
482 |
None |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
483 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
484 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
485 |
} |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
486 |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
487 |
#[cfg(test)] |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
488 |
mod tests { |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
489 |
use super::*; |
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
490 |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
491 |
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
|
492 |
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
|
493 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
494 |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
495 |
#[test] |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
496 |
fn line_basic() { |
14077 | 497 |
let line: Vec<Point> = Line::new(Point::new(0, 0), Point::new(3, 3)) |
498 |
.into_iter() |
|
499 |
.collect(); |
|
500 |
let v = get_points(&[(0, 0), (1, 1), (2, 2), (3, 3)]); |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
501 |
|
14077 | 502 |
assert_eq!(line, v); |
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
503 |
} |
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
504 |
|
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
505 |
#[test] |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
506 |
fn line_skewed() { |
14077 | 507 |
let line: Vec<Point> = Line::new(Point::new(0, 0), Point::new(5, -7)) |
508 |
.into_iter() |
|
509 |
.collect(); |
|
13941 | 510 |
let v = get_points(&[ |
511 |
(0, 0), |
|
512 |
(1, -1), |
|
513 |
(2, -2), |
|
514 |
(2, -3), |
|
515 |
(3, -4), |
|
516 |
(4, -5), |
|
517 |
(4, -6), |
|
518 |
(5, -7), |
|
519 |
]); |
|
13938
1fa905aa4cdb
move point struct into integral-geometry and use it to refactor a bit
alfadur
parents:
13937
diff
changeset
|
520 |
|
14077 | 521 |
assert_eq!(line, v); |
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
522 |
} |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
523 |
|
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
524 |
#[test] |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
525 |
fn equidistant_full() { |
14077 | 526 |
let n: Vec<Point> = EquidistantPoints::new(Point::new(1, 3)).collect(); |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
527 |
let v = get_points(&[ |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
528 |
(-1, -3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
529 |
(1, -3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
530 |
(-1, 3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
531 |
(1, 3), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
532 |
(-3, -1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
533 |
(3, -1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
534 |
(-3, 1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
535 |
(3, 1), |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
536 |
]); |
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
537 |
|
14077 | 538 |
assert_eq!(n, v); |
13942
7e7a03e85ac4
Add EquidistantPoints iterator to help iterating over all points of circles
unc0rr
parents:
13941
diff
changeset
|
539 |
} |
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
540 |
|
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
541 |
#[test] |
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
542 |
fn equidistant_half() { |
14077 | 543 |
let n: Vec<Point> = EquidistantPoints::new(Point::new(2, 2)).collect(); |
544 |
let v = get_points(&[(-2, -2), (2, -2), (-2, 2), (2, 2)]); |
|
545 |
||
546 |
assert_eq!(n, v); |
|
547 |
} |
|
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
548 |
|
14077 | 549 |
#[test] |
550 |
fn line() { |
|
551 |
let l = Line::new(Point::new(1, 1), Point::new(5, 6)); |
|
552 |
||
553 |
assert_eq!(l.center(), Point::new(3, 3)); |
|
13943
a325ed57ebfe
Don't generate unnecessary duplication in case of equal coordinates
unc0rr
parents:
13942
diff
changeset
|
554 |
} |
14078 | 555 |
|
556 |
#[test] |
|
557 |
fn rect() { |
|
558 |
let r = Rect::from_box(10, 100, 0, 70); |
|
559 |
||
14081 | 560 |
assert!(r.contains_inside(Point::new(99, 69))); |
561 |
assert!(!r.contains_inside(Point::new(100, 70))); |
|
562 |
||
14078 | 563 |
assert_eq!(r.top_left(), Point::new(10, 0)); |
564 |
assert_eq!(r.with_margin(12), Rect::from_box(22, 88, 12, 58)); |
|
565 |
} |
|
13935
75eaf7c71789
Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents:
diff
changeset
|
566 |
} |