mr-crocs-adventures/js/geometry/GeometryRectCollection.js

133 lines
2.5 KiB
JavaScript

export default class GeometryRectCollection
{
constructor()
{
this.rects = [];
}
isContainingRect(rect)
{
for (let r = 0; r < this.rects.length; r++) {
if (rect.isEqual(this.rects[r])) {
return true;
}
}
return false;
}
getUniqueWidth()
{
if (this.getLength() === 0) {
return null;
} else if (this.getLength() === 1) {
return this.rects[0];
}
let widths = [this.rects[0].width];
for (let r = 0; r < this.getLength(); r++) {
if (widths.indexOf(this.rects[r].width) !== -1) {
return this.rects[r];
}
widths.push(rect.width);
}
return null;
}
getUniqueHeight()
{
if (this.getLength() === 0) {
return null;
} else if (this.getLength() === 1) {
return this.rects[0].height;
}
let heights = [this.rects[0].height];
for (let r = 0; r < this.getLength(); r++) {
if (heights.indexOf(rect.height) !== -1) {
return rect.height;
}
heights.push(rect.height);
}
return null;
}
addRect(rect)
{
if (!this.isContainingRect(rect)) {
this.rects.push(rect);
}
}
getLength()
{
return this.rects.length;
}
getEqualWidths()
{
if (this.getLength() === 0) {
return null;
}
let width = this.rects[0].width;
for (let r = 1; r < this.getLength(); r++) {
if (this.rects[r].width === width) {
return false;
}
}
return true;
}
hasEqualWidths()
{
if (this.getLength() === 0) {
return false;
}
let width = this.rects[0].width;
for (let r = 1; r < this.getLength(); r++) {
if (this.rects[r].width !== width) {
return false;
}
}
return true;
}
hasEqualHeights()
{
if (this.getLength() === 0) {
return false;
}
let height = this.rects[0].height;
for (let r = 1; r < this.getLength(); r++) {
if (this.rects[r].height !== height) {
return false;
}
}
return true;
}
forEach(callback)
{
this.rects.forEach(callback);
}
getRect(index)
{
return this.rects[index];
}
}