Added a 2D quasi crystal lattice render.

This commit is contained in:
2025-04-14 16:35:39 -05:00
parent b97fa74af2
commit 83331c4265
5 changed files with 641 additions and 50 deletions

View File

@@ -0,0 +1,8 @@
[package]
name = "quasi_lattices_2d"
version = "0.1.0"
edition = "2024"
[dependencies]
minifb = "0.28.0"
nalgebra = "0.33.2"

View File

@@ -0,0 +1,87 @@
use minifb::{Key, Window, WindowOptions};
use nalgebra::DMatrix;
const WIDTH: usize = 800;
const HEIGHT: usize = 800;
const POINT_RADIUS: usize = 1;
const DIMENSION: usize = 11;
const SIZE: usize = 3;
const SCALE: f32 = 40.0;
fn generate_projection_matrix() -> DMatrix<f32> {
let theta = 2.0 * std::f32::consts::PI / ( DIMENSION as f32);
let mut components = Vec::new();
for i in 0..DIMENSION {
components.push((i as f32 * theta).cos());
components.push((i as f32 * theta).sin());
}
let matrix = DMatrix::from_vec(2, DIMENSION, components);
matrix
}
fn draw_point(buffer: &mut [u32], x: usize, y: usize, color: u32) {
for dx in 0..=POINT_RADIUS {
for dy in 0..=POINT_RADIUS {
let px = x + dx;
let py = y + dy;
if px < WIDTH && py < HEIGHT {
buffer[py * WIDTH + px] = color;
}
}
}
}
fn main() {
let mut window = Window::new("2D Lattice - ESC to exit", WIDTH, HEIGHT, WindowOptions::default())
.unwrap_or_else(|e| panic!("{}", e));
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
let mut points = Vec::new();
let origin = vec![0;DIMENSION];
points.push(origin);
for i in 0..DIMENSION {
let mut new_points = points.clone();
for point in &points {
for j in 1..SIZE {
let mut new_point = point.clone();
new_point[i] = j;
new_points.push(new_point);
}
}
points = new_points;
}
let dot = vec![1;DIMENSION];
let dot = DMatrix::from_vec(1, DIMENSION, dot);
let projection_matrix = generate_projection_matrix();
while window.is_open() && !window.is_key_down(Key::Escape) {
// Clear screen
buffer.fill(0);
for point in &points {
let plane = 2;
let vector = DMatrix::from_vec(DIMENSION, 1, point.to_vec());
let dot_product = &dot * &vector;
if dot_product[(0,0)] != plane {
//continue;
}
let point_as_f32: Vec<f32> = point.iter().map(|&e| e as f32).collect();
let vector = DMatrix::from_vec(DIMENSION, 1, point_as_f32.to_vec());
let result = &projection_matrix*vector;
let x = result[(0,0)] * SCALE;
let y = result[(1,0)] * SCALE;
let x = x as isize + (WIDTH as isize) / 2;
let y = y as isize + (HEIGHT as isize) / 2;
draw_point(&mut buffer, x as usize, y as usize, 0xFFFFFF);
}
window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap();
}
}