commit d3d1e1b2e7eab7994ce2ce9b5a9312006e9e88b1 Author: Aditya Gupta Date: Mon Mar 2 19:11:57 2026 +0530 Basic path finder diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..153c940 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..599742e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traffic" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8c3d791 --- /dev/null +++ b/src/main.rs @@ -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); +}