Colorful voronoi cells
This commit is contained in:
parent
acd4a62d08
commit
ffbdb33659
3 changed files with 51 additions and 41 deletions
50
src/main.rs
50
src/main.rs
|
|
@ -1,7 +1,11 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy_vello::{prelude::*, vello::kurbo::{Point, Stroke}, VelloPlugin};
|
||||
use bevy_vello::{
|
||||
VelloPlugin,
|
||||
prelude::*,
|
||||
vello::kurbo::{PathEl, Point},
|
||||
};
|
||||
use rand::Rng;
|
||||
use spade::{DelaunayTriangulation, Point2, Triangulation, handles::VoronoiVertex};
|
||||
use voronator::VoronoiDiagram;
|
||||
use std::ops::DerefMut;
|
||||
|
||||
const JITTER: f64 = 0.5;
|
||||
|
|
@ -23,33 +27,41 @@ 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();
|
||||
scene.reset();
|
||||
transform.scale = Vec3::ONE * 25.0;
|
||||
transform.scale = Vec3::ONE * 30.0;
|
||||
|
||||
let mut triangulation: DelaunayTriangulation<_> = DelaunayTriangulation::new();
|
||||
let mut rng = rand::rng();
|
||||
let mut points = Vec::new();
|
||||
for x in -20..=20 {
|
||||
for y in -10..=10 {
|
||||
triangulation.insert(Point2::new(
|
||||
points.push((
|
||||
(x as f64) + rng.random_range(-JITTER..=JITTER),
|
||||
(y as f64) + rng.random_range(-JITTER..=JITTER),
|
||||
)).unwrap();
|
||||
));
|
||||
}
|
||||
}
|
||||
let diagram = VoronoiDiagram::<voronator::delaunator::Point>::from_tuple(&(-20.0, -10.0), &(20.0, 10.0), &points).unwrap();
|
||||
|
||||
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)),
|
||||
);
|
||||
for cell in diagram.cells() {
|
||||
let mut first = true;
|
||||
let mut path = Vec::new();
|
||||
for point in cell.points() {
|
||||
let point = Point::new(point.x, point.y);
|
||||
if first {
|
||||
first = false;
|
||||
path.push(PathEl::MoveTo(point));
|
||||
} else {
|
||||
path.push(PathEl::LineTo(point));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let r = rng.random_range(0..=255);
|
||||
let g = rng.random_range(0..=255);
|
||||
let b = rng.random_range(0..=255);
|
||||
scene.fill(
|
||||
peniko::Fill::NonZero,
|
||||
kurbo::Affine::default(),
|
||||
peniko::Color::from_rgb8(r, g, b),
|
||||
None,
|
||||
&kurbo::BezPath::from_vec(path),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue