Start hex grid and make tests pass
This commit is contained in:
parent
92ef0f728c
commit
9ff002b19c
@ -1,19 +1,10 @@
|
|||||||
#![feature(array_zip)]
|
#![feature(array_zip)]
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use rand::prelude::*;
|
|
||||||
|
|
||||||
mod system;
|
mod system;
|
||||||
mod example_systems;
|
mod example_systems;
|
||||||
|
|
||||||
use system::walker::{LocalRandomWalker, Walker};
|
|
||||||
use system::grid::VectorStorage;
|
|
||||||
use num_integer::Integer;
|
|
||||||
use rand::rngs::SmallRng;
|
|
||||||
use crate::system::grid::Position;
|
|
||||||
use crate::system::model::DLASystem;
|
|
||||||
use crate::system::Storage;
|
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use crate::example_systems::stick_probability;
|
use crate::example_systems::stick_probability;
|
||||||
|
|
||||||
|
|||||||
@ -73,32 +73,20 @@ impl VectorStorage {
|
|||||||
VectorStorage { grid_size, backing: vec![false; grid_size.pow(2) as usize] }
|
VectorStorage { grid_size, backing: vec![false; grid_size.pow(2) as usize] }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn linear_index(&self, position: &Position) -> usize {
|
|
||||||
assert!(position.x <= self.grid_size as i32 && -(self.grid_size as i32) <= position.x);
|
|
||||||
assert!(position.y <= self.grid_size as i32 && -(self.grid_size as i32) <= position.y);
|
|
||||||
|
|
||||||
let x = (position.x + (self.grid_size as i32) / 2) as usize;
|
|
||||||
let y = (position.y + (self.grid_size as i32) / 2) as usize;
|
|
||||||
|
|
||||||
return self.grid_size as usize * y + x
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Convenience function for c-binding
|
* Convenience function for c-binding
|
||||||
* */
|
* */
|
||||||
pub fn write(&mut self, position: &Position, val: bool) {
|
pub fn write(&mut self, position: &Position, val: bool) {
|
||||||
let index = self.linear_index(position);
|
self.backing[position.linear_index(self.grid_size)] = val;
|
||||||
self.backing[index] = val;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Storage<Position> for VectorStorage {
|
impl Storage<Position> for VectorStorage {
|
||||||
fn at(&self, position: &Position) -> bool {
|
fn at(&self, position: &Position) -> bool {
|
||||||
return self.backing[self.linear_index(position)]
|
return self.backing[position.linear_index(self.grid_size)]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deposit(&mut self, position: &Position) {
|
fn deposit(&mut self, position: &Position) {
|
||||||
let index = self.linear_index(position);
|
self.backing[position.linear_index(self.grid_size)] = true;
|
||||||
self.backing[index] = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
51
src/system/hexagonal.rs
Normal file
51
src/system/hexagonal.rs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
use std::ops::Add;
|
||||||
|
use bevy::render::color::HexColorError::Hex;
|
||||||
|
use rand::Rng;
|
||||||
|
use crate::system::GriddedPosition;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub struct HexPosition {
|
||||||
|
pub q: i32,
|
||||||
|
pub r: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add for HexPosition {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
HexPosition { q: self.q + rhs.q, r: self.r + rhs.r }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GriddedPosition for HexPosition {
|
||||||
|
const NEIGHBOURS: u32 = 6;
|
||||||
|
|
||||||
|
fn zero() -> Self {
|
||||||
|
HexPosition { q: 0, r: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn abs(&self) -> f32 {
|
||||||
|
((self.q.pow(2) + self.r.pow(2) + self.q * self.r) as f32).sqrt()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn neighbour(&self, neighbour_index: u32) -> Self {
|
||||||
|
let neighbour_index = neighbour_index as usize;
|
||||||
|
|
||||||
|
const OFFSETS: [(i32, i32); 6] = [
|
||||||
|
(1, 0), (1, -1), (0, -1),
|
||||||
|
(-1, 0), (-1, 1), (0, 1),
|
||||||
|
];
|
||||||
|
|
||||||
|
self.clone() + HexPosition { q: OFFSETS[neighbour_index].0, r: OFFSETS[neighbour_index].0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn linear_index(&self, grid_size: u32) -> usize {
|
||||||
|
todo!()
|
||||||
|
// ((self.q + grid_size / 2) + grid_size * self.r) as usize
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,14 +1,12 @@
|
|||||||
use std::f32::consts::PI;
|
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
use num_integer::Integer;
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
use serde::Serialize;
|
||||||
use crate::system::grid::VectorStorage;
|
|
||||||
|
|
||||||
pub mod walker;
|
pub mod walker;
|
||||||
pub mod grid;
|
pub mod grid;
|
||||||
pub mod model;
|
pub mod model;
|
||||||
pub mod nd;
|
pub mod nd;
|
||||||
|
mod hexagonal;
|
||||||
|
|
||||||
pub trait GriddedPosition: Add<Output=Self> + Serialize + Clone {
|
pub trait GriddedPosition: Add<Output=Self> + Serialize + Clone {
|
||||||
const NEIGHBOURS: u32;
|
const NEIGHBOURS: u32;
|
||||||
|
|||||||
@ -1,14 +1,8 @@
|
|||||||
use std::f32::consts::PI;
|
|
||||||
use std::fs::File;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use crate::system::{GriddedPosition, grid::Position, Storage};
|
use crate::system::{GriddedPosition, Storage};
|
||||||
use crate::system::grid::VectorStorage;
|
use crate::system::walker::Walker;
|
||||||
use crate::system::walker::{LocalRandomWalker, Walker};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use crate::system::nd::{NDPosition, NDVectorStorage};
|
|
||||||
|
|
||||||
pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> {
|
pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> {
|
||||||
rng: R,
|
rng: R,
|
||||||
|
|||||||
@ -75,11 +75,11 @@ impl<const DIM: usize> GriddedPosition for NDPosition<DIM> {
|
|||||||
.sqrt();
|
.sqrt();
|
||||||
|
|
||||||
for i in 0..DIM {
|
for i in 0..DIM {
|
||||||
a[i] = a[i] / norm;
|
a[i] = a[i] * radius / norm;
|
||||||
b[i] = a[i] as i32;
|
b[i] = a[i] as i32;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Self(b)
|
return Self(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn abs(&self) -> f32 {
|
fn abs(&self) -> f32 {
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
use num_integer::Integer;
|
|
||||||
use rand::prelude::Rng;
|
use rand::prelude::Rng;
|
||||||
use crate::system::{GriddedPosition, grid::Position};
|
use crate::system::GriddedPosition;
|
||||||
|
|
||||||
pub trait Walker<P: GriddedPosition> {
|
pub trait Walker<P: GriddedPosition> {
|
||||||
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P;
|
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P;
|
||||||
@ -22,35 +21,35 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn uniformity() {
|
fn uniformity() {
|
||||||
let walker = LocalRandomWalker::new();
|
let walker = LocalRandomWalker;
|
||||||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
||||||
let mut results: Vec<Position> = vec![];
|
let mut results: Vec<Position> = vec![];
|
||||||
|
|
||||||
let origin = &Position::zero();
|
let origin = &Position::zero();
|
||||||
|
|
||||||
let x: u32 = (1_000000_000);
|
let x: u32 = (1_000_000);
|
||||||
for i in 0..x {
|
for i in 0..x {
|
||||||
results.push(walker.walk(&mut rng, origin));
|
results.push(walker.walk(&mut rng, origin));
|
||||||
}
|
}
|
||||||
|
|
||||||
let a = results
|
let a = results
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| **a == Position(0, 1))
|
.filter(|a| **a == Position { x: 0, y: 1 })
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
let b = results
|
let b = results
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| **a == Position(0, -1))
|
.filter(|a| **a == Position { x: 0, y: -1 })
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
let c = results
|
let c = results
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| **a == Position(1, 0))
|
.filter(|a| **a == Position { x: 1, y: 0 })
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
let d = results
|
let d = results
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| **a == Position(-1, 0))
|
.filter(|a| **a == Position { x: -1, y: 0 })
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
println!("{} {} {} {}", a as f32 / x as f32, b as f32 / x as f32, c as f32 / x as f32, d as f32 / x as f32);
|
println!("{} {} {} {}", a as f32 / x as f32, b as f32 / x as f32, c as f32 / x as f32, d as f32 / x as f32);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user