make find_best_height return an option

This commit is contained in:
Robin Appelman 2025-05-30 20:38:41 +02:00
commit 7d25dc6235
2 changed files with 6 additions and 11 deletions

View file

@ -17,7 +17,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut file = BufReader::new(File::open("data/pl_badwater.nav")?);
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(())
}

View file

@ -109,16 +109,11 @@ impl NavQuadTree {
/// Get the z height at a point
///
/// 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 {
let found_heights = self.find_z_height(x, y);
let best_z = f32::MIN;
found_heights.fold(best_z, |best_z, found_z| {
if (found_z - z_guess).abs() < (best_z - z_guess).abs() {
found_z
} else {
best_z
}
pub fn find_best_height(&self, x: f32, y: f32, z_guess: f32) -> Option<f32> {
self.find_z_height(x, y).min_by(|a, b| {
let a_diff = (a - z_guess).abs();
let b_diff = (b - z_guess).abs();
a_diff.total_cmp(&b_diff)
})
}