Created
August 6, 2022 16:54
-
-
Save slavfox/ecec311f5e732d171f26d0f9578167f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[inline] | |
pub fn copilot(x0: i32, y0: i32, x1: i32, y1: i32) -> Vec<(i32, i32)> { | |
let mut points = Vec::new(); | |
let mut x = x0; | |
let mut y = y0; | |
let dx = x1 - x0; | |
let dy = y1 - y0; | |
let mut step = if x1.abs() > x0.abs() { 1 } else { -1 }; | |
let mut err = dx.abs() - dy.abs(); | |
while x != x1 { | |
points.push((x, y)); | |
if err > 0 { | |
x += step; | |
err -= dy.abs(); | |
} else { | |
y += step; | |
err += dx.abs(); | |
} | |
} | |
points.push((x, y)); | |
points | |
} | |
#[inline] | |
pub fn bresenham(x0: i32, y0: i32, x1: i32, y1: i32) -> Vec<(i32, i32)> { | |
let mut points = Vec::new(); | |
let mut steep = (y1 - y0).abs() > (x1 - x0).abs(); | |
if steep { | |
points.push((y0, x0)); | |
points.push((y1, x1)); | |
} else { | |
points.push((x0, y0)); | |
points.push((x1, y1)); | |
} | |
if points[0].0 > points[1].0 { | |
points.swap(0, 1); | |
} | |
if points[0].1 > points[1].1 { | |
points.swap(0, 1); | |
} | |
let dx = points[1].0 - points[0].0; | |
let dy = points[1].1 - points[0].1; | |
let derror = dy.abs() * 2; | |
let mut error = 0; | |
let ystep = if points[0].1 < points[1].1 { 1 } else { -1 }; | |
let mut y = points[0].1; | |
for x in points[0].0..=points[1].0 { | |
if steep { | |
points.push((y, x)); | |
} else { | |
points.push((x, y)); | |
} | |
error += derror; | |
if error > dx { | |
y += ystep; | |
error -= dx * 2; | |
} | |
} | |
points | |
} | |
#[inline] | |
pub fn naive(x0: i32, y0: i32, x1: i32, y1: i32) -> Vec<(i32, i32)> { | |
let mut points = Vec::new(); | |
let dx = (x1 - x0).abs(); | |
let dy = (y1 - y0).abs(); | |
let length = if dx > dy { dx } else { dy }; | |
for i in 0..length { | |
let t = i as f32 / length as f32; | |
let x = x0 + ((x1 - x0) * t as i32); | |
let y = y0 + ((y1 - y0) * t as i32); | |
points.push((x, y)); | |
} | |
points | |
} | |
#[inline] | |
pub fn naive_rusty(x0: i32, y0: i32, x1: i32, y1: i32) -> Vec<(i32, i32)> { | |
let dx = x1 - x0; | |
let dy = y1 - y0; | |
let length = dx.abs().max(dy.abs()); | |
(0..=length).map( | |
|i| i as f32 / length as f32 | |
).map( | |
|t| { | |
(x0 + (dx * t as i32), y0 + (dy * t as i32)) | |
} | |
).collect() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment