mirror of
https://codeberg.org/icewind/sourcenav.git
synced 2026-06-03 10:14:11 +02:00
make find_best_height return an option
This commit is contained in:
parent
24a4124e04
commit
7d25dc6235
2 changed files with 6 additions and 11 deletions
|
|
@ -17,7 +17,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut file = BufReader::new(File::open("data/pl_badwater.nav")?);
|
let mut file = BufReader::new(File::open("data/pl_badwater.nav")?);
|
||||||
let tree = get_quad_tree(&mut file)?;
|
let tree = get_quad_tree(&mut file)?;
|
||||||
|
|
||||||
assert_eq!(220.83125, tree.find_best_height(320.0, -1030.0, 0.0));
|
assert_eq!(Some(220.83125), tree.find_best_height(320.0, -1030.0, 0.0));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
src/lib.rs
15
src/lib.rs
|
|
@ -109,16 +109,11 @@ impl NavQuadTree {
|
||||||
/// Get the z height at a point
|
/// Get the z height at a point
|
||||||
///
|
///
|
||||||
/// A z-guess should be provided to resolve cases where multiple z values are possible
|
/// A z-guess should be provided to resolve cases where multiple z values are possible
|
||||||
pub fn find_best_height(&self, x: f32, y: f32, z_guess: f32) -> f32 {
|
pub fn find_best_height(&self, x: f32, y: f32, z_guess: f32) -> Option<f32> {
|
||||||
let found_heights = self.find_z_height(x, y);
|
self.find_z_height(x, y).min_by(|a, b| {
|
||||||
let best_z = f32::MIN;
|
let a_diff = (a - z_guess).abs();
|
||||||
|
let b_diff = (b - z_guess).abs();
|
||||||
found_heights.fold(best_z, |best_z, found_z| {
|
a_diff.total_cmp(&b_diff)
|
||||||
if (found_z - z_guess).abs() < (best_z - z_guess).abs() {
|
|
||||||
found_z
|
|
||||||
} else {
|
|
||||||
best_z
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue