1 /**
2 Grimoire
3 Copyright (c) 2017 Enalye
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising
7 from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute
11 it freely, subject to the following restrictions:
12 
13 	1. The origin of this software must not be misrepresented;
14 	   you must not claim that you wrote the original software.
15 	   If you use this software in a product, an acknowledgment
16 	   in the product documentation would be appreciated but
17 	   is not required.
18 
19 	2. Altered source versions must be plainly marked as such,
20 	   and must not be misrepresented as being the original software.
21 
22 	3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 module core.util;
26 
27 public import std.math;
28 
29 import core.vec2;
30 
31 enum sqrt2_2 = std.math.sqrt(2.0) / 2.0;
32 enum pi2 = PI * 2f;
33 
34 T lerp(T)(T a, T b, float t) {
35 	return t * b + (1f - t) * a;
36 }
37 
38 float rlerp(float a, float b, float v) {
39 	return (v - a) / (b - a);
40 }
41 
42 float angleBetween(float a, float b) {
43 	float delta = (b - a) % 360f;
44 	return ((2f * delta) % 360f) - delta;
45 }
46 
47 float angleLerp(float a, float b, float t) {
48 	return a + angleBetween(a, b) * t;
49 }
50 
51 Vec2f scaleToFit(Vec2f src, Vec2f dst) {
52 	float scale;
53 	if(dst.x / dst.y > src.x / src.y)
54 		scale = dst.y / src.y;
55 	else
56 		scale = dst.x / src.x;
57 	return src * scale;
58 }