CabinGame/Luxe/math/rect.wren
2020-09-21 21:18:07 +02:00

92 lines
No EOL
1.8 KiB
Text

import "math/vector" for Vector
import "math/math" for M
class AABB{
x{_x}
y{_y}
x=(val){_x = val}
y=(val){_y = val}
width {_width}
height {_height}
width=(val){_width = val}
height=(val){_height = val}
min_x{_x}
min_y{_y}
max_x{_x + _width}
max_y{_y + _height}
min{Vector.new(min_x, min_y)}
max{Vector.new(max_x, max_y)}
center{(min + max) / 2}
pos{min}
pos=(val){
_x = val.x
_y = val.y
}
size{Vector.new(_width, _height)}
pos_size_list{[x, y, width, height]}
min_max_list{[min_x, min_y, max_x, max_y]}
construct new(){}
static new(pos, size){
return new(pos.x, pos.y, size.x, size.y)
}
construct new(x, y, width, height){
_x = x
_y = y
_width = width
_height = height
}
relative_pos(vector){
return M.inv_lerp(min, max, vector)
}
static ONE(){
return new(0, 0, 1, 1)
}
static min_max(min, max){
return min_max(min.x, min.y, max.x, max.y)
}
construct min_max(min_x, min_y, max_x, max_y){
_x = min_x
_y = min_y
_width = max_x - min_x
_height = max_y - min_y
}
static intersects(one, other){
return one.max > other.min && one.min < other.max
}
static contains(one, other){
return one.max > other.max && one.min < other.min
}
static shrink(rect, amount){
if(amount is Num){
amount = [amount, amount]
}
var half_amount = [amount.x/2, amount.y/2]
return AABB.new(rect.pos + half_amount, rect.size - amount)
}
static grow(rect, amount){
if(amount is Num){
amount = [amount, amount]
}
var half_amount = [amount.x/2, amount.y/2]
return AABB.new(rect.pos - half_amount, rect.size + amount)
}
}