From 7162b3c4fb471357b77a592d2d31ca43518f46da Mon Sep 17 00:00:00 2001 From: "hyojun.choi" Date: Wed, 7 Aug 2024 17:28:34 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A7=80=EB=B6=95=20=EB=B6=84=ED=95=A0=20?= =?UTF-8?q?=EC=9E=91=EC=97=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/qpolygon-utils.js | 69 ++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/src/util/qpolygon-utils.js b/src/util/qpolygon-utils.js index 4b2f5dda..585c01b1 100644 --- a/src/util/qpolygon-utils.js +++ b/src/util/qpolygon-utils.js @@ -963,8 +963,25 @@ export default function offsetPolygon(vertices, offset) { } export const splitPolygonWithLines = (polygon) => { - //todo: polygon의 모든 line들 모음 hip, ridge, connectRidge - const allLines = [...polygon.hips, ...polygon.ridges, ...polygon.connectRidges] + const roofs = [] + const allLines = [...polygon.innerLines] + + // allLines에 x1,y1,x2,y2를 비교해서 중복되는 값을 제거한다. + allLines.forEach((line, index) => { + const startPoint = line.startPoint + const endPoint = line.endPoint + + allLines.forEach((line2, index2) => { + if (index !== index2) { + if ( + (isSamePoint(startPoint, line2.startPoint) && isSamePoint(endPoint, line2.endPoint)) || + (isSamePoint(endPoint, line2.startPoint) && isSamePoint(startPoint, line2.endPoint)) + ) { + allLines.splice(index2, 1) + } + } + }) + }) polygon.points.forEach((point, index) => { allLines.forEach((line) => { @@ -979,10 +996,6 @@ export const splitPolygonWithLines = (polygon) => { polygon.points.forEach((point, index) => { const routes = [] - const polygonPoints = [...polygon.points].map((point, index) => { - return { ...point, index } - }) - // 시작점은 시작 hip라인의 출발점 const startPoint = point // 도착점은 마지막 hip라인의 끝나는 점 @@ -1004,12 +1017,12 @@ export const splitPolygonWithLines = (polygon) => { while (!isSamePoint(currentPoint, arrivalPoint)) { // startHip에서 만나는 출발선 두개. 두개의 선을 출발하여 arrivalPoint에 도착할 때 까지 count를 세고, 더 낮은 count를 가진 길을 선택한다. - let connectedLines = allLinesCopy.filter( - (line) => isSamePoint(line.startPoint, currentPoint) || (isSamePoint(line.endPoint, currentPoint) && line !== currentLine), - ) + let connectedLines = allLinesCopy.filter((line) => isSamePoint(line.startPoint, currentPoint) || isSamePoint(line.endPoint, currentPoint)) + + connectedLines = connectedLines.filter((line) => line !== currentLine) if (connectedLines.length === 0) { - throw new Error('connectedLines is empty') + return } let tempPoints = [] @@ -1040,7 +1053,22 @@ export const splitPolygonWithLines = (polygon) => { } routes.push(endLine.startPoint) - const roof = new QPolygon(routes, { + roofs.push(routes) + }) + + // 중복 제거 + roofs.forEach((roofPoint, index) => { + const samePointLengthRoofPoints = roofs.filter((roof) => roof.length === roofPoint.length && roof !== roofPoint) + + samePointLengthRoofPoints.forEach((samePointRoof) => { + if (arraysHaveSamePoints(samePointRoof, roofPoint)) { + roofs.splice(roofs.indexOf(samePointRoof), 1) + } + }) + }) + + roofs.forEach((roofPoint) => { + const roof = new QPolygon(roofPoint, { fontSize: polygon.fontSize, stroke: 'black', fill: 'transparent', @@ -2504,3 +2532,22 @@ const getLineDirection = (line) => { } } } + +function arePointsEqual(point1, point2) { + return point1.x === point2.x && point1.y === point2.y +} + +function arraysHaveSamePoints(array1, array2) { + if (array1.length !== array2.length) return false + + const sortedArray1 = array1.slice().sort((a, b) => a.x - b.x || a.y - b.y) + const sortedArray2 = array2.slice().sort((a, b) => a.x - b.x || a.y - b.y) + + for (let i = 0; i < sortedArray1.length; i++) { + if (!arePointsEqual(sortedArray1[i], sortedArray2[i])) { + return false + } + } + + return true +}