From 184d74a5aaa1c0c8ca925a19b432ced53d6de856 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Wed, 17 Nov 2021 13:10:50 -0500 Subject: [PATCH] Add "randomly" generated initial nodes --- src/main.rs | 28 +++++++++++++++++++++++----- src/util.rs | 2 +- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index f3c9b4f..6a3ae49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,11 @@ async fn main() { let mut camera = dd.get_camera(); set_camera(&camera); let mut iter = 0; + + for _ in 0..160 { + dd.add_random_point().unwrap(); + } + loop { if iter == 100 { iter = 0; @@ -174,13 +179,15 @@ impl Default for DelaunayDemo { (edges.3, (Border, (&triangles[1]).into())), (edges.4, (Border, (&triangles[1]).into())), ].into(); + let mut random = 0xACAB_1312; + util::pcg64_iterstate(&mut random); let dd = DelaunayDemo { nodes, adjacency, triangles, height: HEIGHT, width: WIDTH, - random_state: 1312_1312_1312, + random_state: random, poisoned: false, spanning_tree: HashSet::with_capacity(100), spanning_tree_conn: HashMap::with_capacity(100), @@ -602,16 +609,27 @@ impl DelaunayDemo { } fn click(&mut self, pos: Vec2) { + if let Err(msg) = self.add_point_random_dir(pos.as_f64()) { + self.poisoned = true; + eprintln!("POISONED: {}", msg); + } + } + + fn add_random_point(&mut self) -> Result<(), String> { + let random = util::pcg64(&mut self.random_state); + let x = (random & 0xffffffff) as f64 / (0xffffffffu32 as f64) * self.width; + let y = (random >> 32) as f64 / (0xffffffffu32 as f64) * self.height; + self.add_point_random_dir((x, y).into()) + } + + fn add_point_random_dir(&mut self, pos: DVec2) -> Result<(), String> { let random = util::pcg64(&mut self.random_state); let angle = (random as f64) / (u64::MAX as f64) * (2.0 * std::f64::consts::PI); let direction = DVec2::new( f64::cos(angle), f64::sin(angle), ); - if let Err(msg) = self.add_point(Node(pos.as_f64(), direction * SPEED)) { - self.poisoned = true; - eprintln!("POISONED: {}", msg); - } + self.add_point(Node(pos, direction * SPEED)) } fn add_point(&mut self, node: Node) -> Result<(), String> { diff --git a/src/util.rs b/src/util.rs index 2330bec..22eef59 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,7 +6,7 @@ pub fn pcg64(state: &mut u128) -> u64 { ((x >> 58) as u64).rotate_right(count) } -fn pcg64_iterstate(state: &mut u128) { +pub fn pcg64_iterstate(state: &mut u128) { const MULTIPLIER: u128 = 0xde92a69f6e2f9f25fd0d90f576075fbd; const INCREMENT: u128 = 621; *state = state.wrapping_mul(MULTIPLIER).wrapping_add(INCREMENT);