/
FMath.hx
72 lines (60 loc) · 2.1 KB
/
FMath.hx
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
//
// Flambe - Rapid game development
// https://github.com/aduros/flambe/blob/master/LICENSE.txt
package flambe.math;
/**
* Some handy math functions, and inlinable constants.
*/
class FMath
{
public static inline var E = 2.718281828459045;
public static inline var LN2 = 0.6931471805599453;
public static inline var LN10 = 2.302585092994046;
public static inline var LOG2E = 1.4426950408889634;
public static inline var LOG10E = 0.43429448190325176;
public static inline var PI = 3.141592653589793;
public static inline var SQRT1_2 = 0.7071067811865476;
public static inline var SQRT2 = 1.4142135623730951;
// Haxe doesn't specify the size of an int or float, in practice it's 32 bits
/** The lowest integer value in Flash and JS. */
public static inline var INT_MIN :Int = -2147483648;
/** The highest integer value in Flash and JS. */
public static inline var INT_MAX :Int = 2147483647;
/** The lowest float value in Flash and JS. */
public static inline var FLOAT_MIN = -1.79769313486231e+308;
/** The highest float value in Flash and JS. */
public static inline var FLOAT_MAX = 1.79769313486231e+308;
/** Converts an angle in degrees to radians. */
inline public static function toRadians (degrees :Float) :Float
{
return degrees * PI/180;
}
/** Converts an angle in radians to degrees. */
inline public static function toDegrees (radians :Float) :Float
{
return radians * 180/PI;
}
#if !js @:generic #end
inline public static function max<T:Float> (a :T, b :T) :T
{
return (a > b) ? a : b;
}
#if !js @:generic #end
inline public static function min<T:Float> (a :T, b :T) :T
{
return (a < b) ? a : b;
}
#if !js @:generic #end
public static function clamp<T:Float> (value :T, min :T, max :T) :T
{
return if (value < min) min
else if (value > max) max
else value;
}
public static function sign (value :Float) :Int
{
return if (value < 0) -1
else if (value > 0) 1
else 0;
}
}