From 041db45a84896e3a66601081f58241f1247c3800 Mon Sep 17 00:00:00 2001 From: Laurent Lienher Date: Wed, 17 Jul 2019 13:22:32 +0200 Subject: [PATCH] Simplify code --- contribs/gmf/src/editing/Snapping.js | 2 +- src/interaction/MeasureLength.js | 39 +++++++++++----------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/contribs/gmf/src/editing/Snapping.js b/contribs/gmf/src/editing/Snapping.js index 6d247b99add8..bbb8829f4421 100644 --- a/contribs/gmf/src/editing/Snapping.js +++ b/contribs/gmf/src/editing/Snapping.js @@ -121,7 +121,7 @@ class CustomSnap extends olInteractionSnap { super(options); this.modifierPressed = false; document.body.addEventListener('keydown', (evt) => { - this.modifierPressed = evt.keyCode === 17; + this.modifierPressed = evt.keyCode === 17; // Ctrl key }); document.body.addEventListener('keyup', () => { this.modifierPressed = false; diff --git a/src/interaction/MeasureLength.js b/src/interaction/MeasureLength.js index 21cccdebf3de..895c1830fd99 100644 --- a/src/interaction/MeasureLength.js +++ b/src/interaction/MeasureLength.js @@ -31,7 +31,7 @@ const exports = function(format, gettextCatalog, options = /** @type {ngeox.inte if (modifierPressed === undefined) { modifierPressed = false; document.body.addEventListener('keydown', (evt) => { - modifierPressed = evt.keyCode === 17; + modifierPressed = evt.keyCode === 17; // Ctrl key }); document.body.addEventListener('keyup', () => { modifierPressed = false; @@ -124,15 +124,7 @@ exports.prototype.linestringGeometryFunction = function(source, coordinates, opt to[1] = b[1]; // intersection calculation - const result = { - x: 0.0, - y: 0.0 - }; - const intersectionPoint = this.computeLineSegmentIntersection(from[0], from[1], to[0], to[1], point1[0], point1[1], point2[0], point2[1], result); - if (intersectionPoint !== undefined) { - to[0] = intersectionPoint.x; - to[1] = intersectionPoint.y; - } + this.computeLineSegmentIntersection(from, to, point1, point2, to); }); }); } @@ -162,22 +154,21 @@ exports.prototype.handleMeasure = function(callback) { /** * Compute the intersection between 2 segments * - * @param {Number} x00 The x coordinate of the first line's first vertex. - * @param {Number} y00 The y coordinate of the first line's first vertex. - * @param {Number} x01 The x coordinate of the first line's second vertex. - * @param {Number} y01 The y coordinate of the first line's second vertex. - * @param {Number} x10 The x coordinate of the second line's first vertex. - * @param {Number} y10 The y coordinate of the second line's first vertex. - * @param {Number} x11 The x coordinate of the second line's second vertex. - * @param {Number} y11 The y coordinate of the second line's second vertex. + * @param {Number} line1vertex1 The coordinates of the first line's first vertex. + * @param {Number} line1vertex2 The coordinates of the first line's second vertex. + * @param {Number} line2vertex1 The coordinates of the second line's first vertex. + * @param {Number} line2vertex2 The coordinates of the second line's second vertex. * @param {Array} [result] An existing array where the result will be copied. If this parameter * is undefined, a new array is created and returned. * @returns {Array | undefined} The intersection point, undefined if there is no intersection point or lines are coincident. */ -exports.prototype.computeLineSegmentIntersection = function(x00, y00, x01, y01, x10, y10, x11, y11, result) { - const numerator1A = (x11 - x10) * (y00 - y10) - (y11 - y10) * (x00 - x10); - const numerator1B = (x01 - x00) * (y00 - y10) - (y01 - y00) * (x00 - x10); - const denominator1 = (y11 - y10) * (x01 - x00) - (x11 - x10) * (y01 - y00); +exports.prototype.computeLineSegmentIntersection = function(line1vertex1, line1vertex2, line2vertex1, line2vertex2, result) { + const numerator1A = (line2vertex2[0] - line2vertex1[0]) * (line1vertex1[1] - line2vertex1[1]) + - (line2vertex2[1] - line2vertex1[1]) * (line1vertex1[0] - line2vertex1[0]); + const numerator1B = (line1vertex2[0] - line1vertex1[0]) * (line1vertex1[1] - line2vertex1[1]) + - (line1vertex2[1] - line1vertex1[1]) * (line1vertex1[0] - line2vertex1[0]); + const denominator1 = (line2vertex2[1] - line2vertex1[1]) * (line1vertex2[0] - line1vertex1[0]) + - (line2vertex2[0] - line2vertex1[0]) * (line1vertex2[1] - line1vertex1[1]); // If denominator = 0, then lines are parallel. If denominator = 0 and both numerators are 0, then coincident if (denominator1 === 0) { @@ -188,8 +179,8 @@ exports.prototype.computeLineSegmentIntersection = function(x00, y00, x01, y01, const ub1 = numerator1B / denominator1; if (ua1 >= 0 && ua1 <= 1 && ub1 >= 0 && ub1 <= 1) { - result.x = x00 + ua1 * (x01 - x00); - result.y = y00 + ua1 * (y01 - y00); + result[0] = line1vertex1[0] + ua1 * (line1vertex2[0] - line1vertex1[0]); + result[1] = line1vertex1[1] + ua1 * (line1vertex2[1] - line1vertex1[1]); return result; } };