app_mapbuilder/libs/bbox.js
2022-04-22 18:34:31 +05:00

78 lines
1.4 KiB
JavaScript

class BBox {
static fromLTWH(l, t, w, h){
return BBox.fromLTRB(l, t, l + w, t + h)
}
static fromLTRB(l, t, r, b){
return Object.assign(new BBox(), {l: Math.min(l, r), t: Math.min(t, b), r: Math.max(l, r), b: Math.max(t, b)})
}
static from_array(arr_xy){
const bbox = BBox.fromLTWH(arr_xy[0].x, arr_xy[0].y, 0, 0)
return bbox.append_many(arr_xy)
}
w(){
return this.r - this.l
}
h(){
return this.b - this.t
}
toLTWH(){
const {l, t, r, b} = this
return {l, t, w: r - l, h: b - t }
}
toLTRB(){
const {l, t, r, b} = this
return {l, t, r, b}
}
toLTRBarr(){
const {l, t, r, b} = this
return [l, t, r, b]
}
append(x, y){
this.l = Math.min(this.l, x)
this.t = Math.min(this.t, y)
this.r = Math.max(this.r, x)
this.b = Math.max(this.b, y)
return this
}
append_many(arr_xy){
arr_xy.forEach(e => this.append(e.x, e.y))
return this
}
scale(k){
this.r = this.l + (this.r - this.l) * k
this.b = this.t + (this.b - this.t) * k
return this
}
move(dx, dy){
this.l += dx
this.t += dy
this.r += dx
this.b += dy
return this
}
moveto(x, y){
let dx = x - this.l
let dy = y - this.t
return this.move(dx, dy)
}
clone(){
return BBox.fromLTRB(this.l, this.t, this.r, this.b)
}
}
module.exports = BBox
module.exports.default = BBox