maplibre/legacy/util/
math.rs

1//! Translated functions from https://github.com/maplibre/maplibre-native/blob/4add9ea/
2//! Likely to be replaced by more generic functions.
3
4use std::f64::consts::PI;
5
6use crate::euclid::{Point2D, Vector2D};
7
8/// maplibre/maplibre-native#4add9ea original name: rotate
9pub fn rotate<U>(a: &Vector2D<f64, U>, angle: f64) -> Vector2D<f64, U> {
10    let cos = angle.cos();
11    let sin = angle.sin();
12    let x = cos * a.x - sin * a.y;
13    let y = sin * a.x + cos * a.y;
14    Vector2D::new(x, y)
15}
16
17/**
18 * @brief Converts degrees to radians
19 *
20 * @param deg Degrees as float.
21 * @return Radians as float.
22 */
23/// maplibre/maplibre-native#4add9ea original name: deg2radf
24pub fn deg2radf(deg: f64) -> f64 {
25    deg * PI / 180.0
26}
27
28/// maplibre/maplibre-native#4add9ea original name: perp
29pub fn perp<U>(a: &Vector2D<f64, U>) -> Vector2D<f64, U> {
30    Vector2D::new(-a.y, a.x)
31}
32
33pub trait MinMax<T> {
34    /// maplibre/maplibre-native#4add9ea original name: max_value
35    fn max_value(self) -> T;
36    /// maplibre/maplibre-native#4add9ea original name: min_value
37    fn min_value(self) -> T;
38}
39
40impl MinMax<f64> for [f64; 4] {
41    /// maplibre/maplibre-native#4add9ea original name: max_value
42    fn max_value(self) -> f64 {
43        *self
44            .iter()
45            .max_by(|a, b| a.total_cmp(b))
46            .expect("array is not empty")
47    }
48
49    /// maplibre/maplibre-native#4add9ea original name: min_value
50    fn min_value(self) -> f64 {
51        *self
52            .iter()
53            .min_by(|a, b| a.total_cmp(b))
54            .expect("array is not empty")
55    }
56}
57
58/// maplibre/maplibre-native#4add9ea original name: convert_point_f64
59pub fn convert_point_f64<U>(point: &Point2D<i16, U>) -> Point2D<f64, U> {
60    Point2D::new(point.x as f64, point.y as f64)
61}
62
63/// maplibre/maplibre-native#4add9ea original name: convert_point_i16
64pub fn convert_point_i16<U>(point: &Point2D<f64, U>) -> Point2D<i16, U> {
65    Point2D::new(point.x as i16, point.y as i16)
66}