make seperate struct for buffer
This commit is contained in:
39
src/buffer.rs
Normal file
39
src/buffer.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
#[derive(Debug)]
|
||||
pub struct Buf {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub buf: Vec<f32>,
|
||||
}
|
||||
|
||||
impl Clone for Buf {
|
||||
fn clone(&self) -> Buf {
|
||||
Buf {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
buf: self.buf.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Buf {
|
||||
pub fn new(width: usize, height: usize, buf: Vec<f32>) -> Self {
|
||||
Buf {
|
||||
width,
|
||||
height,
|
||||
buf,
|
||||
}
|
||||
}
|
||||
|
||||
// Truncate x and y and return a corresponding index into the data slice.
|
||||
fn index(&self, x: f32, y: f32) -> usize {
|
||||
// x/y can come in negative, hence we shift them by width/height.
|
||||
let i = (x + self.width as f32) as usize & (self.width - 1);
|
||||
let j = (y + self.height as f32) as usize & (self.height - 1);
|
||||
j * self.width + i
|
||||
}
|
||||
|
||||
// Get the buffer value at a given position. The implementation effectively treats data as periodic, hence any finite position will produce a value.
|
||||
pub fn get_buf(&self, x: f32, y: f32) -> f32 {
|
||||
self.buf[self.index(x, y)]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user