aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/MathTools.hx
blob: 087748a76abbe9fff822e25bf5cc0b271d1d53f6 (plain) (blame)
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
package tools;

class MathTools {
	public static inline function clamp<T:Float>(v:T, min:T, max:T):T {
		return v < min ? min : v > max ? max : v;
	}

	public static inline function lerp(ratio:Float, a:Float, b:Float):Float {
		return a + ratio * (b - a);
	}

	public static inline function sign(v:Float):Int {
		if (v == 0) return 0;
		return v < 0 ? -1 : 1;
	}

	public static inline function abs<T:Float>(v:T):T {
		return cast Math.abs(v);
	}

	public static inline function pow<T:Float>(v:T, exp:T):T {
		return cast Math.pow(v, exp);
	}

	public static inline function limitMin<T:Float>(a:T, b:T):T {
		return a < b ? b : a;
	}

	public static inline function limitMax<T:Float>(a:T, b:T):T {
		return a > b ? b : a;
	}

	public static inline function wrapAround<T:Float>(v:T, min:T, max:T):T {
		if (min == max) return min;
		final range = max - min + 1;
		return min + (((v - min) % range) + range) % range;
	}

	public static function toFixed(v:Float, digits = 2):Float {
		if (digits > 8) throw 'digits is $digits, but cannot be bigger than 8 (for value $v)';
		final ratio = Math.pow(10, digits);
		return Std.int(v * ratio) / ratio;
	}

	public static function toBitString(value:Int):String {
		var result = "";
		var mask = 1;
		for (i in 0...32) { // 32-bit integer
			result = (value & mask != 0 ? "1" : "0") + result;
			mask <<= 1;
		}
		final i = result.indexOf("1");
		return i > 0 ? result.substr(i) : result;
	}
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage