validCheck 수정

This commit is contained in:
hyojun.choi 2025-05-13 10:55:58 +09:00
parent 72c020acea
commit c52d914c6e

View File

@ -304,19 +304,9 @@ export function removeDuplicatePolygons(polygons) {
} }
}) })
//uniquePolygons중 isValidPoints의 조건을 만족하는 카운트 계산 uniquePolygons = uniquePolygons.filter((polygon) => {
const validCount = uniquePolygons.reduce((acc, polygon) => { return isValidPoints(polygon)
if (!isValidPoints(polygon)) { })
return acc + 1
}
return acc
}, 0)
if (validCount > 1) {
uniquePolygons = uniquePolygons.filter((polygon) => {
return isValidPoints(polygon)
})
}
return uniquePolygons return uniquePolygons
} }
@ -324,34 +314,43 @@ export function removeDuplicatePolygons(polygons) {
// 현재 point의 x와 이전 포인트의 x와 같을경우, 다음 포인트의 x와 달라야 함. // 현재 point의 x와 이전 포인트의 x와 같을경우, 다음 포인트의 x와 달라야 함.
// 현재 point의 y와 이전 포인트의 y와 같을경우, 다음 포인트의 y와 달라야 함. // 현재 point의 y와 이전 포인트의 y와 같을경우, 다음 포인트의 y와 달라야 함.
const isValidPoints = (points) => { const isValidPoints = (points) => {
let prev function isColinear(p1, p2, p3) {
let curr return (p2.x - p1.x) * (p3.y - p1.y) === (p3.x - p1.x) * (p2.y - p1.y)
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]
}
// 현재와 이전의 x가 같다면 다음의 x는 달라야 함 function segmentsOverlap(a1, a2, b1, b2) {
if (Math.abs(curr.x - prev.x) < 1 && Math.abs(curr.x - next.x) < 1) { // 같은 직선 상에 있는가?
if (!isColinear(a1, a2, b1) || !isColinear(a1, a2, b2)) {
return false return false
} }
// 현재와 이전의 y가 같다면 다음의 y는 달라야 함 const isHorizontal = a1.y === a2.y
if (Math.abs(curr.y - prev.y) < 1 && Math.abs(curr.y - next.y) < 1) { 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 false
} }
} }
return true return true
} }