Skip to content

Commit

Permalink
Replace line_drawing with clipline (#381)
Browse files Browse the repository at this point in the history
* Replaced `line_drawing` with `clipline`

* Update `clipline` to 0.1.1
  • Loading branch information
nxsaken authored Oct 31, 2023
1 parent 763a7a9 commit 5461133
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
19 changes: 8 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/conway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ byteorder = "1"
env_logger = "0.10"
error-iter = "0.4"
getrandom = "0.2"
line_drawing = "1"
clipline = "0.1.1"
log = "0.4"
pixels = { path = "../.." }
randomize = "3"
Expand Down
21 changes: 9 additions & 12 deletions examples/conway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,16 @@ impl ConwayGrid {
}
}

fn set_line(&mut self, x0: isize, y0: isize, x1: isize, y1: isize, alive: bool) {
// probably should do sutherland-hodgeman if this were more serious.
// instead just clamp the start pos, and draw until moving towards the
// end pos takes us out of bounds.
let x0 = x0.clamp(0, self.width as isize);
let y0 = y0.clamp(0, self.height as isize);
for (x, y) in line_drawing::Bresenham::new((x0, y0), (x1, y1)) {
if let Some(i) = self.grid_idx(x, y) {
self.cells[i].set_alive(alive);
} else {
break;
}
fn set_line(&mut self, x0: isize, y0: isize, x1: isize, y1: isize, alive: bool) -> Option<()> {
// possible to optimize by matching on Clipline and iterating over its arms
for (x, y) in clipline::Clipline::new(
((x0, y0), (x1, y1)),
((0, 0), (self.width as isize - 1, self.height as isize - 1)),
)? {
let (x, y) = (x as usize, y as usize);
self.cells[x + y * self.width].set_alive(alive);
}
Some(())
}

fn grid_idx<I: std::convert::TryInto<usize>>(&self, x: I, y: I) -> Option<usize> {
Expand Down
2 changes: 1 addition & 1 deletion examples/invaders/simple-invaders/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Jay Oster <jay@kodewerx.org>"]
edition = "2018"

[dependencies]
line_drawing = "1.0"
clipline = "0.1.1"
pcx = "0.2"
randomize = "3.0"

Expand Down
16 changes: 7 additions & 9 deletions examples/invaders/simple-invaders/src/sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use crate::TIME_STEP;
use crate::{Point, HEIGHT, WIDTH};
use alloc::rc::Rc;
use alloc::vec::Vec;
use core::cmp::min;
use core::time::Duration;
use line_drawing::Bresenham;

// This is the type stored in the `Assets` hash map
pub(crate) type CachedSprite = (usize, usize, Rc<[u8]>);
Expand Down Expand Up @@ -215,17 +213,17 @@ where
}

/// Draw a line to the pixel buffer using Bresenham's algorithm.
pub(crate) fn line(screen: &mut [u8], p1: &Point, p2: &Point, color: [u8; 4]) {
let p1 = (p1.x as i64, p1.y as i64);
let p2 = (p2.x as i64, p2.y as i64);

for (x, y) in Bresenham::new(p1, p2) {
let x = min(x as usize, WIDTH - 1);
let y = min(y as usize, HEIGHT - 1);
pub(crate) fn line(screen: &mut [u8], p1: &Point, p2: &Point, color: [u8; 4]) -> Option<()> {
let p1 = (p1.x as isize, p1.y as isize);
let p2 = (p2.x as isize, p2.y as isize);
let clip_max = (WIDTH as isize - 1, HEIGHT as isize - 1);
for (x, y) in clipline::Clipline::new((p1, p2), ((0, 0), clip_max))? {
let (x, y) = (x as usize, y as usize);
let i = x * 4 + y * WIDTH * 4;

screen[i..i + 4].copy_from_slice(&color);
}
Some(())
}

/// Draw a rectangle to the pixel buffer using two points in opposite corners.
Expand Down

0 comments on commit 5461133

Please sign in to comment.