Skip to content

Commit

Permalink
Fix Polygon collision when shapes are equal (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshestein authored Oct 18, 2024
1 parent b4ad997 commit dd010af
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down
6 changes: 2 additions & 4 deletions src/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// (c) Mathigon
// =============================================================================


import {last, tabulate} from '@mathigon/core';
import {nearlyEquals} from '@mathigon/fermat';
import {Circle} from './circle';
Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -100,7 +98,7 @@ export class Polygon implements GeoShape {
}
}

return false;
return p1.equals(p2, tolerance);
}

/** Creates a regular polygon. */
Expand Down
7 changes: 3 additions & 4 deletions src/rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 10 additions & 5 deletions test/polygon-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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();
});

0 comments on commit dd010af

Please sign in to comment.