Gravity and ground height.

This commit is contained in:
Mal
2020-01-25 13:11:25 +01:00
parent 388799c97e
commit 0cd532f45a
8 changed files with 316 additions and 27 deletions

View File

@@ -1,7 +1,6 @@
import GeometryPoint from "./GeometryPoint.js";
import GeometryStroke from "./GeometryStroke.js";
import GeometryPointCollection from "./GeometryPointCollection.js";
import GeometryLine from "./GeometryLine.js";
export default class GeometryRect
{
@@ -26,12 +25,17 @@ export default class GeometryRect
*/
isContainingPoint(geometryPoint)
{
let containsHorizontally = geometryPoint.x >= this.position.x && geometryPoint.x <= this.position.x + this.width;
let containsVertically = geometryPoint.y >= this.position.y && geometryPoint.y <= this.position.y + this.height;
let containsHorizontally = geometryPoint.x >= this.position.x && geometryPoint.x < this.position.x + this.width;
let containsVertically = geometryPoint.y >= this.position.y && geometryPoint.y < this.position.y + this.height;
return containsHorizontally && containsVertically;
}
isEqual(rect)
{
return rect.x === this.x && rect.y === this.y && rect.width === this.width && rect.height === this.height;
}
hasIntersectionWithRect(rect)
{
return this.getBorderIntersectonsWithRect(rect).getLength() > 0;
@@ -170,8 +174,8 @@ export default class GeometryRect
collection.addGeometryPoint(this.position);
collection.addGeometryPoint(new GeometryPoint(this.position.x + this.width, this.position.y));
collection.addGeometryPoint(new GeometryPoint(this.position.x, this.position.y + this.height));
collection.addGeometryPoint(new GeometryPoint(this.position.x + this.width, this.position.y + this.height));
collection.addGeometryPoint(new GeometryPoint(this.position.x, this.position.y + this.height - 1));
collection.addGeometryPoint(new GeometryPoint(this.position.x + this.width, this.position.y + this.height - 1));
return collection;
}

View File

@@ -0,0 +1,133 @@
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];
}
}