class PatchCluster extends Array {
constructor() {
super();
this.parent = null;
this.dirty = false;
}
insert (patch) {
patch.parent = this;
this.push(patch);
}
union (other) {
for (let o of other) {
this.insert(o);
}
other = null;
}
box () {
if (this.length == 0) return null;
var box = this[0].box();
for (var i = 1; i < this.length; i++) {
let other = this[i].box();
let x1 = Math.min (box[0].x, other[0].x);
let y1 = Math.min (box[0].y, other[0].y);
let x2 = Math.max (box[2].x, other[2].x);
let y2 = Math.max (box[2].y, other[2].y);
box = [Vec(x1,y1),Vec(x2,y1),Vec(x2,y2),Vec(x1,y2)];
}
return box;
}
intersect(b1,b2) {
if ( ((b2[0].x > b1[0].x && b2[0].x < b1[1].x) ||
(b2[0].x < b1[0].x && b2[1].x > b1[0].x) ) &&
((b2[0].y > b1[0].y && b2[0].y < b1[2].y) ||
(b2[0].y < b1[0].y && b2[2].y > b1[0].y) ) ) {
return true;
}
return false;
}
dist (other) {
let box = this.box();
let otherbox = other.box();
if (this.intersect(box,otherbox)) return [0,0];
return [ Math.min(Math.abs(otherbox[0].x-box[1].x), Math.abs(otherbox[1].x-box[0].x)),
Math.min(Math.abs(otherbox[0].y-box[3].y), Math.abs(otherbox[3].y-box[0].y)) ];
}
contains (p) {
for (let e of this) {
if (e.contains (p)) return true;
}
return false;
}
translate (p) {
for (let patch of this) {
patch.translate(p);
}
}
}