CabinGame/Luxe/math/math.wren
2020-08-29 21:25:16 +02:00

59 lines
No EOL
1.6 KiB
Text

import "math/vector" for Vector
import "luxe: math" for Math
import "math/repNum" for RepNum
import "math/Util" for Util
class M{
static inv_lerp(from, to, inter){
return (inter - from) / (to - from)
}
static lerp(from, to, value){
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
}
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)
}
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
}
}