Adds point in polygon

pull/71/head
Steve Ruiz 2021-08-21 22:21:11 +01:00
rodzic 429a5e6171
commit 4c39e19543
2 zmienionych plików z 25 dodań i 0 usunięć

Wyświetl plik

@ -899,6 +899,23 @@ export class Utils {
)
}
static pointInPolygon(p: number[], points: number[][]): boolean {
let wn = 0 // winding number
points.forEach((a, i) => {
const b = points[(i + 1) % points.length]
if (a[1] <= p[1]) {
if (b[1] > p[1] && vec.cross(a, b, p) > 0) {
wn += 1
}
} else if (b[1] <= p[1] && vec.cross(a, b, p) < 0) {
wn -= 1
}
})
return wn !== 0
}
/* --------------------- Bounds --------------------- */
/**

Wyświetl plik

@ -136,6 +136,14 @@ export class Vec {
return A[0] * B[1] - B[0] * A[1]
}
/**
* Cross (for point in polygon)
*
*/
static cross(x: number[], y: number[], z: number[]): number {
return (y[0] - x[0]) * (z[1] - x[1]) - (z[0] - x[0]) * (y[1] - x[1])
}
/**
* Length of the vector squared
* @param A