kopia lustrzana https://github.com/Tldraw/Tldraw
Adds point in polygon
rodzic
429a5e6171
commit
4c39e19543
|
@ -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 --------------------- */
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
|
|
Ładowanie…
Reference in New Issue