imgsyn/src/canvas.rs
2026-02-09 13:17:27 +01:00

56 lines
1.3 KiB
Rust

use crate::{ImgSyn, pixel::Pixel};
#[derive(Clone, Copy, PartialEq)]
pub struct Canvas {
pub width: u32,
pub height: u32,
pub default_px: Pixel,
}
#[derive(Clone, PartialEq)]
pub struct PaintedCanvas {
pub meta: Canvas,
pub array: Vec<Vec<Pixel>>,
}
impl Canvas {
pub fn new(width: u32, height: u32, default_px: Pixel) -> Self {
Self {
width,
height,
default_px,
}
}
}
impl ImgSyn for Canvas {
fn getpx(&self, _meta: Canvas, _x: f32, _y: f32) -> Pixel {
self.default_px
}
}
impl PaintedCanvas {
pub fn new(meta @ Canvas { width, height, .. }: Canvas, function: impl ImgSyn) -> Self {
Self {
meta,
array: (0..width)
.map(|x| x as f32 / width as f32)
.map(|x| {
(0..height)
.map(|y| y as f32 / height as f32)
.map(|y| function.getpx(meta, x, y))
.collect()
})
.collect(),
}
}
}
impl ImgSyn for PaintedCanvas {
fn getpx(&self, meta: Canvas, x: f32, y: f32) -> Pixel {
// float coords -> int coords
let x = (x * meta.width as f32).round() as usize;
let y = (y * meta.height as f32).round() as usize;
self.array[x][y]
}
}