Skip to content

Commit dd010af

Browse files
authored
Fix Polygon collision when shapes are equal (#196)
1 parent b4ad997 commit dd010af

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

src/point.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {total} from '@mathigon/core';
88
import {clamp, lerp, nearlyEquals, Random, roundTo, square} from '@mathigon/fermat';
99
import {Bounds} from './bounds';
1010
import {Line} from './line';
11-
import {isPoint} from './types';
1211
import {GeoElement, rad, SimplePoint, TransformMatrix} from './utilities';
1312

1413

src/polygon.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// (c) Mathigon
44
// =============================================================================
55

6-
76
import {last, tabulate} from '@mathigon/core';
87
import {nearlyEquals} from '@mathigon/fermat';
98
import {Circle} from './circle';
@@ -12,7 +11,6 @@ import {Line, Segment} from './line';
1211
import {ORIGIN, Point} from './point';
1312
import {findClosest, GeoShape, SimplePoint, TransformMatrix, TWO_PI} from './utilities';
1413

15-
1614
/** A polygon defined by its vertex points. */
1715
export class Polygon implements GeoShape {
1816
readonly type: string = 'polygon';
@@ -88,7 +86,7 @@ export class Polygon implements GeoShape {
8886
}
8987

9088
/** Checks if two polygons p1 and p2 collide. */
91-
static collision(p1: Polygon, p2: Polygon) {
89+
static collision(p1: Polygon, p2: Polygon, tolerance?: number) {
9290
// Check if one of the vertices is in one of the polygons.
9391
if (p1.points.some(q => p2.contains(q))) return true;
9492
if (p2.points.some(q => p1.contains(q))) return true;
@@ -100,7 +98,7 @@ export class Polygon implements GeoShape {
10098
}
10199
}
102100

103-
return false;
101+
return p1.equals(p2, tolerance);
104102
}
105103

106104
/** Creates a regular polygon. */

src/rectangle.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class Rectangle implements GeoShape {
7979

8080
collision(r: Rectangle) {
8181
return (this.p.x < r.p.x + r.w && this.p.x + this.w > r.p.x &&
82-
this.p.y < r.p.y + r.h && this.p.y + this.h > r.p.y);
82+
this.p.y < r.p.y + r.h && this.p.y + this.h > r.p.y) || this.equals(r.polygon);
8383
}
8484

8585
padding(top: number, right: number, bottom: number, left: number) {
@@ -150,9 +150,8 @@ export class Rectangle implements GeoShape {
150150
return this.shift(p.x, p.y);
151151
}
152152

153-
equals(_other: Polygon) {
154-
// TODO Implement
155-
return false;
153+
equals(other: Polygon) {
154+
return this.polygon.equals(other);
156155
}
157156

158157
toString() {

test/polygon-test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
// (c) Mathigon
44
// =============================================================================
55

6-
76
import tape from 'tape';
8-
import {Line, Point, Polygon, Polyline} from '../src';
9-
10-
11-
const poly = (...p: number[][]) => new Polygon(...p.map(q => new Point(q[0], q[1])));
7+
import {Point, Polygon, Polyline, Rectangle} from '../src';
128

9+
const poly = (...p: number[][]) => new Polygon(...p.map((q) => new Point(q[0], q[1])));
1310

1411
tape('Length and Circumference', (test) => {
1512
const p1 = poly([0, 0], [0, 1], [1, 1], [1, 0]);
@@ -32,3 +29,11 @@ tape('Convex Hull', (test) => {
3229

3330
test.end();
3431
});
32+
33+
tape('Collision and Equals', (test) => {
34+
const rect = new Rectangle(new Point(0, 0), 1);
35+
const shape = poly([0, 0], [0, 1], [1, 1], [1, 0]);
36+
test.equal(Polygon.collision(rect.polygon, shape), true);
37+
test.equal(rect.equals(shape), true);
38+
test.end();
39+
});

0 commit comments

Comments
 (0)