From 418fc5073dac509e47e532f9b3c229557bdbf5a0 Mon Sep 17 00:00:00 2001 From: "hyojun.choi" Date: Wed, 17 Jul 2024 13:07:26 +0900 Subject: [PATCH] =?UTF-8?q?QPolygon=20isValid=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/fabric/QPolygon.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/components/fabric/QPolygon.js b/src/components/fabric/QPolygon.js index bfc133e4..512149f3 100644 --- a/src/components/fabric/QPolygon.js +++ b/src/components/fabric/QPolygon.js @@ -199,6 +199,32 @@ export default class QPolygon extends fabric.Group { this.canvas.renderAll() } + /** + * this.lines의 direction이 top 인 line의 모든 합이 bottom 인 line의 모든 합과 같은지 확인 + * this.lines의 direction이 left 인 line의 모든 합이 right 인 line의 모든 합과 같은지 확인 + * return {boolean} + */ + isValid() { + const leftLinesLengthSum = this.lines + .filter((line) => line.direction === 'left') + .reduce((sum, line) => sum + line.length, 0) + const rightLinesLengthSum = this.lines + .filter((line) => line.direction === 'right') + .reduce((sum, line) => sum + line.length, 0) + + const topLinesLengthSum = this.lines + .filter((line) => line.direction === 'top') + .reduce((sum, line) => sum + line.length, 0) + const bottomLinesLengthSum = this.lines + .filter((line) => line.direction === 'bottom') + .reduce((sum, line) => sum + line.length, 0) + + return ( + leftLinesLengthSum === rightLinesLengthSum && + topLinesLengthSum === bottomLinesLengthSum + ) + } + inPolygon(point) { const vertices = this.getCurrentPoints() let intersects = 0 @@ -302,6 +328,10 @@ export default class QPolygon extends fabric.Group { // 보조선 그리기 사각형에서만 drawHelpLine(chon = 4) { + if (!this.isValid()) { + return + } + if (this.lines.length === 4) { this.#drawHelpLineInRect(chon) } else if (this.lines.length === 6) {