mirror of
https://git.leinelab.org/Mal/mr-crocs-adventures.git
synced 2025-09-11 02:57:36 +02:00
Init
This commit is contained in:
68
js/geometry/GeometryPointCollection.js
Normal file
68
js/geometry/GeometryPointCollection.js
Normal file
@@ -0,0 +1,68 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user