feat: initial commit
This commit is contained in:
76
dot-matrix/src/main.rs
Normal file
76
dot-matrix/src/main.rs
Normal 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: >k::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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user