Skip to content

Commit

Permalink
use chosen heuristic
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilhenry committed Nov 14, 2023
1 parent d534da6 commit 5af3f40
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ use std::collections::{BinaryHeap, HashMap};
use std::rc::Rc;
use std::time::{Duration, Instant};

Check warning on line 13 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Check wasm32

unused import: `Instant`

#[derive(Debug, PartialEq)]
pub enum Heuristic {
Manhattan,
Diagonal,
Euclidean,
}

pub struct Grid {
height: usize,
width: usize,
Expand Down Expand Up @@ -163,7 +170,7 @@ impl Grid {
self.path = Some(path_pos);
}

pub fn solve(&mut self) {
pub fn solve(&mut self, heuristic: &Heuristic) {
let start_pos = self.start.clone();
let Some(start_pos) = start_pos else {
panic!("no start position");
Expand All @@ -173,14 +180,20 @@ impl Grid {
panic!("no goal position")
};

let heuristic: fn(&Position, &Position) -> usize = match heuristic {
Heuristic::Manhattan => position::manhattan_distance,
Heuristic::Euclidean => position::euclidean_distance,
Heuristic::Diagonal => position::diagonal_distance,
};

let goal = self.get_index_from_pos(&goal_pos);

let mut open_set = BinaryHeap::new();
let start_node = self
.nodes
.get(&start_pos)
.expect("must be valid start position");
let start_h_cost = position::euclid_distance(&start_pos, &goal_pos);
let start_h_cost = heuristic(&start_pos, &goal_pos);
start_node.borrow_mut().h_cost = start_h_cost;
start_node.borrow_mut().g_cost = 0;
start_node.borrow_mut().f_cost = start_h_cost;
Expand Down Expand Up @@ -224,7 +237,7 @@ impl Grid {
continue; // this way would have been a worse path
}
let g_cost = temp_g_cost;
let h_cost = position::euclid_distance(&pos, &goal_pos);
let h_cost = heuristic(&pos, &goal_pos);
let f_cost = g_cost + h_cost;
neighbour.borrow_mut().g_cost = g_cost;
neighbour.borrow_mut().h_cost = h_cost;
Expand Down
11 changes: 2 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use eframe::egui::{Context, Sense};
use eframe::{egui, Frame};
use path_finding::frame_history::FrameHistory;
use path_finding::node::{Node, NodeType};
use path_finding::Grid;
use path_finding::{Grid, Heuristic};

#[cfg(target_arch = "wasm32")]
fn main() {
Expand Down Expand Up @@ -45,13 +45,6 @@ enum CursorType {
Start,
}

#[derive(Debug, PartialEq)]
enum Heuristic {
Manhattan,
Diagonal,
Euclidean,
}

trait NodeColor {
fn get_color(&self) -> egui::Color32;
}
Expand Down Expand Up @@ -171,7 +164,7 @@ impl eframe::App for MyApp {
ui.add_space(WIDGET_SPACING);
ui.add_enabled_ui(self.grid.is_ready(), |ui| {
if ui.button("Find Path").clicked() {
self.grid.solve();
self.grid.solve(&self.heuristic);
}
});
ui.separator();
Expand Down
15 changes: 14 additions & 1 deletion src/position.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp;
use std::hash::Hash;
use std::ops::Add;

Expand All @@ -6,12 +7,24 @@ pub struct Position {
pub x: i32,
pub y: i32,
}
pub fn euclid_distance(from: &Position, to: &Position) -> usize {
pub fn euclidean_distance(from: &Position, to: &Position) -> usize {
let x_dist = (from.x - to.x).pow(2);
let y_dist = (from.y - to.y).pow(2);
(f32::sqrt((x_dist + y_dist) as f32) * 10.0) as usize
}

pub fn manhattan_distance(from: &Position, to: &Position) -> usize {
let x_dist = (from.x - to.x).abs();
let y_dist = (from.y - to.y).abs();
(x_dist + y_dist) as usize
}

pub fn diagonal_distance(from: &Position, to: &Position) -> usize {
let x_dist = (from.x - to.x).abs();
let y_dist = (from.y - to.y).abs();
cmp::max(x_dist, y_dist) as usize
}

impl Position {
pub const fn new(x: i32, y: i32) -> Self {
Position { x, y }
Expand Down

0 comments on commit 5af3f40

Please sign in to comment.