button_controller/
math.rs

1//! Various useful methods for math.
2
3use std::time::Duration;
4
5use vecmath;
6use vecmath::mat2x3_inv as inv;
7use vecmath::row_mat2x3_transform_pos2 as transform_pos;
8
9/// The type used for scalars.
10pub type Scalar = f64;
11
12/// The type used for matrices.
13pub type Matrix2d<T = Scalar> = vecmath::Matrix2x3<T>;
14
15/// Rectangle dimensions: [x, y, w, h]
16pub type Rectangle<T = Scalar> = [T; 4];
17
18/// The type used for 2D vectors.
19pub type Vec2d<T = Scalar> = vecmath::Vector2<T>;
20
21/// Returns true if transformed point is inside rectangle.
22pub fn is_inside(pos: Vec2d, transform: Matrix2d, rect: Rectangle) -> bool {
23    let inv = inv(transform);
24    let pos = transform_pos(inv, pos);
25    pos[0] >= rect[0] && pos[1] >= rect[1] && pos[0] < rect[0] + rect[2] &&
26    pos[1] < rect[1] + rect[3]
27}
28
29/// Returns the number of seconds of duration.
30pub fn duration_to_secs(duration: &Duration) -> f64 {
31    duration.as_secs() as f64 * 1.0e9 + duration.subsec_nanos() as f64 / 1.0e9
32}