CabinGame/Luxe/math/math.wren

102 lines
3 KiB
Text
Raw Permalink Normal View History

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
}
2020-10-11 15:22:57 +00:00
static pow2(value){
return value * value
}
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
}
//not remainder!!
//this is the cool modulo
//the one that can deal with negative numbers not badly
static mod(number, min, max){
return min + mod(number, max - min)
2020-09-25 08:42:41 +00:00
}
static mod(number, max){
2020-09-25 08:42:41 +00:00
return max.sign * ((number % max + max) % max)
}
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
}
Log.print("can't clamp %(value.type) between %(min.type) and %(max.type)")
2020-08-29 19:25:16 +00:00
return null
}
2020-09-21 19:18:07 +00:00
static sqr_length(vector){
return vector.reduce(0){|sum, val| sum + val * val }
}
static length(vector){
return sqr_length(vector).sqrt
}
2020-10-17 17:43:53 +00:00
}
import "math/vector" for Vector
import "luxe: math" for Math
import "math/repVal" for RepVal
2021-02-09 08:44:12 +00:00
import "math/util" for Util