Skip to content

Commit c31c1e7

Browse files
authored
Merge pull request #25 from xThrodx/master
Fix edge case
2 parents 5c90d7d + fd52181 commit c31c1e7

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function pointInPolygon(p, polygon) {
3737

3838
if (v1 === 0 && v2 === 0) {
3939
if ((u2 <= 0 && u1 >= 0) || (u1 <= 0 && u2 >= 0)) return 0
40-
} else if ((v2 >= 0 && v1 < 0) || (v2 < 0 && v1 >= 0)) {
40+
} else if ((v2 >= 0 && v1 <= 0) || (v2 <= 0 && v1 >= 0)) {
4141
f = orient2d(u1, u2, v1, v2, 0, 0)
4242
if (f === 0) return 0
4343
if ((f > 0 && v2 > 0 && v1 <= 0) || (f < 0 && v2 <= 0 && v1 > 0)) k++

test/test.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,38 @@ test('error is thrown when not the same first and last coords', () => {
6363

6464
expect(() => inside([1, 1], poly)).toThrowError(/First and last/)
6565
});
66+
67+
68+
const polygonDiamond = [[[-1, 0], [0, -1], [1, 0], [0, 1], [-1, 0]]];
69+
70+
test('is inside diamond', () => {
71+
expect(inside([0, 0], polygonDiamond)).toBe(true)
72+
});
73+
74+
test('is outside diamond', () => {
75+
expect(inside([1, 1], polygonDiamond)).toBe(false)
76+
});
77+
78+
test('is on left vertex', () => {
79+
expect(inside([-1, 0], polygonDiamond)).toBe(0)
80+
});
81+
82+
test('is on bottom vertex', () => {
83+
expect(inside([0, -1], polygonDiamond)).toBe(0)
84+
});
85+
86+
test('is on right vertex', () => {
87+
expect(inside([1, 0], polygonDiamond)).toBe(0)
88+
});
89+
90+
test('is on top vertex', () => {
91+
expect(inside([0, 1], polygonDiamond)).toBe(0)
92+
});
93+
94+
test('is on bottom left edge', () => {
95+
expect(inside([-0.5, -0.5], polygonDiamond)).toBe(0)
96+
});
97+
98+
test('is on bottom right edge', () => {
99+
expect(inside([0.5, -0.5], polygonDiamond)).toBe(0)
100+
});

0 commit comments

Comments
 (0)