Replace voronoi crate with voronator

This commit is contained in:
Héctor Ramón Jiménez 2022-10-04 11:50:32 +02:00
parent ddb8f5b96c
commit ce2c795bda
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 18 additions and 24 deletions

View file

@ -9,4 +9,4 @@ publish = false
iced = { path = "../..", features = ["canvas", "tokio", "debug"] } iced = { path = "../..", features = ["canvas", "tokio", "debug"] }
tokio = { version = "1.0", features = ["sync"] } tokio = { version = "1.0", features = ["sync"] }
env_logger = "0.9" env_logger = "0.9"
voronoi = "0.1.4" voronator = "0.2"

View file

@ -144,21 +144,29 @@ impl<'a> canvas::Program<Message> for State {
zones.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); zones.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
// Generate sorted list of points // Generate sorted list of points
let vpoints: Vec<voronoi::Point> = let vpoints: Vec<(f64, f64)> = zones
zones.iter().map(|zone| to_voronoi_point(&zone.1)).collect(); .iter()
.map(|(_, p)| (f64::from(p.x), f64::from(p.y)))
.collect();
let diagram = voronoi::voronoi(vpoints, 700.0); let diagram: voronator::VoronoiDiagram<
let polys = voronoi::make_polygons(&diagram); voronator::delaunator::Point,
> = voronator::VoronoiDiagram::from_tuple(
&(0.0, 0.0),
&(700.0, 700.0),
&vpoints,
)
.expect("Generate Voronoi diagram");
for (poly, zone) in polys.iter().zip(zones) { for (cell, zone) in diagram.cells().iter().zip(zones) {
let mut builder = canvas::path::Builder::new(); let mut builder = canvas::path::Builder::new();
for (index, pt) in poly.iter().enumerate() { for (index, p) in cell.points().iter().enumerate() {
let pt = from_voronoi_point(pt); let p = Point::new(p.x as f32, p.y as f32);
match index { match index {
0 => builder.move_to(pt), 0 => builder.move_to(p),
_ => builder.line_to(pt), _ => builder.line_to(p),
} }
} }
@ -192,17 +200,3 @@ impl<'a> canvas::Program<Message> for State {
vec![fingerweb] vec![fingerweb]
} }
} }
fn to_voronoi_point(pt: &iced::Point) -> voronoi::Point {
voronoi::Point::new(pt.x.into(), pt.y.into())
}
fn from_voronoi_point(pt: &voronoi::Point) -> iced::Point {
let x: f64 = pt.x.into();
let y: f64 = pt.y.into();
Point {
x: x as f32,
y: y as f32,
}
}