CabinGame/Luxe/math/math.wren

78 lines
2.5 KiB
Text
Raw Normal View History

2020-08-29 19:25:16 +00:00
import "math/vector" for Vector
import "luxe: math" for Math
2020-08-30 08:31:57 +00:00
import "math/repVal" for RepVal
2020-08-29 19:25:16 +00:00
import "math/Util" for Util
2020-08-16 13:59:43 +00:00
class M{
2020-08-30 08:31:57 +00:00
static inv_lerp(from, to, value){
//if the range are numbers, we assume the interpolation value is too
if(from is Num && to is Num) return (value - from) / (to - from)
//if only one of the range qualifiers is a sequence, we make the other one a endless sequence of itself
//(will do a union to take the shortest length anyways)
if(from is Sequence != to is Sequence) {
if(from is Sequence){
to = RepVal.new(to)
} else {
from = RepVal.new(from)
}
}
//if the interpolation value is a number, make it infinite
if(value is Num) value = RepVal.new(value)
var result = []
2020-09-03 15:31:23 +00:00
Util.for_all([from, to, value]) { |from, to, value|
result.add(inv_lerp(from, to, value))
2020-08-30 08:31:57 +00:00
}
return result
2020-08-16 13:59:43 +00:00
}
static lerp(from, to, value){
2020-08-30 08:31:57 +00:00
//if the range are numbers, we assume the interpolation value is too
2020-08-29 19:25:16 +00:00
if(from is Num && to is Num) return from + (to - from) * value
2020-08-30 08:31:57 +00:00
//if only one of the range qualifiers is a sequence, we make the other one a endless sequence of itself
//(will do a union to take the shortest length anyways)
2020-08-29 19:25:16 +00:00
if(from is Sequence != to is Sequence) {
if(from is Sequence){
2020-08-30 08:31:57 +00:00
to = RepVal.new(to)
2020-08-29 19:25:16 +00:00
} else {
2020-08-30 08:31:57 +00:00
from = RepVal.new(from)
2020-08-29 19:25:16 +00:00
}
}
2020-08-30 08:31:57 +00:00
//if the interpolation value is a number, make it infinite
if(value is Num) value = RepVal.new(value)
2020-08-29 19:25:16 +00:00
var result = []
2020-09-03 15:31:23 +00:00
Util.for_all([from, to, value]) { |from, to, value|
result.add(lerp(from, to, value))
2020-08-29 19:25:16 +00:00
}
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 = []
2020-08-30 08:31:57 +00:00
Util.for_all([value, min, max]) { |v|
result.add(clamp(v[0], v[1], v[2]))
2020-08-29 19:25:16 +00:00
}
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
}