1 /**
2  * Miscelaneous math functions and definitions.
3  */
4 module bettercmath.misc;
5 
6 import std.math : PI;
7 import std.traits : isFloatingPoint, isNumeric;
8 
9 /// Templated alias for a floating point type correspondent with `T`.
10 template FloatType(T)
11 if (isNumeric!T)
12 {
13     static if (isFloatingPoint!T)
14     {
15         alias FloatType = T;
16     }
17     else
18     {
19         alias FloatType = float;
20     }
21 }
22 
23 /// Convert angle from degrees to radians.
24 FloatType!T degreesToRadians(T)(const T degrees)
25 {
26     return degrees * (PI / 180.0);
27 }
28 alias deg2rad = degreesToRadians;  /// ditto
29 
30 /// Convert angle from radias to degrees.
31 FloatType!T radiansToDegrees(T)(const T radians)
32 {
33     return radians * (180.0 / PI);
34 }
35 alias rad2deg = radiansToDegrees;  /// ditto
36 
37 /// Linearly interpolates values `from` and `to` by `amount`.
38 T lerp(T, U)(const T from, const T to, const U amount)
39 {
40     return cast(T) (from + amount * (to - from));
41 }
42 /// Linearly interpolates the values from `fromTo` by `amount`.
43 T lerp(T, U)(const T[2] fromTo, const U amount)
44 {
45     return lerp(fromTo[0], fromTo[1], amount);
46 }