68 lines
1.4 KiB
JavaScript
68 lines
1.4 KiB
JavaScript
|
import GeometryRect from "./GeometryRect.js";
|
||
|
|
||
|
export default class GeometryPointCollection
|
||
|
{
|
||
|
constructor()
|
||
|
{
|
||
|
this.points = [];
|
||
|
}
|
||
|
|
||
|
addGeometryPoint(geometryPoint)
|
||
|
{
|
||
|
if (!this.isContainingPoint(geometryPoint)) {
|
||
|
this.points.push(geometryPoint);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
isContainingPoint(geometryPoint)
|
||
|
{
|
||
|
for (let p = 0; p < this.points.length; p++) {
|
||
|
if (geometryPoint.isEqual(this.points[p])) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
getLength()
|
||
|
{
|
||
|
return this.points.length;
|
||
|
}
|
||
|
|
||
|
getPoint(index)
|
||
|
{
|
||
|
return this.points[index];
|
||
|
}
|
||
|
|
||
|
forEach(callback)
|
||
|
{
|
||
|
this.points.forEach(callback);
|
||
|
}
|
||
|
|
||
|
sort()
|
||
|
{
|
||
|
this.points.sort(
|
||
|
function (a, b) {
|
||
|
return a.x > b.x || a.y > b.y;
|
||
|
}
|
||
|
)
|
||
|
}
|
||
|
|
||
|
getRect()
|
||
|
{
|
||
|
if (this.getLength() < 2) {
|
||
|
console.log('GeometryPointCollection.getRect(): Collection must contain at least 2 points!');
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
this.sort();
|
||
|
|
||
|
let positionTopLeft = this.getPoint(0);
|
||
|
let positionBottomRight = this.getPoint(this.getLength() - 1);
|
||
|
let width = positionBottomRight.x - positionTopLeft.x;
|
||
|
let height = positionBottomRight.y - positionTopLeft.y;
|
||
|
|
||
|
return new GeometryRect(positionTopLeft.x, positionTopLeft.y, width, height);
|
||
|
}
|
||
|
}
|