Skip to content

Instantly share code, notes, and snippets.

@arskiy
Last active April 10, 2020 00:36
Show Gist options
  • Save arskiy/524e1a9a6a78339d70d40c19aa98f720 to your computer and use it in GitHub Desktop.
Save arskiy/524e1a9a6a78339d70d40c19aa98f720 to your computer and use it in GitHub Desktop.
pub struct Point {
x: i32,
y: i32,
}
fn line(x1: i32, y1: i32, x2: i32, y2: i32, map: &Map) -> Vec<Point> {
let mut coordinates = vec![];
let mut x1 = x1;
let mut y1 = y1;
let dx = i32::abs(x2 - x1);
let dy = i32::abs(y2 - y1);
let sx = if x1 < x2 { 1 } else { -1 };
let sy = if y1 < y2 { 1 } else { -1 };
let mut err = if dx > dy { dx / 2 } else { -dy / 2 };
let mut err2;
loop {
coordinates.push(Point { x: x1, y: y1 });
if x1 == x2 && y1 == y2 || map[x1 as usize][y1 as usize].block_sight {
break;
}
err2 = err;
if err2 > -dx {
err -= dy;
x1 += sx;
}
if err2 < dy {
err += dx;
y1 += sy;
}
}
coordinates
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment