Code and tests
This commit is contained in:
parent
62fb6bc229
commit
68c0fca124
3 changed files with 58 additions and 0 deletions
|
@ -13,6 +13,7 @@ import {
|
|||
beamIntersectsX,
|
||||
beamIntersectsY,
|
||||
beamsIntersect,
|
||||
beamIntersectsCurve,
|
||||
capitalize,
|
||||
circlesIntersect,
|
||||
curveEdge,
|
||||
|
@ -55,6 +56,7 @@ export {
|
|||
beamIntersectsX,
|
||||
beamIntersectsY,
|
||||
beamsIntersect,
|
||||
beamIntersectsCurve,
|
||||
capitalize,
|
||||
circlesIntersect,
|
||||
curveEdge,
|
||||
|
|
|
@ -111,6 +111,25 @@ export function beamsIntersect(a1, a2, b1, b2) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the intersections between an endless line (beam) and a curve
|
||||
*
|
||||
*
|
||||
* @param {Point} start - Start Point of the line
|
||||
* @param {Point} end - End Point of the line
|
||||
* @param {Point} from - Start Point of the curve
|
||||
* @param {Point} cp1 - Control Point at the start of the curve
|
||||
* @param {Point} cp2 - Control Point at the end of the curve
|
||||
* @param {Point} to - End Point of the curve
|
||||
* @return {Array} intersections - An array of Points at the intersections
|
||||
*/
|
||||
export function beamIntersectsCurve(start, end, from, cp1, cp2, to) {
|
||||
let _start = new Point(start.x + (start.x - end.x) * 1000, start.y + (start.y - end.y) * 1000)
|
||||
let _end = new Point(end.x + (end.x - start.x) * 1000, end.y + (end.y - start.y) * 1000)
|
||||
console.log({ _start: _start, _end: _end })
|
||||
return lineIntersectsCurve(_start, _end, from, cp1, cp2, to)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string you pass with with the first character converted to uppercase
|
||||
*
|
||||
|
|
|
@ -11,6 +11,7 @@ import {
|
|||
splitCurve,
|
||||
beamIntersectsX,
|
||||
beamIntersectsY,
|
||||
beamIntersectsCurve,
|
||||
units,
|
||||
lineIntersectsCurve,
|
||||
curveIntersectsX,
|
||||
|
@ -70,6 +71,42 @@ describe('Utils', () => {
|
|||
expect(round(X.x)).to.equal(7.14)
|
||||
expect(round(X.y)).to.equal(40)
|
||||
})
|
||||
it('Should find no intersections between a curve and a beam', () => {
|
||||
let A = new Point(10, 10)
|
||||
let Acp = new Point(10, 30)
|
||||
let B = new Point(110, 10)
|
||||
let Bcp = new Point(110, 30)
|
||||
let E = new Point(10, 40)
|
||||
let D = new Point(20, 40)
|
||||
|
||||
let hit = beamIntersectsCurve(E, D, A, Acp, Bcp, B)
|
||||
expect(hit).to.equal(false)
|
||||
})
|
||||
|
||||
it('Should find one intersections between a curve and a beam', () => {
|
||||
let A = new Point(10, 10)
|
||||
let Acp = new Point(10, 30)
|
||||
let B = new Point(110, 10)
|
||||
let Bcp = new Point(110, 30)
|
||||
let E = new Point(50, 14)
|
||||
let D = new Point(55, 16)
|
||||
|
||||
let hit = beamIntersectsCurve(E, D, A, Acp, Bcp, B)
|
||||
expect(round(hit.x)).to.equal(75.79)
|
||||
expect(round(hit.y)).to.equal(24.31)
|
||||
})
|
||||
|
||||
it('Should find two intersections between a curve and a beam', () => {
|
||||
let A = new Point(10, 10)
|
||||
let Acp = new Point(10, 30)
|
||||
let B = new Point(110, 10)
|
||||
let Bcp = new Point(110, 30)
|
||||
let E = new Point(0, 14)
|
||||
let D = new Point(5, 15)
|
||||
|
||||
let hits = beamIntersectsCurve(E, D, A, Acp, Bcp, B)
|
||||
expect(hits.length).to.equal(2)
|
||||
})
|
||||
|
||||
it("Should return false when two lines don't intersect", () => {
|
||||
let a = new Point(10, 20)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue