/
lerp.go
92 lines (78 loc) · 2.14 KB
/
lerp.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package util
// Linear interpolation.
// Lerp returns the value (1-t)*start + t*end.
func Lerp(t, start, end float64) float64 {
return (1-t)*start + t*end
}
// LerpClamp is a clamped [0,1] version of Lerp.
func LerpClamp(t, start, end float64) float64 {
if t < 0 {
t = 0
}
if t > 1 {
t = 1
}
return (1-t)*start + t*end
}
// InvLerp performs the inverse of Lerp and returns the value of t for a value v.
func InvLerp(v, start, end float64) float64 {
return (v - start) / (end - start)
}
// InvLerpClamp is a clamped [start, end] version of InvLerp.
func InvLerpClamp(v, start, end float64) float64 {
t := (v - start) / (end - start)
if t < 0 {
return 0
}
if t > 1 {
return 1
}
return t
}
// Remap converts v from one space to another by applying InvLerp to find t in the initial range, and
// then using t to find v' in the new range.
func Remap(v, istart, iend, ostart, oend float64) float64 {
return Lerp(InvLerp(v, istart, iend), ostart, oend)
}
// RemapClamp is a clamped version of Remap.
func RemapClamp(v, istart, iend, ostart, oend float64) float64 {
return LerpClamp(InvLerpClamp(v, istart, iend), ostart, oend)
}
// Float32 versions for Path and x/image/vector
// Lerp32 is a float32 version of Lerp.
func Lerp32(t, start, end float32) float32 {
return (1-t)*start + t*end
}
// LerpClamp32 is a float32 version of LerpClamp.
func LerpClamp32(t, start, end float32) float32 {
if t < 0 {
t = 0
}
if t > 1 {
t = 1
}
return (1-t)*start + t*end
}
// InvLerp32 is a float32 version of InvLerp.
func InvLerp32(v, start, end float32) float32 {
return (v - start) / (end - start)
}
// InvLerpClamp32 is a float32 version of InvLerpClamp.
func InvLerpClamp32(v, start, end float32) float32 {
t := (v - start) / (end - start)
if t < 0 {
return 0
}
if t > 1 {
return 1
}
return t
}
// Remap32 is a float32 version of Remap.
func Remap32(v, istart, iend, ostart, oend float32) float32 {
return Lerp32(InvLerp32(v, istart, iend), ostart, oend)
}
// RemapClamp32 is a float32 version of RemapClamp.
func RemapClamp32(v, istart, iend, ostart, oend float32) float32 {
return LerpClamp32(InvLerpClamp32(v, istart, iend), ostart, oend)
}