Basic implementations

This commit is contained in:
Mal
2020-01-22 22:50:45 +01:00
parent 893c226fe4
commit fb762d1778
17 changed files with 424 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
export default class GeometryPoint
{
constructor(x, y)
constructor(x = 0, y = 0)
{
this.x = x;
this.y = y;

View File

@@ -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.
*

View File

@@ -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);