added Vec2::size

This commit is contained in:
dani 2023-07-01 14:07:07 +00:00
parent c223a2d900
commit 36c8b87d47
2 changed files with 43 additions and 15 deletions

View File

@ -1,17 +1,17 @@
use std::fs::File;
use crate::vec2::Vec2;
use std::fs::File;
//todo: make dynamically different bitdepths
pub struct Image {
data: Vec<u8>,
size: Vec2<i32>
size: Vec2<i32>,
}
impl Image {
pub fn new(size: Vec2<i32>) -> Self {
Image {
data: vec![0u8; (size.x * size.y) as usize],
size
data: vec![0u8; size.size()],
size,
}
}
@ -29,7 +29,10 @@ impl Image {
decoder.read_into_buffer(&mut data).unwrap();
Image {
data,
size: Vec2 {x: x as i32, y: y as i32}
size: Vec2 {
x: x as i32,
y: y as i32,
},
}
}
@ -50,10 +53,20 @@ impl Image {
for y in 0..image.size.y {
for x in 0..image.size.x {
//todo: implement better(very stupid to do per pixel)
if x + pos.x >= 0 && y + pos.y >= 0 && x + pos.x < self.size.x && y + pos.y < self.size.y {
if x + pos.x >= 0
&& y + pos.y >= 0
&& x + pos.x < self.size.x
&& y + pos.y < self.size.y
{
let p = image.get_pixel(Vec2 { x, y });
if p > 0 {
self.set_pixel(Vec2 { x: x + pos.x, y: y + pos.y }, p);
self.set_pixel(
Vec2 {
x: x + pos.x,
y: y + pos.y,
},
p,
);
}
}
}
@ -75,7 +88,13 @@ impl Image {
let xs = xdiff / step;
let ys = ydiff / step;
for _ in 1..=(step as i32) {
self.set_pixel(Vec2 { x: x as i32, y: y as i32 }, color);
self.set_pixel(
Vec2 {
x: x as i32,
y: y as i32,
},
color,
);
x += xs;
y += ys;
}

View File

@ -1,14 +1,23 @@
use num::Num;
use num::{Num, ToPrimitive};
#[derive(Copy, Clone)]
pub struct Vec2<T : Num + Copy> {
pub struct Vec2<T: Num + Copy + ToPrimitive> {
pub x: T,
pub y: T,
}
impl<T: Num + Copy> Vec2<T> {
//pub static
pub fn zero() -> Self {
Vec2 {x: T::zero(), y: T::zero()}
Vec2 {
x: T::zero(),
y: T::zero(),
}
}
//pub
pub fn size(&self) -> usize {
(self.x * self.y) as usize
}
}
@ -16,7 +25,7 @@ impl<T : Num + Copy + Default> Default for Vec2<T> {
fn default() -> Self {
Vec2 {
x: T::default(),
y: T::default()
y: T::default(),
}
}
}