From 381c639d187280c5ca891ac79ffce6883e801a80 Mon Sep 17 00:00:00 2001 From: "hyojun.choi" Date: Wed, 7 May 2025 11:21:26 +0900 Subject: [PATCH] =?UTF-8?q?roofPoints=20valid=20=EC=B2=B4=ED=81=AC=20?= =?UTF-8?q?=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 | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/util/qpolygon-utils.js b/src/util/qpolygon-utils.js index 131e9232..b1cf5ae1 100644 --- a/src/util/qpolygon-utils.js +++ b/src/util/qpolygon-utils.js @@ -294,7 +294,7 @@ function arePolygonsEqual(polygon1, polygon2) { } export function removeDuplicatePolygons(polygons) { - const uniquePolygons = [] + let uniquePolygons = [] polygons.forEach((polygon) => { const isDuplicate = uniquePolygons.some((uniquePolygon) => arePolygonsEqual(polygon, uniquePolygon)) @@ -304,12 +304,32 @@ export function removeDuplicatePolygons(polygons) { }) // x가 전부 같거나, y가 전부 같은 경우 제거 - return uniquePolygons.filter((polygon) => { - const xValues = polygon.map((point) => point.x) - const yValues = polygon.map((point) => point.y) - - return !(xValues.every((x) => x === xValues[0]) || yValues.every((y) => y === yValues[0])) + uniquePolygons = uniquePolygons.filter((polygon) => { + return isValidPoints(polygon) }) + + return uniquePolygons +} + +// 현재 point의 x와 이전 포인트의 x와 같을경우, 다음 포인트의 x와 달라야 함. +// 현재 point의 y와 이전 포인트의 y와 같을경우, 다음 포인트의 y와 달라야 함. +const isValidPoints = (points) => { + for (let i = 1; i < points.length - 1; i++) { + const prev = points[i - 1] + const curr = points[i] + const next = points[i + 1] + + // 현재와 이전의 x가 같다면 다음의 x는 달라야 함 + if (curr.x === prev.x && curr.x === next.x) { + return false + } + + // 현재와 이전의 y가 같다면 다음의 y는 달라야 함 + if (curr.y === prev.y && curr.y === next.y) { + return false + } + } + return true } export const isSamePoint = (a, b) => {