make width and height private to make it read only

This commit is contained in:
Vinzenz Schroeter 2024-05-17 18:43:39 +02:00
parent ab66a6a33e
commit 5a717beda5
4 changed files with 37 additions and 21 deletions

View file

@ -39,8 +39,8 @@ fn main() {
fn iteration(field: PixelGrid) -> PixelGrid { fn iteration(field: PixelGrid) -> PixelGrid {
let mut next = field.clone(); let mut next = field.clone();
for x in 0..field.width { for x in 0..field.width() {
for y in 0..field.height { for y in 0..field.height() {
let old_state = field.get(x, y); let old_state = field.get(x, y);
let neighbors = count_neighbors(&field, x as i32, y as i32); let neighbors = count_neighbors(&field, x as i32, y as i32);
@ -64,8 +64,8 @@ fn count_neighbors(field: &PixelGrid, x: i32, y: i32) -> i32 {
if nx < 0 if nx < 0
|| ny < 0 || ny < 0
|| nx >= field.width as i32 || nx >= field.width() as i32
|| ny >= field.height as i32 || ny >= field.height() as i32
{ {
continue; // pixels outside the grid do not count continue; // pixels outside the grid do not count
} }
@ -85,8 +85,8 @@ fn make_random_field(probability: f64) -> PixelGrid {
let mut field = PixelGrid::max_sized(); let mut field = PixelGrid::max_sized();
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let d = distributions::Bernoulli::new(probability).unwrap(); let d = distributions::Bernoulli::new(probability).unwrap();
for x in 0..field.width { for x in 0..field.width() {
for y in 0..field.height { for y in 0..field.height() {
field.set(x, y, rng.sample(d)); field.set(x, y, rng.sample(d));
} }
} }

View file

@ -1,10 +1,8 @@
/// A 2D grid of bytes /// A 2D grid of bytes
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct ByteGrid { pub struct ByteGrid {
/// Size in the x-axis width: usize,
pub width: usize, height: usize,
/// Size in the y-axis
pub height: usize,
data: Vec<u8>, data: Vec<u8>,
} }
@ -57,6 +55,16 @@ impl ByteGrid {
pub fn mut_data_ref(&mut self) -> &mut [u8] { pub fn mut_data_ref(&mut self) -> &mut [u8] {
self.data.as_mut_slice() self.data.as_mut_slice()
} }
/// the size in x-direction
pub fn width(&self) -> usize {
self.width
}
/// the height in y-direction
pub fn height(&self) -> usize {
self.height
}
} }
impl From<ByteGrid> for Vec<u8> { impl From<ByteGrid> for Vec<u8> {

View file

@ -74,8 +74,8 @@ impl From<Command> for Packet {
CommandCode::CharBrightness.into(), CommandCode::CharBrightness.into(),
x, x,
y, y,
grid.width as u16, grid.width() as u16,
grid.height as u16, grid.height() as u16,
), ),
grid.into(), grid.into(),
), ),
@ -95,11 +95,11 @@ impl From<Command> for Packet {
compression, compression,
) => { ) => {
debug_assert_eq!(pixel_x % 8, 0); debug_assert_eq!(pixel_x % 8, 0);
debug_assert_eq!(pixels.width % 8, 0); debug_assert_eq!(pixels.width() % 8, 0);
let tile_x = pixel_x / TILE_SIZE; let tile_x = pixel_x / TILE_SIZE;
let tile_w = pixels.width as u16 / TILE_SIZE; let tile_w = pixels.width() as u16 / TILE_SIZE;
let pixel_h = pixels.height as u16; let pixel_h = pixels.height() as u16;
let payload = into_compressed(compression, pixels.into()); let payload = into_compressed(compression, pixels.into());
let command = match compression { let command = match compression {
CompressionCode::Uncompressed => { CompressionCode::Uncompressed => {
@ -153,8 +153,8 @@ impl From<Command> for Packet {
CommandCode::Cp437Data.into(), CommandCode::Cp437Data.into(),
x, x,
y, y,
grid.width as u16, grid.width() as u16,
grid.height as u16, grid.height() as u16,
), ),
grid.into(), grid.into(),
), ),

View file

@ -3,10 +3,8 @@ use crate::{BitVec, PIXEL_HEIGHT, PIXEL_WIDTH};
/// A grid of pixels stored in packed bytes. /// A grid of pixels stored in packed bytes.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct PixelGrid { pub struct PixelGrid {
/// the width in pixels width: usize,
pub width: usize, height: usize,
/// the height in pixels
pub height: usize,
bit_vec: BitVec, bit_vec: BitVec,
} }
@ -80,6 +78,16 @@ impl PixelGrid {
pub fn mut_data_ref(&mut self) -> &mut [u8] { pub fn mut_data_ref(&mut self) -> &mut [u8] {
self.bit_vec.mut_data_ref() self.bit_vec.mut_data_ref()
} }
/// the size in x-direction in pixels
pub fn width(&self) -> usize {
self.width
}
/// the height in y-direction in pixels
pub fn height(&self) -> usize {
self.height
}
} }
impl From<PixelGrid> for Vec<u8> { impl From<PixelGrid> for Vec<u8> {