diff --git a/src/util/qpolygon-utils.js b/src/util/qpolygon-utils.js index 65cbc96d..a574c393 100644 --- a/src/util/qpolygon-utils.js +++ b/src/util/qpolygon-utils.js @@ -1380,22 +1380,27 @@ export const splitPolygonWithLines = (polygon) => { }) } -const removeDuplicatePolygons = (polygons) => { +function normalizePoint(point) { + return { + x: Math.round(point.x), + y: Math.round(point.y), + } +} + +function arePolygonsEqual(polygon1, polygon2) { + if (polygon1.length !== polygon2.length) return false + + const normalizedPolygon1 = polygon1.map(normalizePoint).sort((a, b) => a.x - b.x || a.y - b.y) + const normalizedPolygon2 = polygon2.map(normalizePoint).sort((a, b) => a.x - b.x || a.y - b.y) + + return normalizedPolygon1.every((point, index) => arePointsEqual(point, normalizedPolygon2[index])) +} + +function removeDuplicatePolygons(polygons) { const uniquePolygons = [] polygons.forEach((polygon) => { - const sortedPolygon = polygon - .map((point) => `${Math.floor(point.x)},${Math.floor(point.y)}`) - .sort() - .join('|') - const isDuplicate = uniquePolygons.some((uniquePolygon) => { - const sortedUniquePolygon = uniquePolygon - .map((point) => `${Math.floor(point.x)},${Math.floor(point.y)}`) - .sort() - .join('|') - return sortedPolygon === sortedUniquePolygon - }) - + const isDuplicate = uniquePolygons.some((uniquePolygon) => arePolygonsEqual(polygon, uniquePolygon)) if (!isDuplicate) { uniquePolygons.push(polygon) } @@ -3551,7 +3556,7 @@ function createRoofPaddingPolygon(polygon, lines, arcSegments = 0) { } function arePointsEqual(point1, point2) { - return Math.abs(point1.x - point2.x) < 1 && Math.abs(point1.y - point2.y) - 1 + return Math.abs(point1.x - point2.x) <= 1 && Math.abs(point1.y - point2.y) <= 1 } function arraysHaveSamePoints(array1, array2) {