sailboat_physics/
lib.rs

1//! All angles in radians with 0 pointing North
2//! 
3
4pub mod goemetry;
5pub mod aerodynamics;
6
7use goemetry::Vec2d;
8use std::f32::consts::PI;
9
10
11/// Wind vectors have x coordinate pointing North
12pub struct Wind {
13    pub velocity: Vec2d,
14}
15
16pub struct Sail {
17}
18
19pub struct Sailboat {
20}
21
22pub struct SailboatState {
23}
24
25
26impl Wind {
27    pub fn new(velocity: Vec2d) -> Wind {
28        Wind {velocity}
29    }
30
31    /// Wind direction in degres 0ᵒ = North, 90ᵒ = East
32    pub fn direction(&self) -> f32 {
33        let phi =  self.velocity.phi();
34        let alpha = phi / PI * 180.0;
35        alpha
36    }
37
38    pub fn speed(&self) -> f32 {
39        self.velocity.r()
40    }
41}
42
43impl Sailboat {
44    pub fn new() -> Sailboat {
45        Sailboat { }
46    }
47}
48
49
50pub fn apparent_wind(boat_velocity: &Vec2d, wind: &Vec2d) -> Vec2d {
51    boat_velocity.neg().add(wind)
52}
53
54// ----------------------------------------------------------------------------
55#[cfg(test)]
56mod tests {
57    use crate::*;
58    use approx::assert_abs_diff_eq;
59
60    #[test]
61    fn wind_dir() {
62        assert_abs_diff_eq!(Wind::new(Vec2d::new(1., 0.)).direction(), 0.0);
63        assert_abs_diff_eq!(Wind::new(Vec2d::new(3., 3.)).direction(), 45.0);
64        assert_abs_diff_eq!(Wind::new(Vec2d::new(-3., -3.)).direction(), -135.0);
65    }
66}