Skip to content

Instantly share code, notes, and snippets.

@linuxlizard
Last active June 6, 2023 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linuxlizard/1645b0d4129adff90a1024a7516ca6a4 to your computer and use it in GitHub Desktop.
Save linuxlizard/1645b0d4129adff90a1024a7516ca6a4 to your computer and use it in GitHub Desktop.
Bresenham's line algorithm in Rust via ChatGPT
struct Point {
x: i32,
y: i32,
}
// via ChatGPT4 (!!!)
fn teleport(p1: Point, p2: Point) -> Vec<Point> {
let mut points = Vec::new();
let mut x1 = p1.x;
let mut y1 = p1.y;
let x2 = p2.x;
let y2 = p2.y;
let dx = (x2 - x1).abs();
let dy = (y2 - y1).abs();
let mut sx = 0;
let mut sy = 0;
if x1 < x2 {
sx = 1;
} else {
sx = -1;
}
if y1 < y2 {
sy = 1;
} else {
sy = -1;
}
let mut err = dx - dy;
loop {
points.push(Point { x: x1, y: y1 });
if x1 == x2 && y1 == y2 {
break;
}
let e2 = 2 * err;
if e2 > -dy {
err -= dy;
x1 += sx;
}
if e2 < dx {
err += dx;
y1 += sy;
}
}
points
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment