-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversions.df
48 lines (40 loc) · 1.85 KB
/
Conversions.df
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
class Conversions
function Linear(range_from, range_to, value_from)
// Converts/scales a value from one range to another in a linear fashion.
// Note that this is not suitable for converting/scaling delta values.
//
// Args:
// - range_from: An array comprising minimum and maximum values from which to
// convert a value.
// - range_to: An array comprising minimum and maximum values toward which to
// convert a value.
// - value_from: A value to convert/scale from one range to another.
//
// Returns:
// - Supplied value converted/scaled to its target range.
private delta_range_from = Max(range_from) - Min(range_from)
private delta_range_to = Max(range_to) - Min(range_to)
private derivative = delta_range_to / delta_range_from
private offset = derivative * Min(range_from) - Min(range_to)
return derivative * value_from - offset
endfunction
function LinearDeltaConversion(range_from, range_to, value_from)
// Converts/scales a delta value from one range to another in a linear
// fashion.
//
// Args:
// - range_from: An array comprising minimum and maximum values from which to
// convert a delta value.
// - range_to: An array comprising minimum and maximum values toward which to
// convert a delta value.
// - value_from: A delta value to convert/scale from one range to another.
//
// Returns:
// - Supplied delta value converted/scaled to its target range.
private delta_range_from = Max(range_from) - Min(range_from)
private delta_range_to = Max(range_to) - Min(range_to)
private derivative = delta_range_to / delta_range_from
return derivative * value_from
endfunction
endclass
global Conversions = new(Conversions)