Function
isPointInPolygon
(point: Point, polygon: Point[])
Source from the content-addressed store, hash-verified
| 153 | |
| 154 | // Ray casting point-in-polygon test. |
| 155 | function isPointInPolygon(point: Point, polygon: Point[]): boolean { |
| 156 | let {x, y} = point; |
| 157 | let inside = false; |
| 158 | for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) { |
| 159 | let xi = polygon[i].x; |
| 160 | let yi = polygon[i].y; |
| 161 | let xj = polygon[j].x; |
| 162 | let yj = polygon[j].y; |
| 163 | let intersect = yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi; |
| 164 | if (intersect) { |
| 165 | inside = !inside; |
| 166 | } |
| 167 | } |
| 168 | return inside; |
| 169 | } |
Tested by
no test coverage detected