feat: initial commit

This commit is contained in:
Nathan
2025-06-03 15:48:59 -06:00
commit eb59c1b50a
8 changed files with 1998 additions and 0 deletions

76
dot-matrix/src/main.rs Normal file
View File

@@ -0,0 +1,76 @@
use rand::prelude::*;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, DrawingArea};
use gtk::cairo::Context;
fn main() {
let app = Application::builder()
.application_id("com.example.DotMatrix")
.build();
app.connect_activate(build_ui);
app.run();
}
fn build_ui(app: &gtk::Application) {
let window = ApplicationWindow::builder()
.application(app)
.title("Dot Matrix")
.default_width(1000)
.default_height(800)
.build();
let drawing_area = DrawingArea::new();
let mut rng = rand::rng();
let rows = 100 as u32;
let cols = 100 as u32;
// Generate the matrix using iterators
let dot_matrix: Vec<Vec<u32>> = (0..rows)
.map(|_| {
(0..cols)
.map(|_| rng.random_range(0..=10))
.collect()
})
.collect();
drawing_area.set_draw_func(move |_area, cr, width, height| {
draw_dot_matrix(cr, width, height, &dot_matrix);
});
window.set_child(Some(&drawing_area));
window.show();
}
fn draw_dot_matrix(cr: &Context, width: i32, height: i32, matrix: &Vec<Vec<u32>>) {
let rows = matrix.len();
let cols = if rows > 0 { matrix[0].len() } else { 0 };
// Background: dark gray
cr.set_source_rgb(0.1, 0.1, 0.1);
cr.rectangle(0.0, 0.0, width as f64, height as f64);
cr.fill();
// Set dot color (yellow)
cr.set_source_rgb(1.0, 1.0, 0.0);
// Calculate spacing
let spacing_x = 8.0;
let spacing_y = 8.0;
let radius = 2.0;
for (row_index, row) in matrix.iter().enumerate() {
for (col_index, &val) in row.iter().enumerate() {
if val == 1 {
let x = (col_index as f64 + 0.5) * spacing_x;
let y = (row_index as f64 + 0.5) * spacing_y;
cr.arc(x, y, radius, 0.0, std::f64::consts::PI * 2.0);
cr.fill();
}
}
}
}