feat: Add end position control
This commit is contained in:
29
src/main.rs
29
src/main.rs
@@ -36,11 +36,13 @@ fn current_distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
|
||||
((x2 - x1).powi(2) + (y2 - y1).powi(2)).sqrt()
|
||||
}
|
||||
|
||||
fn visualizer(grid: f64, points: &Vec<[f64; 2]>) {
|
||||
fn visualizer(grid: f64, points: &Vec<[f64; 2]>, blocked_points: &Vec<[f64; 2]>) {
|
||||
for i in 0..grid as i32 {
|
||||
for j in 0..grid as i32 {
|
||||
if points.contains(&[i as f64, j as f64]) {
|
||||
print!("▒▒▒");
|
||||
} else if blocked_points.contains(&[i as f64, j as f64]) {
|
||||
print!(" ");
|
||||
} else {
|
||||
print!("▇▇▇");
|
||||
}
|
||||
@@ -50,16 +52,23 @@ fn visualizer(grid: f64, points: &Vec<[f64; 2]>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn pathfinder(home: [f64; 2], _final: [f64; 2]) -> Vec<[f64; 2]> {
|
||||
fn pathfinder(home: [f64; 2], _final: [f64; 2], blocked_points: &Vec<[f64; 2]>) -> Vec<[f64; 2]> {
|
||||
let mut current_pos = home;
|
||||
let mut points: Vec<[f64; 2]> = vec![home, _final];
|
||||
while current_pos != _final {
|
||||
let adjacent_squares = vec![
|
||||
let neighbours = vec![
|
||||
[current_pos[0] + 1.0, current_pos[1]],
|
||||
[current_pos[0], current_pos[1] + 1.0],
|
||||
[current_pos[0] - 1.0, current_pos[1]],
|
||||
[current_pos[0], current_pos[1] - 1.0],
|
||||
];
|
||||
let mut adjacent_squares: Vec<[f64; 2]> = vec![];
|
||||
for square in neighbours.iter() {
|
||||
if blocked_points.contains(square) {
|
||||
continue;
|
||||
}
|
||||
adjacent_squares.push(*square);
|
||||
}
|
||||
current_pos = *adjacent_squares
|
||||
.iter()
|
||||
.min_by(|a, b| {
|
||||
@@ -77,11 +86,21 @@ fn main() {
|
||||
const GRID: i32 = 30;
|
||||
let home = [3.0, 5.0];
|
||||
let mut _final = [22.0, 12.0];
|
||||
let blocked_points = vec![
|
||||
[13.0, 15.0],
|
||||
[13.0, 14.0],
|
||||
[12.0, 14.0],
|
||||
[7.0, 7.0],
|
||||
[7.0, 8.0],
|
||||
[8.0, 8.0],
|
||||
[9.0, 8.0],
|
||||
[10.0, 8.0],
|
||||
];
|
||||
enable_raw_mode().unwrap();
|
||||
loop {
|
||||
execute!(stdout(), Clear(ClearType::All), MoveTo(0, 0)).unwrap();
|
||||
let points = pathfinder(home, _final);
|
||||
visualizer(GRID as f64, &points);
|
||||
let points = pathfinder(home, _final, &blocked_points);
|
||||
visualizer(GRID as f64, &points, &blocked_points);
|
||||
if let Event::Key(event) = read().unwrap() {
|
||||
match event.code {
|
||||
KeyCode::Char(c) => {
|
||||
|
||||
Reference in New Issue
Block a user