38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
import * as turf from '@turf/turf'
|
|
|
|
export const useTurf = () => {
|
|
/**
|
|
* 모듈이 배치면 안에 있는지 확인
|
|
* @param {*} module
|
|
* @param {*} surface
|
|
* @param spare
|
|
* @returns
|
|
*/
|
|
const checkModuleDisjointSurface = (module, surface, spare = 1) => {
|
|
// 표면 영역을 spare만큼 수동 확장
|
|
const expandedSurface = {
|
|
type: 'Polygon',
|
|
coordinates: [
|
|
surface.geometry.coordinates[0].map(([x, y]) => {
|
|
// 각 점을 바깥쪽으로 2 단위씩 이동
|
|
const coords = surface.geometry.coordinates[0]
|
|
const centerX = coords.slice(0, -1).reduce((sum, [x, y]) => sum + x, 0) / (coords.length - 1)
|
|
const centerY = coords.slice(0, -1).reduce((sum, [x, y]) => sum + y, 0) / (coords.length - 1)
|
|
|
|
return [x < centerX ? x - spare : x + spare, y < centerY ? y - spare : y + spare]
|
|
}),
|
|
],
|
|
}
|
|
|
|
const isWithin = turf.booleanContains(expandedSurface, module) || turf.booleanWithin(module, expandedSurface)
|
|
|
|
const isContact = turf.booleanIntersects(module, expandedSurface) && !turf.booleanOverlap(module, expandedSurface)
|
|
|
|
return isWithin || isContact
|
|
}
|
|
|
|
return {
|
|
checkModuleDisjointSurface,
|
|
}
|
|
}
|