diff --git a/config/changelog.yaml b/config/changelog.yaml index 2b363caca4a..02ae4a2b8b7 100644 --- a/config/changelog.yaml +++ b/config/changelog.yaml @@ -1,3 +1,11 @@ +unreleased: + date: 2021-08-01 + + Fixed: + core: + - Fixed edge case in utils.beamsIntersect() when line is almost vertical + See [#1206](https://github.com/freesewing/freesewing/issues/1206) + 2.17.0: date: 2021-07-01 diff --git a/packages/core/src/utils.js b/packages/core/src/utils.js index f2ddf1a7f1d..ee8d50587b9 100644 --- a/packages/core/src/utils.js +++ b/packages/core/src/utils.js @@ -22,12 +22,15 @@ export function macroName(name) { export function beamsIntersect(a1, a2, b1, b2) { let slopeA = a1.slope(a2) let slopeB = b1.slope(b2) + console.log({a1, a2, slopeA}) if (slopeA === slopeB) return false // Parallel lines - if (a1.x === a2.x) return new Point(a1.x, slopeB * a1.x + (b1.y - slopeB * b1.x)) - // Vertical line A - else if (b1.x === b2.x) return new Point(b1.x, slopeA * b1.x + (a1.y - slopeA * a1.x)) - // Vertical line B + // Check for vertical line A + if (Math.round(a1.x*10000) === Math.round(a2.x*10000)) + return new Point(a1.x, slopeB * a1.x + (b1.y - slopeB * b1.x)) + // Check for vertical line B + else if (Math.round(b1.x*10000) === Math.round(b2.x*10000)) + return new Point(b1.x, slopeA * b1.x + (a1.y - slopeA * a1.x)) else { // Swap points if line A or B goes from right to left if (a1.x > a2.x) a1 = a2.copy() diff --git a/packages/core/tests/utils.test.js b/packages/core/tests/utils.test.js index 429beb22f90..05ba37328bf 100644 --- a/packages/core/tests/utils.test.js +++ b/packages/core/tests/utils.test.js @@ -490,3 +490,12 @@ it("Should find where a curve intersects a given Y-value", () => { expect(round(i.x)).to.equal(39.49); expect(round(i.y)).to.equal(30); }); + +// Recreate issue #1206 +it("Should find intersecting beams when a line is almost vertical", () => { + let a = new freesewing.Point(225.72, 241); + let b = new freesewing.Point(225.71999999999997, 600); + let i = utils.beamIntersectsY(a, b, 400); + expect(round(i.y)).to.equal(400); +}); +