maplibre/legacy/util/
math.rs1use std::f64::consts::PI;
5
6use crate::euclid::{Point2D, Vector2D};
7
8pub 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
17pub fn deg2radf(deg: f64) -> f64 {
25 deg * PI / 180.0
26}
27
28pub fn perp<U>(a: &Vector2D<f64, U>) -> Vector2D<f64, U> {
30 Vector2D::new(-a.y, a.x)
31}
32
33pub trait MinMax<T> {
34 fn max_value(self) -> T;
36 fn min_value(self) -> T;
38}
39
40impl MinMax<f64> for [f64; 4] {
41 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 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
58pub 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
63pub fn convert_point_i16<U>(point: &Point2D<f64, U>) -> Point2D<i16, U> {
65 Point2D::new(point.x as i16, point.y as i16)
66}