diff --git a/src/hooks/usePolygon.js b/src/hooks/usePolygon.js index 84f8e9bf..463be9f2 100644 --- a/src/hooks/usePolygon.js +++ b/src/hooks/usePolygon.js @@ -1,14 +1,7 @@ import { ANGLE_TYPE, canvasState, currentAngleTypeSelector, pitchTextSelector } from '@/store/canvasAtom' import { useRecoilValue } from 'recoil' import { fabric } from 'fabric' -import { - distanceBetweenPoints, - findAndRemoveClosestPoint, - getDegreeByChon, - getDegreeInOrientation, - isPointOnLine, - toFixedWithoutRounding, -} from '@/util/canvas-util' +import { findAndRemoveClosestPoint, getDegreeByChon, getDegreeInOrientation, isPointOnLine } from '@/util/canvas-util' import { QPolygon } from '@/components/fabric/QPolygon' import { isSamePoint, removeDuplicatePolygons } from '@/util/qpolygon-utils' import { flowDisplaySelector } from '@/store/settingAtom' @@ -1073,8 +1066,14 @@ export const usePolygon = () => { ) }) + allLines = allLines.filter((line) => { + return Math.abs(line.startPoint.x - line.endPoint.x) > 2 || Math.abs(line.startPoint.y - line.endPoint.y) > 2 + }) + // 나눠서 중복 제거된 roof return - const newRoofs = getSplitRoofsPoints(allLines) + let newRoofs = getSplitRoofsPoints(allLines) + + newRoofs = newRoofs.filter((roof) => roof.length !== 0) newRoofs.forEach((roofPoint, index) => { let defense, pitch @@ -1212,10 +1211,45 @@ export const usePolygon = () => { } const getSplitRoofsPoints = (allLines) => { + // 모든 좌표점들을 수집 + const allPoints = [] + allLines.forEach((line, lineIndex) => { + allPoints.push({ point: line.startPoint, lineIndex, pointType: 'start' }) + allPoints.push({ point: line.endPoint, lineIndex, pointType: 'end' }) + }) + + // X 좌표 통일 + for (let i = 0; i < allPoints.length; i++) { + for (let j = i + 1; j < allPoints.length; j++) { + const point1 = allPoints[i].point + const point2 = allPoints[j].point + + if (Math.abs(point1.x - point2.x) < 1) { + const maxX = Math.max(point1.x, point2.x) + point1.x = maxX + point2.x = maxX + } + } + } + + // Y 좌표 통일 + for (let i = 0; i < allPoints.length; i++) { + for (let j = i + 1; j < allPoints.length; j++) { + const point1 = allPoints[i].point + const point2 = allPoints[j].point + + if (Math.abs(point1.y - point2.y) < 1) { + const maxY = Math.max(point1.y, point2.y) + point1.y = maxY + point2.y = maxY + } + } + } + // ==== Utility functions ==== function isSamePoint(p1, p2, epsilon = 1) { - return Math.abs(p1.x - p2.x) <= 2 && Math.abs(p1.y - p2.y) <= 2 + return Math.abs(p1.x - p2.x) <= epsilon && Math.abs(p1.y - p2.y) <= epsilon } function normalizePoint(p, epsilon = 1) { @@ -1241,6 +1275,7 @@ export const usePolygon = () => { const startKey = pointToKey(start, epsilon) return (graph[startKey] || []).some((neighbor) => isSamePoint(neighbor.point, end, epsilon)) } + // ==== Dijkstra pathfinding ==== function findShortestPath(start, end, graph, epsilon = 1) { @@ -1329,11 +1364,15 @@ export const usePolygon = () => { } const startPoint = { ...line.startPoint } // 시작점 + let arrivalPoint = { ...line.endPoint } // 도착점 roofs.push(getPath(startPoint, arrivalPoint, graph)) }) - return removeDuplicatePolygons(roofs.filter((roof) => roof.length < 100)) + return removeDuplicatePolygons( + roofs.filter((roof) => roof.length < 100), + allLines.some((line) => line.name === 'auxiliaryLine'), + ) } const splitPolygonWithSeparate = (separates) => { diff --git a/src/util/qpolygon-utils.js b/src/util/qpolygon-utils.js index 610d043d..72ea0db0 100644 --- a/src/util/qpolygon-utils.js +++ b/src/util/qpolygon-utils.js @@ -292,7 +292,7 @@ function arePolygonsEqual(polygon1, polygon2) { return normalizedPolygon1.every((point, index) => arePointsEqual(point, normalizedPolygon2[index])) } -export function removeDuplicatePolygons(polygons) { +export function removeDuplicatePolygons(polygons, hasAuxiliaryLine = false) { let uniquePolygons = [] // x가 전부 같거나, y가 전부 같은 경우 제거 @@ -303,9 +303,11 @@ export function removeDuplicatePolygons(polygons) { } }) - uniquePolygons = uniquePolygons.filter((polygon) => { - return isValidPoints(polygon) - }) + if (!hasAuxiliaryLine) { + uniquePolygons = uniquePolygons.filter((polygon) => { + return isValidPoints(polygon) + }) + } return uniquePolygons }