Skip to content

Commit

Permalink
changed type of size params from usize to u32
Browse files Browse the repository at this point in the history
  • Loading branch information
salam99823 committed Oct 23, 2024
1 parent 334361c commit 7fcb962
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
30 changes: 17 additions & 13 deletions src/bin_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::{utils::Point, Vec2};

pub struct BinImage {
data: Vec<u8>,
pub height: usize,
pub width: usize,
pub height: u32,
pub width: u32,
}

impl BinImage {
pub fn new(height: usize, width: usize, data: &[u8]) -> Self {
let compress_step = data.len() / (height * width);
pub fn new(height: u32, width: u32, data: &[u8]) -> Self {
let compress_step = data.len() / (height * width) as usize;
Self {
data: data
.chunks(8 * compress_step)
Expand All @@ -29,7 +29,11 @@ impl BinImage {
/// get pixel value at given coordinate
pub fn get(&self, (x, y): Point) -> bool {
let index = y * self.width + x;
if let Some(mut byte) = self.data.get(index / 8 /* index of byte */).copied() {
if let Some(mut byte) = self
.data
.get((index / 8) as usize /* index of byte */)
.copied()
{
byte >>= index % 8; // index of bit
x <= self.width && byte & 1 > 0
} else {
Expand All @@ -39,14 +43,14 @@ impl BinImage {

pub fn get_neighbors(&self, (x, y): Point) -> [bool; 8] {
[
y < usize::MAX && self.get((x, y + 1)),
y > usize::MIN && self.get((x, y - 1)),
x < usize::MAX && self.get((x + 1, y)),
x > usize::MIN && self.get((x - 1, y)),
x < usize::MAX && y < usize::MAX && self.get((x + 1, y + 1)),
x > usize::MIN && y > usize::MIN && self.get((x - 1, y - 1)),
x < usize::MAX && y > usize::MIN && self.get((x + 1, y - 1)),
x > usize::MIN && y < usize::MAX && self.get((x - 1, y + 1)),
y < u32::MAX && self.get((x, y + 1)),
y > u32::MIN && self.get((x, y - 1)),
x < u32::MAX && self.get((x + 1, y)),
x > u32::MIN && self.get((x - 1, y)),
x < u32::MAX && y < u32::MAX && self.get((x + 1, y + 1)),
x > u32::MIN && y > u32::MIN && self.get((x - 1, y - 1)),
x < u32::MAX && y > u32::MIN && self.get((x + 1, y - 1)),
x > u32::MIN && y < u32::MAX && self.get((x - 1, y + 1)),
]
}

Expand Down
26 changes: 15 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ pub struct Edges {

impl Edges {
#[must_use]
pub fn new(height: usize, width: usize, data: &[u8]) -> Self {
pub fn new(height: u32, width: u32, data: &[u8]) -> Self {
Self {
image: BinImage::new(height, width, data),
}
}

/// If there's only one sprite / object in the image, this returns just one, with
/// coordinates translated to either side of (0, 0)
#[must_use]
Expand Down Expand Up @@ -94,7 +95,7 @@ impl Edges {
while drawn_points_with_counts.len() < points.len() {
if let Some(p) = points
.iter()
.filter(|p| (distance(current, p) - 1.0).abs() <= f32::EPSILON)
.filter(|p| (distance(current, **p) - 1.0).abs() <= f32::EPSILON)
.min_by_key(|n| drawn_points_with_counts.get(n).map_or(0, |c| *c))
{
current = *p;
Expand Down Expand Up @@ -147,23 +148,26 @@ impl Edges {
#[cfg(feature = "bevy")]
impl From<bevy_render::prelude::Image> for Edges {
fn from(i: bevy_render::prelude::Image) -> Edges {
Self::new(i.height() as usize, i.width() as usize, &i.data)
Self::new(i.height(), i.width(), &i.data)
}
}

impl From<image::DynamicImage> for Edges {
fn from(i: image::DynamicImage) -> Edges {
Self::new(i.height() as usize, i.width() as usize, i.as_bytes())
Self::new(i.height(), i.width(), i.as_bytes())
}
}

#[cfg(feature = "bevy")]
impl From<&bevy_render::prelude::Image> for Edges {
fn from(i: &bevy_render::prelude::Image) -> Edges {
Self::new(i.height(), i.width(), &i.data)
}
}

impl<T> From<&T> for Edges
where
T: Clone,
Edges: From<T>,
{
fn from(value: &T) -> Self {
Self::from(value.clone())
impl From<&image::DynamicImage> for Edges {
fn from(i: &image::DynamicImage) -> Edges {
Self::new(i.height(), i.width(), i.as_bytes())
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub type Point = (usize, usize);
pub type Point = (u32, u32);

// d=√((x2-x1)²+(y2-y1)²)
pub fn distance((x2, y2): Point, (x1, y1): &Point) -> f32 {
((x2 as f32 - *x1 as f32).powi(2) + (y2 as f32 - *y1 as f32).powi(2)).sqrt()
pub fn distance((x2, y2): Point, (x1, y1): Point) -> f32 {
((x2 as f32 - x1 as f32).powi(2) + (y2 as f32 - y1 as f32).powi(2)).sqrt()
}

0 comments on commit 7fcb962

Please sign in to comment.