-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPointInPolygon.js
More file actions
32 lines (30 loc) · 1.04 KB
/
PointInPolygon.js
File metadata and controls
32 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* The problem
*
* In this kata, you're going write a function called pointInPoly to test if a point is inside
* a polygon.
*
* Points will be represented as [x,y] arrays.
*
* The polygon will be an array of points which are the polygon's vertices.
* The last point in the array connects back to the first point.
*
* You can assume:
*
* The polygon will be a valid simple polygon. That is, it will have at least three points,
* none of its edges will cross each other, and exactly two edges will meet at each vertex.
* In the tests, the point will never fall exactly on an edge of the polygon.
*/
let pointsIntersect = function (yi, y, yj, x, xj, xi) {
return ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi)
}
function pointInPoly(polygon, point) {
const [x, y] = point
let j = polygon.length - 1
return polygon.reduce((inside, el, i) => {
const [xi, yi] = el, [xj, yj] = polygon[j]
j = i++
return pointsIntersect(yi, y, yj, x, xj, xi) ? !inside : inside
}, false)
}