Skip to content

Commit 482c98d

Browse files
committed
Extract PointGenerator trait.
1 parent 2dfaf91 commit 482c98d

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/bin/cli.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ fn main() {
4848
.expect("Can't open source file");
4949
let sourcebuf = source.to_rgba();
5050

51-
let mut filter =
52-
Geometrify::new(
53-
RandomPointGenerator::new(sourcebuf.width() as i32, sourcebuf.height() as i32),
54-
);
51+
52+
let pointgen = Box::new(RandomPointGenerator::new());
53+
let mut filter = Geometrify::new(pointgen);
5554

5655

5756
let outputbuf = filter.apply(

src/lib.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,35 +125,37 @@ impl Primitive for Triangle {
125125
}
126126
}
127127

128+
trait PointGenerator {
129+
fn next_point(&mut self, width: u32, height: u32) -> Point;
130+
}
131+
128132
pub struct RandomPointGenerator {
129133
rng: Box<Rng>,
130-
width: i32,
131-
height: i32,
132134
}
133135

134136
impl RandomPointGenerator {
135-
pub fn new(width: i32, height: i32) -> RandomPointGenerator {
137+
pub fn new() -> RandomPointGenerator {
136138
RandomPointGenerator {
137139
rng: Box::new(rand::thread_rng()),
138-
width: width,
139-
height: height,
140140
}
141141
}
142+
}
142143

143-
fn next_point(&mut self) -> Point {
144+
impl PointGenerator for RandomPointGenerator {
145+
fn next_point(&mut self, width: u32, height: u32) -> Point {
144146
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),
147149
}
148150
}
149151
}
150152

151153
pub struct Geometrify {
152-
point_gen: RandomPointGenerator,
154+
point_gen: Box<RandomPointGenerator>,
153155
}
154156

155157
impl Geometrify {
156-
pub fn new(point_gen: RandomPointGenerator) -> Geometrify {
158+
pub fn new(point_gen: Box<RandomPointGenerator>) -> Geometrify {
157159
Geometrify { point_gen: point_gen }
158160
}
159161

@@ -189,11 +191,11 @@ impl Geometrify {
189191
}
190192
}
191193

192-
fn generate_primitive(&mut self) -> Triangle {
194+
fn generate_primitive(&mut self, width: u32, height: u32) -> Triangle {
193195
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),
197199
)
198200
}
199201

@@ -335,7 +337,7 @@ impl Geometrify {
335337
let difference_lut = Geometrify::calculate_difference_lut(&image, &destination);
336338

337339
let primitives = (0..number_of_samples)
338-
.map(|_| self.generate_primitive())
340+
.map(|_| self.generate_primitive(image.width(), image.height()))
339341
.map(
340342
|mut p| {
341343
p.span_div_save();

0 commit comments

Comments
 (0)