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 {
let mut next = field.clone();
for x in 0..field.width {
for y in 0..field.height {
for x in 0..field.width() {
for y in 0..field.height() {
let old_state = field.get(x, y);
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
|| ny < 0
|| nx >= field.width as i32
|| ny >= field.height as i32
|| nx >= field.width() as i32
|| ny >= field.height() as i32
{
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 rng = rand::thread_rng();
let d = distributions::Bernoulli::new(probability).unwrap();
for x in 0..field.width {
for y in 0..field.height {
for x in 0..field.width() {
for y in 0..field.height() {
field.set(x, y, rng.sample(d));
}
}

View file

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

View file

@ -74,8 +74,8 @@ impl From<Command> for Packet {
CommandCode::CharBrightness.into(),
x,
y,
grid.width as u16,
grid.height as u16,
grid.width() as u16,
grid.height() as u16,
),
grid.into(),
),
@ -95,11 +95,11 @@ impl From<Command> for Packet {
compression,
) => {
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_w = pixels.width as u16 / TILE_SIZE;
let pixel_h = pixels.height as u16;
let tile_w = pixels.width() as u16 / TILE_SIZE;
let pixel_h = pixels.height() as u16;
let payload = into_compressed(compression, pixels.into());
let command = match compression {
CompressionCode::Uncompressed => {
@ -153,8 +153,8 @@ impl From<Command> for Packet {
CommandCode::Cp437Data.into(),
x,
y,
grid.width as u16,
grid.height as u16,
grid.width() as u16,
grid.height() as u16,
),
grid.into(),
),

View file

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