diff --git a/src/util/qpolygon-utils.js b/src/util/qpolygon-utils.js index 280245c6..d13a9e54 100644 --- a/src/util/qpolygon-utils.js +++ b/src/util/qpolygon-utils.js @@ -304,19 +304,9 @@ export function removeDuplicatePolygons(polygons) { } }) - //uniquePolygons중 isValidPoints의 조건을 만족하는 카운트 계산 - const validCount = uniquePolygons.reduce((acc, polygon) => { - if (!isValidPoints(polygon)) { - return acc + 1 - } - return acc - }, 0) - - if (validCount > 1) { - uniquePolygons = uniquePolygons.filter((polygon) => { - return isValidPoints(polygon) - }) - } + uniquePolygons = uniquePolygons.filter((polygon) => { + return isValidPoints(polygon) + }) return uniquePolygons } @@ -324,34 +314,43 @@ export function removeDuplicatePolygons(polygons) { // 현재 point의 x와 이전 포인트의 x와 같을경우, 다음 포인트의 x와 달라야 함. // 현재 point의 y와 이전 포인트의 y와 같을경우, 다음 포인트의 y와 달라야 함. const isValidPoints = (points) => { - let prev - let curr - let next - for (let i = 0; i < points.length; i++) { - if (i === 0) { - prev = points[points.length - 1] - curr = points[i] - next = points[i + 1] - } else if (i === points.length - 1) { - prev = points[i - 1] - curr = points[i] - next = points[0] - } else { - prev = points[i - 1] - curr = points[i] - next = points[i + 1] - } + function isColinear(p1, p2, p3) { + return (p2.x - p1.x) * (p3.y - p1.y) === (p3.x - p1.x) * (p2.y - p1.y) + } - // 현재와 이전의 x가 같다면 다음의 x는 달라야 함 - if (Math.abs(curr.x - prev.x) < 1 && Math.abs(curr.x - next.x) < 1) { + function segmentsOverlap(a1, a2, b1, b2) { + // 같은 직선 상에 있는가? + if (!isColinear(a1, a2, b1) || !isColinear(a1, a2, b2)) { return false } - // 현재와 이전의 y가 같다면 다음의 y는 달라야 함 - if (Math.abs(curr.y - prev.y) < 1 && Math.abs(curr.y - next.y) < 1) { + const isHorizontal = a1.y === a2.y + if (isHorizontal) { + const aMin = Math.min(a1.x, a2.x), + aMax = Math.max(a1.x, a2.x) + const bMin = Math.min(b1.x, b2.x), + bMax = Math.max(b1.x, b2.x) + return Math.max(aMin, bMin) < Math.min(aMax, bMax) + } else { + const aMin = Math.min(a1.y, a2.y), + aMax = Math.max(a1.y, a2.y) + const bMin = Math.min(b1.y, b2.y), + bMax = Math.max(b1.y, b2.y) + return Math.max(aMin, bMin) < Math.min(aMax, bMax) + } + } + + for (let i = 0; i < points.length - 2; i++) { + const a1 = points[i] + const a2 = points[i + 1] + const b1 = points[i + 1] // 연속되는 점 + const b2 = points[i + 2] + + if (segmentsOverlap(a1, a2, b1, b2)) { return false } } + return true }