pub const RANGE_MARGIN: f32 = 1.25;
pub fn raised_light(intensity: f32, reach: f32, over: f32) -> (f32, f32) {
let reach = reach.max(1.0);
let slant = reach.hypot(over.max(0.0));
(intensity * (slant / reach).powi(2), slant * RANGE_MARGIN)
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
mod tests {
use super::*;
#[test]
fn a_raised_light_is_brightened_by_its_slant_and_reaches_past_it() {
let (intensity, range) = raised_light(1000.0, 30.0, 40.0);
assert!((intensity - 1000.0 * (50.0f32 / 30.0).powi(2)).abs() < 1e-3);
assert!((range - 62.5).abs() < 1e-5);
}
#[test]
fn a_degenerate_reach_or_a_sunken_light_still_answers() {
let (on_the_ground, range) = raised_light(500.0, 0.0, 3.0);
assert!(
(on_the_ground - 500.0 * 10.0).abs() < 1e-2,
"reach clamps to one world unit: {on_the_ground}"
);
assert!((range - 1.25 * 10.0f32.sqrt()).abs() < 1e-4);
let (below, _) = raised_light(500.0, 10.0, -4.0);
assert!((below - 500.0).abs() < 1e-3, "over clamps at 0: {below}");
}
}