Skip to content

Commit

Permalink
Factor out duplicated code, see #276
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Dec 21, 2018
1 parent b820c67 commit e664180
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions js/slits/view/TheoryInterferenceOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ define( require => {
// Render all the minima and maxima on both sides of the origin
[ 'maxima', 'minima' ].forEach( type => {
[ -1, 1 ].forEach( sign => {

/**
* Adds a line for the given maximum or minimum
* @param {number} arg - argument to the arcsin
*/
const addLine = arg => {
const theta = sign * Math.asin( arg );

const x = LENGTH * Math.cos( theta );
const y = LENGTH * Math.sin( theta );
const line = new Line( barrierX, barrierY, barrierX + x, barrierY + y, {
stroke: type === 'maxima' ? MAXIMUM_COLOR : MINIMUM_COLOR,
lineWidth: LINE_WIDTH
} );
this.addChild( line );
};

// Limit the maximum number of lines that can be shown on each side.
for ( let m = 0; m < 20; m++ ) {

Expand All @@ -63,15 +80,7 @@ define( require => {

// make sure in bounds
if ( arg <= 1 ) {
const theta = sign * Math.asin( arg );

const x = LENGTH * Math.cos( theta );
const y = LENGTH * Math.sin( theta );
const line = new Line( barrierX, barrierY, barrierX + x, barrierY + y, {
stroke: type === 'maxima' ? MAXIMUM_COLOR : MINIMUM_COLOR,
lineWidth: LINE_WIDTH
} );
this.addChild( line );
addLine( arg );
}
}

Expand All @@ -86,15 +95,7 @@ define( require => {

// make sure in bounds. Single slit begins at m=1
if ( arg <= 1 && m > 0 ) {
const theta = sign * Math.asin( arg );

const x = LENGTH * Math.cos( theta );
const y = LENGTH * Math.sin( theta );
const line = new Line( barrierX, barrierY, barrierX + x, barrierY + y, {
stroke: type === 'maxima' ? MAXIMUM_COLOR : MINIMUM_COLOR,
lineWidth: LINE_WIDTH
} );
this.addChild( line );
addLine( arg );
}
}
}
Expand Down

0 comments on commit e664180

Please sign in to comment.