Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
llienher committed Jul 18, 2019
1 parent 5738418 commit 041db45
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion contribs/gmf/src/editing/Snapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 15 additions & 24 deletions src/interaction/MeasureLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
});
}
Expand Down Expand Up @@ -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<number>} [result] An existing array where the result will be copied. If this parameter
* is undefined, a new array is created and returned.
* @returns {Array<number> | 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) {
Expand All @@ -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;
}
};
Expand Down

0 comments on commit 041db45

Please sign in to comment.