mesh_sweeper/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use voronator::{VoronoiDiagram, delaunator::Point};
use rand::{prelude::*, distributions::Uniform};
use plotpy::{Curve, Plot};
use std::time::Instant;

fn main() {

    let mut rng = thread_rng();
    let range1 = Uniform::new(0.0, 100.0);
    let range2 = Uniform::new(0.0, 100.0);
    let points: Vec<(f64, f64)> = (0..10000)
        .map(|_| (rng.sample(&range1), rng.sample(&range2)))
        .collect();

    let now = Instant::now();

    let diagram = VoronoiDiagram::<Point>::from_tuple(&(0.0, 0.0), &(100.0, 100.0), &points).unwrap();

    let elapsed = now.elapsed();

    println!("{:.2?} elapsed time", elapsed);

    let mut plot = Plot::new();

    for cell in diagram.cells() {
        let mut p_x: Vec<f32> = cell.points().into_iter().map(|x| x.x as f32).collect();
        let mut p_y: Vec<f32> = cell.points().into_iter().map(|x| x.y as f32).collect();

        p_x.push(p_x[0]);
        p_y.push(p_y[0]);

        let mut curve = Curve::new();
        curve.set_line_width(0.5);
        curve.draw(&p_x, &p_y);
        plot.add(&curve);
    }

    plot.save("plot.svg").unwrap();
}