Basic path finder
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "traffic"
|
||||
version = "0.1.0"
|
||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "traffic"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
50
src/main.rs
Normal file
50
src/main.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
vec,
|
||||
};
|
||||
|
||||
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]>) {
|
||||
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 {
|
||||
print!("▇▇▇");
|
||||
}
|
||||
io::stdout().flush().unwrap();
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let home_pos: [f64; 2] = [4.0, 4.0];
|
||||
let final_pos: [f64; 2] = [25.0, 22.0];
|
||||
let mut points: Vec<[f64; 2]> = vec![[4.0, 4.0], [55.0, 22.0]];
|
||||
|
||||
let mut current_pos = home_pos;
|
||||
|
||||
while current_pos != final_pos {
|
||||
let adjacent_squares = 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],
|
||||
];
|
||||
|
||||
current_pos = *adjacent_squares
|
||||
.iter()
|
||||
.min_by(|a, b| {
|
||||
let da = current_distance(a[0], a[1], final_pos[0], final_pos[1]);
|
||||
let db = current_distance(b[0], b[1], final_pos[0], final_pos[1]);
|
||||
da.partial_cmp(&db).unwrap()
|
||||
})
|
||||
.unwrap();
|
||||
points.push(current_pos);
|
||||
}
|
||||
visualizer(30.0, &points);
|
||||
}
|
||||
Reference in New Issue
Block a user