Bitmap/Bresenham's line algorithm: Difference between revisions

(Added solution for Action!)
Line 3,787:
 
fn main() {
let resultmut points: Vec<Point> = get_coordinatesVec::new(1, 1, 69, 28);
points.append(&mut get_coordinates(1, 20, 20, 28));
draw_line(result, 70, 30);
points.append(&mut get_coordinates(20, 28, 69, 0));
draw_line(resultpoints, 70, 30);
}
 
fn get_coordinates(x1: i32, y1: i32, x2: i32, y2: i32) -> Vec<Point> {
let mut coordinates: Vec<Point> = vec![];
let dx:i32 = i32::abs(x2 - x1);
let dy:i32 = i32::abs(y2 - y1);
let sx:i32 = { if x1 < x2 { 1 } else { -1 } };
let sy:i32 = { if x1y1 < x2y2 { 1 } else { -1 } };
 
1
let mut error:i32 = (if dx > dy { dx } else { -dy }) / 2 ;
-1
}
};
let sy:i32 ={
if y1 < y2 {
1
} else {
-1
}
};
let mut error:i32 = dx - dy;
let mut current_x:i32 = x1;
let mut current_y:i32 = y1;
loop {
coordinates.push(Point { x : current_x, y: current_y });
while current_x != x2 && current_y != y2 {
 
let error2:i32 = 2 * error;
if error2current_x >== i32::abs(dy)x2 && current_y == y2 { break; }
 
let syerror2:i32 ={ error;
 
if error2 > -1dx {
error -= dy;
current_x += sx;
1}
coordinates.push(Point { x: current_x, y: current_y });
} else if error2 <= i32::abs(dx)dy {
error -+= dx;
current_y += sy;
coordinates.push(Point { x: current_x, y: current_y });
}
}
coordinates
}
 
fn draw_line(line: std::vec::Vec<Point>, width: i32, height: i32) {
for col in 0..height {
for row in 0..width {
let is_point_in_line = line.iter().any(| point| point.x == row && point.y == col);
match is_point_in_line {
true => print!("❖"),
_ => {
if col == 0 || col == (height - 1) || row == 0 || row == (width - 1) {
print!("☗");
} else {
print!(".");
}
}
};
}
print!("\n");
}
}
</lang>
'''Output:'''
<pre>
.....................................................................@
☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗
☗❖❖❖...................................................................@@.
..❖❖❖...............................................................@@...
....❖❖❖❖...........................................................@@.....
.......❖❖❖.......................................................@.......
.........❖❖❖❖...................................................@@........
............❖❖❖..............................................@@..........
..............❖❖❖❖..........................................@@............
.................❖❖❖......................................@..............
...................❖❖❖❖..................................@@...............
......................❖❖❖.............................@@.................
........................❖❖❖❖.........................@@...................
...........................❖❖❖.....................@.....................
.............................❖❖❖❖.................@@......................
................................❖❖❖............@@........................
..................................❖❖❖❖........@@..........................
.....................................❖❖❖....@............................
.......................................❖❖❖❖@@.............................
.....................................@@........❖❖❖.......................
...................................@@.............❖❖❖❖....................
.@@...............................@.................❖❖❖..................
...@@...........................@@.....................❖❖❖❖...............
.....@@.......................@@.........................❖❖❖.............
.......@@@..................@@..............................❖❖❖❖..........
..........@@...............@..................................❖❖❖........
............@@@..........@@......................................❖❖❖❖.....
...............@@......@@..........................................❖❖❖...
.................@@..@@...............................................❖❖❖❖☗
...................@@.................................................❖☗
......................................................................
☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗☗
</pre>
 
Anonymous user