summaryrefslogtreecommitdiffstats
path: root/walleng/src/vec2d.rs
blob: 1759d32c069c54e9accd27d975f527e1f3df2f02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// -*- coding: utf-8 -*-
//
// Copyright 2021 Michael Büsch <m@bues.ch>
//
// Derived from https://github.com/mlochen/dungeon.git
// Copyright (C) 2020 Marco Lochen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

use std::ops::{Add, Mul, Sub};

#[derive(Copy, Clone, Debug)]
pub struct Vec2D {
    x: f32,
    y: f32,
}

impl Vec2D {
    /// Create a new vector.
    #[inline]
    pub fn new(x: f32, y: f32) -> Vec2D {
        Vec2D { x, y }
    }

    /// Get the X coordinate.
    #[inline]
    pub fn x(&self) -> f32 {
        self.x
    }

    /// Set the X coordinate.
    #[inline]
    pub fn set_x(&mut self, x: f32) {
        self.x = x;
    }

    /// Get the Y coordinate.
    #[inline]
    pub fn y(&self) -> f32 {
        self.y
    }

    /// Set the Y coordinate.
    #[inline]
    pub fn set_y(&mut self, y: f32) {
        self.y = y;
    }

    /// Get the coordinates.
    #[inline]
    pub fn get(&self) -> (f32, f32) {
        (self.x, self.y)
    }

    /// Set the coordinates.
    #[inline]
    pub fn set(&mut self, coord: (f32, f32)) {
        self.x = coord.0;
        self.y = coord.1;
    }

    /// Get the length of self.
    #[inline]
    pub fn len(&self) -> f32 {
        ((self.x * self.x) + (self.y * self.y)).sqrt()
    }

    /// Get a normalized version of self.
    #[inline]
    pub fn normalized(&self) -> Vec2D {
        let len = self.len();
        if len != 0.0 {
            Vec2D::new(self.x / len, self.y / len)
        } else {
            Default::default()
        }
    }

    /// Get the dot product of self and other.
    #[inline]
    pub fn dot(&self, other: &Vec2D) -> f32 {
        (self.x * other.x) + (self.y * other.y)
    }

    /// Get the angle of self w.r.t. the coordinate system abscissa.
    #[inline]
    pub fn phi(&self) -> f32 {
        if self.x != 0.0 {
            if self.x > 0.0 {
                (self.y / self.x).atan()
            } else {
                (self.y / self.x).atan() + std::f32::consts::PI
            }
        } else {
            std::f32::consts::FRAC_PI_2
        }
    }

    /// Get angle between self and other.
    #[inline]
    pub fn phi_to(&self, other: &Vec2D) -> f32 {
        (self.dot(other) / (self.len() * other.len())).acos()
    }

    /// Check if the angle between self and other is positive.
    #[inline]
    pub fn phi_to_is_positive(&self, other: &Vec2D) -> bool {
        ((self.x * other.y) - (self.y * other.x)) >= 0.0
    }

    /// Check if the angle between self and other is negative.
    #[inline]
    pub fn phi_to_is_negative(&self, other: &Vec2D) -> bool {
        !self.phi_to_is_positive(other)
    }

    /// Get self projected onto other.
    #[inline]
    pub fn projected(&self, other: &Vec2D) -> Vec2D {
        let dotother = other.dot(other);
        if dotother != 0.0 {
            *other * (self.dot(other) / dotother)
        } else {
            Default::default()
        }
    }

    /// Get self rotated by angle phi.
    #[inline]
    pub fn rotated(&self, phi: f32) -> Vec2D {
        let (sinphi, cosphi) = phi.sin_cos();
        Vec2D::new(
            (cosphi * self.x) - (sinphi * self.y),
            (sinphi * self.x) + (cosphi * self.y),
        )
    }
}

impl Default for Vec2D {
    /// Get a zero vector.
    #[inline]
    fn default() -> Self {
        Self::new(0.0, 0.0)
    }
}

impl Mul<f32> for Vec2D {
    type Output = Vec2D;

    /// Multiply the vector self with a scalar.
    #[inline]
    fn mul(self, rhs: f32) -> Self::Output {
        Vec2D {
            x: self.x * rhs,
            y: self.y * rhs,
        }
    }
}

impl Add for Vec2D {
    type Output = Vec2D;

    /// Add two vectors.
    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        Vec2D {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}

impl Sub for Vec2D {
    type Output = Vec2D;

