Skip to content

Commit

Permalink
Tracing and drawing islands
Browse files Browse the repository at this point in the history
  • Loading branch information
madsravn committed Aug 15, 2023
1 parent 83c3428 commit eeac664
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pixtra"
version = "0.2.4"
version = "0.2.5"
edition = "2021"
authors = ["Mads Ravn <madsravn@gmail.com>"]
license = "GPL-3.0"
Expand Down
63 changes: 63 additions & 0 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,59 @@ impl Canvas {
}
}

// TODO: Four parameters? Ugly
pub fn trace(mut self, island: &Island, left: u32, right: u32, up: u32, down: u32) -> Canvas {
let left: i64 = left.into();
let right: i64 = right.into();
let up: i64 = up.into();
let down: i64 = down.into();
let color = self.get_pixel(island.points[0].x, island.points[0].y);

for point in island.points.iter() {

for x in -left..=right {
for y in -up..=down {
self.set_pixel_mut_signed(point.x as i64 + x, point.y as i64 + y, &color);
}
}
}

self
}

pub fn trace_mut(&mut self, island: &Island, left: u32, right: u32, up: u32, down: u32) {
let left: i64 = left.into();
let right: i64 = right.into();
let up: i64 = up.into();
let down: i64 = down.into();
let color = self.get_pixel(island.points[0].x, island.points[0].y);

for point in island.points.iter() {

for x in -left..=right {
for y in -up..=down {
self.set_pixel_mut_signed(point.x as i64 + x, point.y as i64 + y, &color);
}
}
}

}

pub fn draw_island(mut self, island: &Island, color: &Pixel) -> Canvas {
for point in island.points.iter() {
self.set_pixel_mut(point.x, point.y, color);
}

self
}

pub fn draw_island_mut(&mut self, island: &Island, color: &Pixel) {
for point in island.points.iter() {
self.set_pixel_mut(point.x, point.y, color);
}

}

/// Draws canvas `canvas` as a subimage at `(x, y)`
pub fn draw_subimage(mut self, x: u32, y: u32, canvas: &Canvas) -> Canvas {
let binding = self.clone();
Expand Down Expand Up @@ -612,15 +665,25 @@ impl Canvas {

/// Sets pixel at position `(x, y)` to `pixel`
pub fn set_pixel(mut self, x: u32, y: u32, pixel: &Pixel) -> Canvas {
// TODO: We need to make sure that (x, y) is within the border
self.pixels[(self.width * y + x) as usize] = pixel.clone();
self
}

/// Mutable sets pixel at position `(x, y)` to `pixel`
pub fn set_pixel_mut(&mut self, x: u32, y: u32, pixel: &Pixel) {
// TODO: We need to make sure that (x, y) is within the border
self.pixels[(self.width * y + x) as usize] = pixel.clone();
}

/// Mutable sets pixel at position `(x, y)` to `pixel` if `x: i64` and `y: i64` is within image
/// bounds
pub fn set_pixel_mut_signed(&mut self, x: i64, y: i64, pixel: &Pixel) {
if self.in_bounds(x, y) {
self.set_pixel_mut(x as u32, y as u32, pixel);
}
}

/// Turns the entire canvas grayscale.
pub fn to_grey(&self) -> Canvas {
let pixels = self.pixels.iter().map(|x| to_grey_lumiosity(x)).collect();
Expand Down

0 comments on commit eeac664

Please sign in to comment.