Voronoi diagram
This commit is contained in:
parent
ef3253d777
commit
acd4a62d08
3 changed files with 44 additions and 12 deletions
34
src/main.rs
34
src/main.rs
|
|
@ -1,6 +1,7 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy_vello::{VelloPlugin, prelude::*, vello::kurbo::Point};
|
||||
use bevy_vello::{prelude::*, vello::kurbo::{Point, Stroke}, VelloPlugin};
|
||||
use rand::Rng;
|
||||
use spade::{DelaunayTriangulation, Point2, Triangulation, handles::VoronoiVertex};
|
||||
use std::ops::DerefMut;
|
||||
|
||||
const JITTER: f64 = 0.5;
|
||||
|
|
@ -21,25 +22,34 @@ fn setup_vector_graphics(mut commands: Commands) {
|
|||
|
||||
fn draw_map(mut query_scene: Single<(&mut Transform, &mut VelloScene)>) {
|
||||
let (transform, scene) = query_scene.deref_mut();
|
||||
// Reset scene every frame
|
||||
scene.reset();
|
||||
transform.scale = Vec3::ONE * 25.0;
|
||||
|
||||
let mut triangulation: DelaunayTriangulation<_> = DelaunayTriangulation::new();
|
||||
let mut rng = rand::rng();
|
||||
for x in -20..=20 {
|
||||
for y in -10..=10 {
|
||||
let point = Point::new(
|
||||
triangulation.insert(Point2::new(
|
||||
(x as f64) + rng.random_range(-JITTER..=JITTER),
|
||||
(y as f64) + rng.random_range(-JITTER..=JITTER),
|
||||
);
|
||||
scene.fill(
|
||||
peniko::Fill::NonZero,
|
||||
kurbo::Affine::default(),
|
||||
peniko::Color::WHITE,
|
||||
None,
|
||||
&kurbo::Circle::new(point, 0.1),
|
||||
);
|
||||
)).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
transform.scale = Vec3::ONE * 25.0;
|
||||
for edge in triangulation.undirected_voronoi_edges() {
|
||||
match edge.vertices() {
|
||||
[VoronoiVertex::Inner(from), VoronoiVertex::Inner(to)] => {
|
||||
let from = from.circumcenter();
|
||||
let to = to.circumcenter();
|
||||
scene.stroke(
|
||||
&Stroke::new(0.1),
|
||||
kurbo::Affine::default(),
|
||||
peniko::Color::WHITE,
|
||||
None,
|
||||
&kurbo::Line::new(Point::new(from.x, from.y), Point::new(to.x, to.y)),
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue