diff --git a/src/components/fabric/QPolygon.js b/src/components/fabric/QPolygon.js index 7c52a0ad..0022b820 100644 --- a/src/components/fabric/QPolygon.js +++ b/src/components/fabric/QPolygon.js @@ -268,64 +268,70 @@ export const QPolygon = fabric.util.createClass(fabric.Polygon, { break } - const types = [] - this.lines.forEach((line) => types.push(line.attributes.type)) + const types = this.lines.map((line) => line.attributes.type) - const gableType = [LINE_TYPE.WALLLINE.GABLE, LINE_TYPE.WALLLINE.JERKINHEAD] - const isEaves = types.every((type) => type === LINE_TYPE.WALLLINE.EAVES) - - let isGable = false - if (types.includes(LINE_TYPE.WALLLINE.GABLE)) { - const gableOdd = types.filter((type, i) => i % 2 === 0) - const gableEven = types.filter((type, i) => i % 2 === 1) - if ( - (gableOdd.every((type) => type === LINE_TYPE.WALLLINE.EAVES) && gableEven.every((type) => gableType.includes(type))) || - (gableEven.every((type) => type === LINE_TYPE.WALLLINE.EAVES) && gableOdd.every((type) => gableType.includes(type))) - ) { - isGable = true + const isGableRoof = function (types) { + if (!types.includes(LINE_TYPE.WALLLINE.GABLE)) { + return false } + const gableTypes = [LINE_TYPE.WALLLINE.GABLE, LINE_TYPE.WALLLINE.JERKINHEAD] + const oddTypes = types.filter((type, i) => i % 2 === 0) + const evenTypes = types.filter((type, i) => i % 2 === 1) + + const oddAllEaves = oddTypes.every((type) => type === LINE_TYPE.WALLLINE.EAVES) + const evenAllGable = evenTypes.every((type) => gableTypes.includes(type)) + const evenAllEaves = evenTypes.every((type) => type === LINE_TYPE.WALLLINE.EAVES) + const oddAllGable = oddTypes.every((type) => gableTypes.includes(type)) + + return (oddAllEaves && evenAllGable) || (evenAllEaves && oddAllGable) } - let isShed = false - if (types.includes(LINE_TYPE.WALLLINE.SHED)) { - const sheds = this.lines.filter((line) => line.attributes !== undefined && line.attributes.type === LINE_TYPE.WALLLINE.SHED) - const areLinesParallel = function (line1, line2) { - const angle1 = calculateAngle(line1.startPoint, line1.endPoint) - const angle2 = calculateAngle(line2.startPoint, line2.endPoint) - return angle1 === angle2 + const isShedRoof = function (types, lines) { + const gableTypes = [LINE_TYPE.WALLLINE.GABLE, LINE_TYPE.WALLLINE.JERKINHEAD] + if (!types.includes(LINE_TYPE.WALLLINE.SHED)) { + return false + } + const shedLines = lines.filter((line) => line.attributes?.type === LINE_TYPE.WALLLINE.SHED) + const areShedLinesParallel = function (shedLines) { + return shedLines.every((shed, i) => { + const nextShed = shedLines[(i + 1) % shedLines.length] + const angle1 = calculateAngle(shed.startPoint, shed.endPoint) + const angle2 = calculateAngle(nextShed.startPoint, nextShed.endPoint) + return angle1 === angle2 + }) + } + if (!areShedLinesParallel(shedLines)) { + return false } - let isShedRoof = true - sheds.forEach((shed, i) => { - isShedRoof = areLinesParallel(shed, sheds[(i + 1) % sheds.length]) - }) - if (isShedRoof) { - const eaves = this.lines - .filter((line) => line.attributes !== undefined && line.attributes.type === LINE_TYPE.WALLLINE.EAVES) - .filter((line) => { - const angle1 = calculateAngle(sheds[0].startPoint, sheds[0].endPoint) - const angle2 = calculateAngle(line.startPoint, line.endPoint) - if (Math.abs(angle1 - angle2) === 180) { - return line - } - }) - if (eaves.length > 0) { - const gables = this.lines.filter((line) => sheds.includes(line) === false && eaves.includes(line) === false) - const isGable = gables.every((line) => gableType.includes(line.attributes.type)) - if (isGable) { - isShed = true - } - } + const getParallelEavesLines = function (shedLines, lines) { + const eavesLines = lines.filter((line) => line.attributes?.type === LINE_TYPE.WALLLINE.EAVES) + + const referenceAngle = calculateAngle(shedLines[0].startPoint, shedLines[0].endPoint) + + return eavesLines.filter((line) => { + const eavesAngle = calculateAngle(line.startPoint, line.endPoint) + return Math.abs(referenceAngle - eavesAngle) === 180 + }) } + + const parallelEaves = getParallelEavesLines(shedLines, lines) + if (parallelEaves.length === 0) { + return false + } + + const remainingLines = lines.filter((line) => !shedLines.includes(line) && !parallelEaves.includes(line)) + return remainingLines.every((line) => gableTypes.includes(line.attributes.type)) } - if (isEaves) { + if (types.every((type) => type === LINE_TYPE.WALLLINE.EAVES)) { // 용마루 -- straight-skeleton console.log('용마루 지붕') - } else if (isGable) { + } else if (isGableRoof(types)) { // A형, B형 박공 지붕 console.log('패턴 지붕') - } else if (isShed) { + isGableRoof(types) + } else if (isShedRoof(types, this.lines)) { console.log('한쪽흐름 지붕') drawShedRoof(this.id, this.canvas, textMode) } else {