diff --git a/src/components/fabric/QPolygon.js b/src/components/fabric/QPolygon.js index 2cb17965..6c4c7f5e 100644 --- a/src/components/fabric/QPolygon.js +++ b/src/components/fabric/QPolygon.js @@ -2,7 +2,7 @@ import { fabric } from 'fabric' import { v4 as uuidv4 } from 'uuid' import { QLine } from '@/components/fabric/QLine' import { distanceBetweenPoints, findTopTwoIndexesByDistance, getDirectionByPoint, sortedPointLessEightPoint, sortedPoints } from '@/util/canvas-util' -import { calculateAngle, drawHippedRoof, splitPolygonWithLines, toGeoJSON } from '@/util/qpolygon-utils' +import { calculateAngle, drawHippedRoof, inPolygon, lineIntersect, splitPolygonWithLines, toGeoJSON } from '@/util/qpolygon-utils' import * as turf from '@turf/turf' export const QPolygon = fabric.util.createClass(fabric.Polygon, { @@ -244,9 +244,7 @@ export const QPolygon = fabric.util.createClass(fabric.Polygon, { { x: rectLeft + rectWidth, y: rectTop }, ] - const allPointsInside = this.inPolygon2(rectPoints) - - if (allPointsInside) { + if (inPolygon(this.points, rectPoints)) { const rect = new fabric.Rect({ left: rectLeft, top: rectTop, diff --git a/src/util/qpolygon-utils.js b/src/util/qpolygon-utils.js index 8ba2a3c9..258f5d97 100644 --- a/src/util/qpolygon-utils.js +++ b/src/util/qpolygon-utils.js @@ -2670,3 +2670,25 @@ export const toGeoJSON = (pointsArray) => { return coordinates } + +export const inPolygon = (polygonPoints, rectPoints) => { + const polygonCoordinates = toGeoJSON(polygonPoints) + const rectCoordinates = toGeoJSON(rectPoints) + + const polygonFeature = turf.polygon([polygonCoordinates]) + const rectFeature = turf.polygon([rectCoordinates]) + + // 사각형의 모든 꼭짓점이 다각형 내부에 있는지 확인 + const allPointsInsidePolygon = rectCoordinates.every((coord) => { + const point = turf.point(coord) + return turf.booleanPointInPolygon(point, polygonFeature) + }) + + // 다각형의 모든 점이 사각형 내부에 있지 않은지 확인 + const noPolygonPointsInsideRect = polygonCoordinates.every((coord) => { + const point = turf.point(coord) + return !turf.booleanPointInPolygon(point, rectFeature) + }) + + return allPointsInsidePolygon && noPolygonPointsInsideRect +}