CabinGame/Luxe/math/math.wren

59 lines
1.6 KiB
Text
Raw Normal View History

2020-08-29 19:25:16 +00:00
import "math/vector" for Vector
import "luxe: math" for Math
import "math/repNum" for RepNum
import "math/Util" for Util
2020-08-16 13:59:43 +00:00
class M{
static inv_lerp(from, to, inter){
return (inter - from) / (to - from)
}
static lerp(from, to, value){
2020-08-29 19:25:16 +00:00
if(from is Num && to is Num) return from + (to - from) * value
if(from is Sequence != to is Sequence) {
if(from is Sequence){
to = RepNum.new(to, from.count)
} else {
from = RepNum.new(from, to.count)
}
}
if(value is Num) value = RepNum.new(value)
System.print("%(from.toList) %(to.toList)")
var result = []
Util.for_all([from, to, value]) { |from, to, value|
System.print(from)
result.add(lerp(from, to, value))
}
return result
2020-08-16 13:59:43 +00:00
}
static remap(min_in, max_in, min_out, max_out, value){
var inter = inv_lerp(min_in, max_in, value)
return lerp(min_out, max_out, inter)
}
2020-08-29 19:25:16 +00:00
static clamp(value, min, max){
//if its a simple number, we assume so are the bounds
if(value is Num){
return Math.max(Math.min(value, max), min)
}
//otherwise we assume its a sequence
//if bounds are numbers, just use them
if(min is Num && max is Num){
return value.map {|val| clamp(val, min, max)}.toList
}
if(min is Sequence && max is Sequence){
var result = []
Util.for_all([value, min, max]) { |value, min, max|
result.add(clamp(value, min, max))
}
return result
}
System.print("can't clamp %(value.type) between %(min.type) and %(max.type)")
return null
}
2020-08-16 13:59:43 +00:00
}