@@ -125,35 +125,37 @@ impl Primitive for Triangle {
125
125
}
126
126
}
127
127
128
+ trait PointGenerator {
129
+ fn next_point ( & mut self , width : u32 , height : u32 ) -> Point ;
130
+ }
131
+
128
132
pub struct RandomPointGenerator {
129
133
rng : Box < Rng > ,
130
- width : i32 ,
131
- height : i32 ,
132
134
}
133
135
134
136
impl RandomPointGenerator {
135
- pub fn new ( width : i32 , height : i32 ) -> RandomPointGenerator {
137
+ pub fn new ( ) -> RandomPointGenerator {
136
138
RandomPointGenerator {
137
139
rng : Box :: new ( rand:: thread_rng ( ) ) ,
138
- width : width,
139
- height : height,
140
140
}
141
141
}
142
+ }
142
143
143
- fn next_point ( & mut self ) -> Point {
144
+ impl PointGenerator for RandomPointGenerator {
145
+ fn next_point ( & mut self , width : u32 , height : u32 ) -> Point {
144
146
Point {
145
- x : self . rng . gen_range ( 0 , self . width ) ,
146
- y : self . rng . gen_range ( 0 , self . height ) ,
147
+ x : self . rng . gen_range ( 0 , width as i32 ) ,
148
+ y : self . rng . gen_range ( 0 , height as i32 ) ,
147
149
}
148
150
}
149
151
}
150
152
151
153
pub struct Geometrify {
152
- point_gen : RandomPointGenerator ,
154
+ point_gen : Box < RandomPointGenerator > ,
153
155
}
154
156
155
157
impl Geometrify {
156
- pub fn new ( point_gen : RandomPointGenerator ) -> Geometrify {
158
+ pub fn new ( point_gen : Box < RandomPointGenerator > ) -> Geometrify {
157
159
Geometrify { point_gen : point_gen }
158
160
}
159
161
@@ -189,11 +191,11 @@ impl Geometrify {
189
191
}
190
192
}
191
193
192
- fn generate_primitive ( & mut self ) -> Triangle {
194
+ fn generate_primitive ( & mut self , width : u32 , height : u32 ) -> Triangle {
193
195
Triangle :: new (
194
- self . point_gen . next_point ( ) ,
195
- self . point_gen . next_point ( ) ,
196
- self . point_gen . next_point ( ) ,
196
+ self . point_gen . next_point ( width , height ) ,
197
+ self . point_gen . next_point ( width , height ) ,
198
+ self . point_gen . next_point ( width , height ) ,
197
199
)
198
200
}
199
201
@@ -335,7 +337,7 @@ impl Geometrify {
335
337
let difference_lut = Geometrify :: calculate_difference_lut ( & image, & destination) ;
336
338
337
339
let primitives = ( 0 ..number_of_samples)
338
- . map ( |_| self . generate_primitive ( ) )
340
+ . map ( |_| self . generate_primitive ( image . width ( ) , image . height ( ) ) )
339
341
. map (
340
342
|mut p| {
341
343
p. span_div_save ( ) ;
0 commit comments