diff --git a/src/engine/tools2d/ToolAngle.js b/src/engine/tools2d/ToolAngle.js index c8a0d03a..47b32cc8 100644 --- a/src/engine/tools2d/ToolAngle.js +++ b/src/engine/tools2d/ToolAngle.js @@ -14,6 +14,7 @@ // ********************************************** // import Modes2d from '../../store/Modes2d'; +import PointerChecker from '../utils/PointerChecker'; import ToolDistance from './ToolDistance'; // ********************************************** @@ -78,20 +79,36 @@ class ToolAngle { const vScr0 = ToolDistance.textureToScreen(objAngle.points[0].x, objAngle.points[0].y, this.m_wScreen, this.m_hScreen, store); const vScr1 = ToolDistance.textureToScreen(objAngle.points[1].x, objAngle.points[1].y, this.m_wScreen, this.m_hScreen, store); const vScr2 = ToolDistance.textureToScreen(objAngle.points[2].x, objAngle.points[2].y, this.m_wScreen, this.m_hScreen, store); - const MIN_DIST = 4.0; - if (this.getDistMm(vScr, vScr0) <= MIN_DIST) { - this.m_objEdit = objAngle; - return objAngle.points[0]; - } - if (this.getDistMm(vScr, vScr1) <= MIN_DIST) { + + const vScrLine1_S = vScr0; + const vScrLine1_E = vScr1; + const vScrLine2_S = vScr0; + const vScrLine2_E = vScr2; + + if (PointerChecker.isPointerOnLine(vScrLine1_S, vScrLine1_E, vScr)) { this.m_objEdit = objAngle; - return objAngle.points[1]; + return objAngle.points; } - if (this.getDistMm(vScr, vScr2) <= MIN_DIST) { + if (PointerChecker.isPointerOnLine(vScrLine2_S, vScrLine2_E, vScr)) { this.m_objEdit = objAngle; - return objAngle.points[2]; + return objAngle.points; } } + + // const MIN_DIST = 4.0; + // if (this.getDistMm(vScr, vScr0) <= MIN_DIST) { + // this.m_objEdit = objAngle; + // return objAngle.points[0]; + // } + // if (this.getDistMm(vScr, vScr1) <= MIN_DIST) { + // this.m_objEdit = objAngle; + // return objAngle.points[1]; + // } + // if (this.getDistMm(vScr, vScr2) <= MIN_DIST) { + // this.m_objEdit = objAngle; + // return objAngle.points[2]; + // } + return null; } diff --git a/src/engine/utils/PointerChecker.js b/src/engine/utils/PointerChecker.js new file mode 100644 index 00000000..285f0d3f --- /dev/null +++ b/src/engine/utils/PointerChecker.js @@ -0,0 +1,48 @@ +/* + * Copyright 2021 EPAM Systems, Inc. (https://www.epam.com/) + * SPDX-License-Identifier: Apache-2.0 + */ +/** + * This is a tool for checking whether a pointer is on the line + * @module demo/engine/utils/tool + */ +// absolute imports +/** + * Class PointerChecker to check if the point is on the line + * @class PointerChecker + */ +class PointerChecker { + static isPointerOnLine(vs, ve, vScr) { + // Define the center point of the line by "y" coordinate + const centerLineY = (vs.y + ve.y) / 2; + // Define the angle slope of the line + const m = (ve.y - vs.y) / (ve.x - vs.x); + // Define the intercept of the line + const b = vs.y - m * vs.x; + // Define the length of the line by "y" + const widthY = Math.abs(ve.y - vs.y); + // Define the distance to the line by "y" + const MIN_DIST_Y = 10; + // Check if the point is on the border of the line + if ( + vs.x != ve.x && + (vs.x < ve.x ? vScr.x >= vs.x && vScr.x <= ve.x : vScr.x <= vs.x && vScr.x >= ve.x) && + Math.floor(vScr.y) >= Math.floor(m * vScr.x + b) - MIN_DIST_Y && + Math.floor(vScr.y) <= Math.floor(m * vScr.x + b) + MIN_DIST_Y + ) { + return true; + } + // Check if the point is on the border of the vertical line + if ( + vs.x === ve.x && + vScr.x <= vs.x + 10 && + vScr.x >= vs.x - 10 && + vScr.y >= centerLineY - widthY / 2 && + vScr.y <= centerLineY + widthY / 2 + ) { + return true; + } + } +} + +export default PointerChecker;