    /// Subtract rhs from self.
    #[inline]
    fn sub(self, rhs: Self) -> Self::Output {
        Vec2D {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::util::radians;
    use float_eq::assert_float_eq;

    #[test]
    fn test_add() {
        let a = Vec2D::new(1.0, 2.0) + Vec2D::new(10.0, -20.0);
        assert_float_eq!(a.x(), 11.0, abs <= 1e-6);
        assert_float_eq!(a.y(), -18.0, abs <= 1e-6);
    }

    #[test]
    fn test_sub() {
        let a = Vec2D::new(1.0, 2.0) - Vec2D::new(10.0, -20.0);
        assert_float_eq!(a.x(), -9.0, abs <= 1e-6);
        assert_float_eq!(a.y(), 22.0, abs <= 1e-6);
    }

    #[test]
    fn test_mul() {
        let a = Vec2D::new(1.0, -2.0) * 10.0;
        assert_float_eq!(a.x(), 10.0, abs <= 1e-6);
        assert_float_eq!(a.y(), -20.0, abs <= 1e-6);
    }

    #[test]
    fn test_len() {
        let a = Vec2D::new(1.5, 2.2).len();
        assert_float_eq!(a, 2.662705, abs <= 1e-6);
    }

    #[test]
    fn test_normalized() {
        let a = Vec2D::new(1.5, 2.2).normalized();
        assert_float_eq!(a.x(), 0.5633368, abs <= 1e-6);
        assert_float_eq!(a.y(), 0.8262273, abs <= 1e-6);
    }

    #[test]
    fn test_dot() {
        let a = Vec2D::new(1.5, 2.2).dot(&Vec2D::new(10.0, 11.0));
        assert_float_eq!(a, 39.2, abs <= 1e-6);
    }

    #[test]
    fn test_phi() {
        let a = Vec2D::new(1.5, 2.2).phi();
        assert_float_eq!(a, 0.9723774, abs <= 1e-6);
        let a = Vec2D::new(0.0, 1.23).phi();
        assert_float_eq!(a, std::f32::consts::FRAC_PI_2, abs <= 1e-6);
        let a = Vec2D::new(-3.3, 3.45).phi();
        assert_float_eq!(a, 2.333976, abs <= 1e-6);
        let a = Vec2D::new(4.5, -2.1).phi();
        assert_float_eq!(a, -0.4366271, abs <= 1e-6);
        let a = Vec2D::new(-8.4, -9.1).phi();
        assert_float_eq!(a, 3.96697, abs <= 1e-6);
    }

    #[test]
    fn test_phi_to() {
        let a = Vec2D::new(1.0, 0.0).phi_to(&Vec2D::new(1.0, 0.0));
        assert_float_eq!(a, 0.0, abs <= 1e-6);
        let a = Vec2D::new(1.0, 0.0).phi_to(&Vec2D::new(1.0, 1.0));
        assert_float_eq!(a, std::f32::consts::FRAC_PI_4, abs <= 1e-6);
        let a = Vec2D::new(1.0, 0.0).phi_to(&Vec2D::new(0.0, 1.0));
        assert_float_eq!(a, std::f32::consts::FRAC_PI_2, abs <= 1e-6);
        let a = Vec2D::new(1.0, 0.0).phi_to(&Vec2D::new(-1.0, 1.0));
        assert_float_eq!(a, std::f32::consts::FRAC_PI_4 * 3.0, abs <= 1e-6);
        let a = Vec2D::new(1.0, 0.0).phi_to(&Vec2D::new(-1.0, 0.0));
        assert_float_eq!(a, std::f32::consts::PI, abs <= 1e-6);
        let a = Vec2D::new(1.0, 0.0).phi_to(&Vec2D::new(-1.0, -1.0));
        assert_float_eq!(a, std::f32::consts::FRAC_PI_4 * 3.0, abs <= 1e-6);
        let a = Vec2D::new(1.0, 0.0).phi_to(&Vec2D::new(0.0, -1.0));
        assert_float_eq!(a, std::f32::consts::FRAC_PI_2, abs <= 1e-6);
        let a = Vec2D::new(1.0, 0.0).phi_to(&Vec2D::new(1.0, -1.0));
        assert_float_eq!(a, std::f32::consts::FRAC_PI_4, abs <= 1e-6);

        let a = Vec2D::new(1.5, 2.2).phi_to(&Vec2D::new(10.0, 11.0));
        assert_float_eq!(a, 0.1393962, abs <= 1e-6);

        for i in 0..=360 {
            let a = Vec2D::new(1.0, 0.0);
            assert!(a.phi_to(&a.rotated(radians(i as f32))) >= 0.0);
            assert!(a.phi_to(&a.rotated(radians((-i) as f32))) >= 0.0);
        }
    }

    #[test]
    fn test_phi_to_is_positive() {
        let a = Vec2D::new(1.0, 0.0);
        assert!(a.phi_to_is_positive(&a) == true);
        assert!(a.phi_to_is_positive(&a.rotated(radians(45.0))) == true);
        assert!(a.phi_to_is_positive(&a.rotated(radians(90.0))) == true);
        assert!(a.phi_to_is_positive(&a.rotated(radians(135.0))) == true);
        assert!(a.phi_to_is_positive(&Vec2D::new(-1.0, 0.0)) == true);
        assert!(a.phi_to_is_positive(&a.rotated(radians(225.0))) == false);
        assert!(a.phi_to_is_positive(&a.rotated(radians(270.0))) == false);
        assert!(a.phi_to_is_positive(&a.rotated(radians(315.0))) == false);
    }

    #[test]
    fn test_phi_to_is_negative() {
        let a = Vec2D::new(1.0, 0.0);
        assert!(a.phi_to_is_negative(&a) == false);
        assert!(a.phi_to_is_negative(&a.rotated(radians(45.0))) == false);
        assert!(a.phi_to_is_negative(&a.rotated(radians(90.0))) == false);
        assert!(a.phi_to_is_negative(&a.rotated(radians(135.0))) == false);
        assert!(a.phi_to_is_negative(&Vec2D::new(-1.0, 0.0)) == false);
        assert!(a.phi_to_is_negative(&a.rotated(radians(225.0))) == true);
        assert!(a.phi_to_is_negative(&a.rotated(radians(270.0))) == true);
        assert!(a.phi_to_is_negative(&a.rotated(radians(315.0))) == true);
    }

    #[test]
    fn test_rotated() {
        let a = Vec2D::new(1.5, 2.2).rotated(radians(45.0));
        assert_float_eq!(a.x(), -0.4949747, abs <= 1e-6);
        assert_float_eq!(a.y(), 2.616295, abs <= 1e-6);
    }

    #[test]
    fn test_proj() {
        let a = Vec2D::new(1.5, 2.2).projected(&Vec2D::new(10.0, 11.0));
        assert_float_eq!(a.x(), 1.773756, abs <= 1e-6);
        assert_float_eq!(a.y(), 1.951131, abs <= 1e-6);
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface