mirror of
https://git.leinelab.org/Mal/mr-crocs-adventures.git
synced 2025-09-11 02:57:36 +02:00
Basic implementations
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
export default class GeometryPoint
|
||||
{
|
||||
constructor(x, y)
|
||||
constructor(x = 0, y = 0)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import GeometryPoint from "./GeometryPoint.js";
|
||||
import GeometryStroke from "./GeometryStroke.js";
|
||||
import GeometryPointCollection from "./GeometryPointCollection.js";
|
||||
import GeometryLine from "./GeometryLine.js";
|
||||
|
||||
export default class GeometryRect
|
||||
{
|
||||
@@ -31,6 +32,11 @@ export default class GeometryRect
|
||||
return containsHorizontally && containsVertically;
|
||||
}
|
||||
|
||||
hasIntersectionWithRect(rect)
|
||||
{
|
||||
return this.getBorderIntersectonsWithRect(rect).getLength() > 0;
|
||||
}
|
||||
|
||||
getBorderTop()
|
||||
{
|
||||
return new GeometryStroke(
|
||||
@@ -92,6 +98,12 @@ export default class GeometryRect
|
||||
return intersections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the intersection points between the border lines and the given stroke.
|
||||
*
|
||||
* @param stroke
|
||||
* @returns {GeometryPointCollection}
|
||||
*/
|
||||
getBorderIntersectionsWithStroke(stroke)
|
||||
{
|
||||
let borders = [
|
||||
@@ -172,6 +184,12 @@ export default class GeometryRect
|
||||
*/
|
||||
getRectIntersection(rect)
|
||||
{
|
||||
let distanceToOrigin = this.getCenter().getDistanceToPoint(rect.getCenter());
|
||||
|
||||
if (distanceToOrigin > this.getDiagonal() * 0.5 + rect.getDiagonal() * 0.5) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let intersectionPoints = new GeometryPointCollection();
|
||||
|
||||
this.getRectanglePoints().forEach(
|
||||
@@ -211,6 +229,22 @@ export default class GeometryRect
|
||||
return null;
|
||||
}
|
||||
|
||||
getCenter()
|
||||
{
|
||||
return new GeometryPoint(
|
||||
this.position.x + this.width * 0.5,
|
||||
this.position.y + this.height * 0.5
|
||||
);
|
||||
}
|
||||
|
||||
getDiagonal()
|
||||
{
|
||||
return new GeometryStroke(
|
||||
this.position,
|
||||
new GeometryPoint(this.position.x + this.width, this.position.y + this.height)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visualizes the rect.
|
||||
*
|
||||
|
||||
@@ -21,6 +21,11 @@ export default class GeometryStroke extends GeometryLine
|
||||
return new GeometryLine(this.pointA, this.pointB);
|
||||
}
|
||||
|
||||
getLength()
|
||||
{
|
||||
return this.pointA.getDistanceToPoint(this.pointB);
|
||||
}
|
||||
|
||||
getIntersectionWithStroke(stroke)
|
||||
{
|
||||
let intersection = super.getIntersectionWithLine(stroke);
|
||||
|
||||
Reference in New Issue
Block a user