diff --git a/src/point.ts b/src/point.ts index ec0c4c1..fd37e98 100755 --- a/src/point.ts +++ b/src/point.ts @@ -8,7 +8,6 @@ import {total} from '@mathigon/core'; import {clamp, lerp, nearlyEquals, Random, roundTo, square} from '@mathigon/fermat'; import {Bounds} from './bounds'; import {Line} from './line'; -import {isPoint} from './types'; import {GeoElement, rad, SimplePoint, TransformMatrix} from './utilities'; diff --git a/src/polygon.ts b/src/polygon.ts index 0583592..b49353b 100755 --- a/src/polygon.ts +++ b/src/polygon.ts @@ -3,7 +3,6 @@ // (c) Mathigon // ============================================================================= - import {last, tabulate} from '@mathigon/core'; import {nearlyEquals} from '@mathigon/fermat'; import {Circle} from './circle'; @@ -12,7 +11,6 @@ import {Line, Segment} from './line'; import {ORIGIN, Point} from './point'; import {findClosest, GeoShape, SimplePoint, TransformMatrix, TWO_PI} from './utilities'; - /** A polygon defined by its vertex points. */ export class Polygon implements GeoShape { readonly type: string = 'polygon'; @@ -88,7 +86,7 @@ export class Polygon implements GeoShape { } /** Checks if two polygons p1 and p2 collide. */ - static collision(p1: Polygon, p2: Polygon) { + static collision(p1: Polygon, p2: Polygon, tolerance?: number) { // Check if one of the vertices is in one of the polygons. if (p1.points.some(q => p2.contains(q))) return true; if (p2.points.some(q => p1.contains(q))) return true; @@ -100,7 +98,7 @@ export class Polygon implements GeoShape { } } - return false; + return p1.equals(p2, tolerance); } /** Creates a regular polygon. */ diff --git a/src/rectangle.ts b/src/rectangle.ts index 685baef..9484c3c 100644 --- a/src/rectangle.ts +++ b/src/rectangle.ts @@ -79,7 +79,7 @@ export class Rectangle implements GeoShape { collision(r: Rectangle) { return (this.p.x < r.p.x + r.w && this.p.x + this.w > r.p.x && - this.p.y < r.p.y + r.h && this.p.y + this.h > r.p.y); + this.p.y < r.p.y + r.h && this.p.y + this.h > r.p.y) || this.equals(r.polygon); } padding(top: number, right: number, bottom: number, left: number) { @@ -150,9 +150,8 @@ export class Rectangle implements GeoShape { return this.shift(p.x, p.y); } - equals(_other: Polygon) { - // TODO Implement - return false; + equals(other: Polygon) { + return this.polygon.equals(other); } toString() { diff --git a/test/polygon-test.ts b/test/polygon-test.ts index 222dc25..8c4e3b4 100644 --- a/test/polygon-test.ts +++ b/test/polygon-test.ts @@ -3,13 +3,10 @@ // (c) Mathigon // ============================================================================= - import tape from 'tape'; -import {Line, Point, Polygon, Polyline} from '../src'; - - -const poly = (...p: number[][]) => new Polygon(...p.map(q => new Point(q[0], q[1]))); +import {Point, Polygon, Polyline, Rectangle} from '../src'; +const poly = (...p: number[][]) => new Polygon(...p.map((q) => new Point(q[0], q[1]))); tape('Length and Circumference', (test) => { const p1 = poly([0, 0], [0, 1], [1, 1], [1, 0]); @@ -32,3 +29,11 @@ tape('Convex Hull', (test) => { test.end(); }); + +tape('Collision and Equals', (test) => { + const rect = new Rectangle(new Point(0, 0), 1); + const shape = poly([0, 0], [0, 1], [1, 1], [1, 0]); + test.equal(Polygon.collision(rect.polygon, shape), true); + test.equal(rect.equals(shape), true); + test.end(); +});