Skip to content

Commit cd2ac9c

Browse files
committed
Change boxsize into rectangular bounds
1 parent 527a931 commit cd2ac9c

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

benches/genvoronoi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod tests {
3131
let points = vec![Point::new(0.0, 1.0)];
3232

3333
b.iter(|| {
34-
voronoi(points.clone(), BOX_SIZE);
34+
voronoi(points.clone(), (BOX_SIZE, BOX_SIZE));
3535
});
3636
}
3737

@@ -40,7 +40,7 @@ mod tests {
4040
let points = generate_points(100);
4141

4242
b.iter(|| {
43-
voronoi(points.clone(), BOX_SIZE);
43+
voronoi(points.clone(), (BOX_SIZE, BOX_SIZE));
4444
});
4545
}
4646

@@ -50,7 +50,7 @@ mod tests {
5050
let points = generate_points(10000);
5151

5252
b.iter(|| {
53-
voronoi(points.clone(), BOX_SIZE);
53+
voronoi(points.clone(), (BOX_SIZE, BOX_SIZE));
5454
});
5555
}
5656
}

src/lloyd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub fn polygon_centroid(pts: &Vec<Point>) -> Point {
1414
/// Produces the Lloyd Relaxation of a set of points.
1515
///
1616
/// Each point is moved to the centroid of its Voronoi cell.
17-
pub fn lloyd_relaxation(pts: Vec<Point>, box_size: f64) -> Vec<Point> {
18-
let voronoi = voronoi(pts, box_size);
17+
pub fn lloyd_relaxation(pts: Vec<Point>, bounds: (f64, f64)) -> Vec<Point> {
18+
let voronoi = voronoi(pts, bounds);
1919
let faces = make_polygons(&voronoi);
2020
faces.iter().map(polygon_centroid).collect::<Vec<Point>>()
2121
}

src/voronoi.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type TripleSite = (Point, Point, Point);
88

99
/// Computes the Voronoi diagram of a set of points.
1010
/// Returns a Doubly Connected Edge List.
11-
pub fn voronoi(points: Vec<Point>, boxsize: f64) -> DCEL {
11+
pub fn voronoi(points: Vec<Point>, bounds: (f64, f64)) -> DCEL {
1212
trace!("Starting Voronoi Computation");
1313
let mut event_queue = EventQueue::new();
1414
let mut beachline = BeachLine::new();
@@ -32,7 +32,7 @@ pub fn voronoi(points: Vec<Point>, boxsize: f64) -> DCEL {
3232
}
3333
}
3434
}
35-
add_bounding_box(boxsize, &beachline, &mut result);
35+
add_bounding_box(bounds, &beachline, &mut result);
3636
add_faces(&mut result);
3737
return result;
3838
}
@@ -259,19 +259,22 @@ fn handle_circle_event(
259259
}
260260
}
261261

262-
fn outside_bb(pt: Point, box_size: f64) -> bool {
262+
fn outside_bb(pt: Point, bounds: (f64, f64)) -> bool {
263263
let delta = 0.1;
264-
pt.x() < 0. - delta || pt.x() > box_size + delta || pt.y() < 0. - delta || pt.y() > box_size + delta
264+
let (max_x, max_y) = bounds;
265+
pt.x() < 0. - delta || pt.x() > max_x + delta || pt.y() < 0. - delta || pt.y() > max_y + delta
265266
}
266267

267-
fn add_bounding_box(boxsize: f64, beachline: &BeachLine, dcel: &mut DCEL) {
268+
fn add_bounding_box(bounds: (f64, f64), beachline: &BeachLine, dcel: &mut DCEL) {
268269
extend_edges(beachline, dcel);
269270

271+
let (max_x, max_y) = bounds;
272+
270273
let delta = 50.;
271-
let bb_top = [Point::new(0. - delta, 0.), Point::new(boxsize + delta, 0.)];
272-
let bb_bottom = [Point::new(0. - delta, boxsize), Point::new(boxsize + delta, boxsize)];
273-
let bb_left = [Point::new(0., 0. - delta), Point::new(0., boxsize + delta)];
274-
let bb_right = [Point::new(boxsize, 0. - delta), Point::new(boxsize, boxsize + delta)];
274+
let bb_top = [Point::new(0. - delta, 0.), Point::new(max_x + delta, 0.)];
275+
let bb_bottom = [Point::new(0. - delta, max_y), Point::new(max_x + delta, max_y)];
276+
let bb_left = [Point::new(0., 0. - delta), Point::new(0., max_y + delta)];
277+
let bb_right = [Point::new(max_x, 0. - delta), Point::new(max_x, max_y + delta)];
275278

276279
add_line(bb_top, dcel);
277280
add_line(bb_right, dcel);
@@ -282,7 +285,7 @@ fn add_bounding_box(boxsize: f64, beachline: &BeachLine, dcel: &mut DCEL) {
282285

283286
for vert in 0..dcel.vertices.len() {
284287
let this_pt = dcel.vertices[vert].coordinates;
285-
if outside_bb(this_pt, boxsize) {
288+
if outside_bb(this_pt, bounds) {
286289
dcel.remove_vertex(vert);
287290
}
288291
}

0 commit comments

Comments
 (0)