* @zh 检查点是否在多边形内 * @en Check if point is inside polygon
(x: number, y: number, vertices: readonly IPoint[])
| 276 | * @en Check if point is inside polygon |
| 277 | */ |
| 278 | private isPointInPolygon(x: number, y: number, vertices: readonly IPoint[]): boolean { |
| 279 | let inside = false; |
| 280 | const n = vertices.length; |
| 281 | |
| 282 | for (let i = 0, j = n - 1; i < n; j = i++) { |
| 283 | const xi = vertices[i].x; |
| 284 | const yi = vertices[i].y; |
| 285 | const xj = vertices[j].x; |
| 286 | const yj = vertices[j].y; |
| 287 | |
| 288 | if ( |
| 289 | yi > y !== yj > y && |
| 290 | x < ((xj - xi) * (y - yi)) / (yj - yi) + xi |
| 291 | ) { |
| 292 | inside = !inside; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return inside; |
| 297 | } |
| 298 | |
| 299 | // ========================================================================== |
| 300 | // IPathfindingMap 接口实现 | IPathfindingMap Interface Implementation |