roast2d_internal 0.4.0

Roast2D internal crate
Documentation
use std::f32::consts::PI;

use glam::Vec2;

/// Round a number to the nearest even number
pub fn round_to_even(v: Vec2) -> Vec2 {
    (v * 0.5).round() * 2.0
}

/// Rotate angle in radian
pub fn rot(mut ang: f32, dt: f32) -> f32 {
    ang += dt;
    while ang > PI {
        ang -= 2.0 * PI;
    }
    while ang < -PI {
        ang += 2.0 * PI;
    }
    ang
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_round_to_even() {
        // Basic rounding
        assert_eq!(round_to_even(Vec2::new(1.0, 1.0)), Vec2::new(2.0, 2.0));
        assert_eq!(round_to_even(Vec2::new(2.0, 2.0)), Vec2::new(2.0, 2.0));
        assert_eq!(round_to_even(Vec2::new(3.0, 3.0)), Vec2::new(4.0, 4.0));

        // Fractional values
        assert_eq!(round_to_even(Vec2::new(1.5, 2.5)), Vec2::new(2.0, 2.0));
        assert_eq!(round_to_even(Vec2::new(0.5, 0.5)), Vec2::new(0.0, 0.0));

        // Negative values
        assert_eq!(round_to_even(Vec2::new(-1.0, -1.0)), Vec2::new(-2.0, -2.0));
    }

    #[test]
    fn test_rot_no_wrap() {
        // Simple rotation within bounds
        let result = rot(0.0, 0.5);
        assert!((result - 0.5).abs() < 1e-6);

        let result = rot(0.0, -0.5);
        assert!((result - (-0.5)).abs() < 1e-6);
    }

    #[test]
    fn test_rot_wrap_positive() {
        // Wrap around positive (3π → π)
        let result = rot(0.0, 3.0 * PI);
        assert!((result - PI).abs() < 1e-5);

        // 4π → 0
        let result = rot(0.0, 4.0 * PI);
        assert!(result.abs() < 1e-5);
    }

    #[test]
    fn test_rot_wrap_negative() {
        // Wrap around negative (-3π → -π)
        let result = rot(0.0, -3.0 * PI);
        assert!((result - (-PI)).abs() < 1e-5);

        // -4π → 0
        let result = rot(0.0, -4.0 * PI);
        assert!(result.abs() < 1e-5);
    }

    #[test]
    fn test_rot_boundary() {
        // At exactly PI
        let result = rot(0.0, PI);
        assert!((result - PI).abs() < 1e-6 || (result - (-PI)).abs() < 1e-6);

        // At exactly -PI
        let result = rot(0.0, -PI);
        assert!((result - PI).abs() < 1e-6 || (result - (-PI)).abs() < 1e-6);
    }
}