Skip to content

Commit

Permalink
Minor cleanup and var names, see #191
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Nov 27, 2018
1 parent b8425a4 commit fa46635
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 33 deletions.
17 changes: 9 additions & 8 deletions js/common/model/IntensitySample.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ define( require => {
this.clear();
}


/**
* Gets the intensity values of the rightmost column in the visible wave area.
* @returns {number[]}
Expand All @@ -48,19 +47,21 @@ define( require => {
for ( let i = 0; i < this.history[ 0 ].length; i++ ) {
let sum = 0;
for ( let k = 0; k < this.history.length; k++ ) {
sum = sum + this.history[ k ][ i ] * this.history[ k ][ i ]; // squared for intensity, see https://physics.info/intensity/

// squared for intensity, see https://physics.info/intensity/
sum = sum + this.history[ k ][ i ] * this.history[ k ][ i ];
}
intensities.push( sum / this.history.length );
}

const result = [];
result.length = intensities.length;
result[ 0 ] = intensities[ 0 ];
result[ result.length - 1 ] = intensities[ result.length - 1 ];
const averagedIntensities = [];
averagedIntensities.length = intensities.length;
averagedIntensities[ 0 ] = intensities[ 0 ];
averagedIntensities[ averagedIntensities.length - 1 ] = intensities[ averagedIntensities.length - 1 ];
for ( let i = 1; i < intensities.length - 1; i++ ) {
result[ i ] = ( intensities[ i - 1 ] + intensities[ i ] + intensities[ i + 1 ] ) / 3;
averagedIntensities[ i ] = ( intensities[ i - 1 ] + intensities[ i ] + intensities[ i + 1 ] ) / 3;
}
return result;
return averagedIntensities;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions js/common/model/Lattice.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ define( require => {
const LIGHT_VISIT_THRESHOLD = 0.06;

// Damp more aggressively further from the edge of the visible lattice
window.damps = [ 0.999, 0.99, 0.98, 0.97, 0.95, 0.925, 0.9, 0.85, 0.8, 0.73, 0.66, 0.6, 0.55, 0.5, 0.45, 0.42, 0.4, 0.38, 0.36 ];
const DAMPING_PROFILE = [
0.999, 0.99, 0.98, 0.97, 0.95, 0.925, 0.9, 0.85, 0.8, 0.73, 0.66, 0.6, 0.55, 0.5, 0.45, 0.42, 0.4, 0.38, 0.36
];

class Lattice {

Expand Down Expand Up @@ -193,10 +195,9 @@ define( require => {
* @private
*/
decayVertical( i0, sign, width ) {

for ( let j = 0; j < this.height; j++ ) {
for ( let step = 0; step < width; step++ ) {
const damp = window.damps[ step ] || window.damps[ window.damps.length - 1 ];
const damp = DAMPING_PROFILE[ step ] || DAMPING_PROFILE[ DAMPING_PROFILE.length - 1 ];
const i = i0 + sign * step;
this.setCurrentValue( i, j, this.getCurrentValue( i, j ) * damp );
this.setLastValue( i, j, this.getLastValue( i, j ) * damp );
Expand All @@ -212,10 +213,9 @@ define( require => {
* @private
*/
decayHorizontal( j0, sign, height ) {

for ( let i = 0; i < this.width; i++ ) {
for ( let step = 0; step < height; step++ ) {
const damp = window.damps[ step ] || window.damps[ window.damps.length - 1 ];
const damp = DAMPING_PROFILE[ step ] || DAMPING_PROFILE[ DAMPING_PROFILE.length - 1 ];
const j = j0 + sign * step;
this.setCurrentValue( i, j, this.getCurrentValue( i, j ) * damp );
this.setLastValue( i, j, this.getLastValue( i, j ) * damp );
Expand Down
17 changes: 7 additions & 10 deletions js/common/model/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ define( require => {
validValues: WaveTemporalType.VALUES
} );


// The first button can trigger a pulse, or continuous wave, depending on the waveTemporalTypeProperty
this.button1PressedProperty.lazyLink( isPressed => {
if ( config.sceneType !== SceneType.WATER ) {
Expand Down Expand Up @@ -219,9 +218,6 @@ define( require => {
// @public {BooleanProperty} - true when the second source is continuously oscillating
this.continuousWave2OscillatingProperty = new BooleanProperty( false );

this.timeProperty.reset();
this.phase = 0;

// When frequency changes, choose a new phase such that the new sine curve has the same value and direction
// for continuity
const phaseUpdate = ( newFrequency, oldFrequency ) => {
Expand Down Expand Up @@ -273,7 +269,7 @@ define( require => {
// See setSourceValues
const frequency = this.frequencyProperty.get();
const wavelength = this.wavelength;
const k = Math.PI * 2 / wavelength;
const k = Math.PI * 2 / wavelength; // k is the wave number in sin(k*x-wt)
const angularFrequency = frequency * Math.PI * 2;
const x = this.modelToLatticeTransform.viewToModelX( this.lattice.dampX );

Expand Down Expand Up @@ -340,22 +336,22 @@ define( require => {

// Point source
if ( this.continuousWave1OscillatingProperty.get() || this.pulseFiringProperty.get() ) {
const j1 = latticeCenterJ + distanceAboveAxis;
lattice.setCurrentValue( WaveInterferenceConstants.POINT_SOURCE_HORIZONTAL_COORDINATE, j1, waveValue );
const j = latticeCenterJ + distanceAboveAxis;
lattice.setCurrentValue( WaveInterferenceConstants.POINT_SOURCE_HORIZONTAL_COORDINATE, j, waveValue );
this.oscillator1Property.value = waveValue;
}

// Secondary source (note if there is only one source, this sets the same value as above)
if ( this.continuousWave2OscillatingProperty.get() ) {
const j2 = latticeCenterJ - distanceAboveAxis;
lattice.setCurrentValue( WaveInterferenceConstants.POINT_SOURCE_HORIZONTAL_COORDINATE, j2, waveValue );
const j = latticeCenterJ - distanceAboveAxis;
lattice.setCurrentValue( WaveInterferenceConstants.POINT_SOURCE_HORIZONTAL_COORDINATE, j, waveValue );
this.oscillator2Property.value = waveValue;
}
}
}
else {
// plane waves

// plane waves
const lattice = this.lattice;

// Round this to make sure it appears at an integer cell column
Expand All @@ -374,6 +370,7 @@ define( require => {
const frequency = this.frequencyProperty.get();
const wavelength = this.wavelength;

// Solve for the wave number
// lambda * k = 2 * pi
// k = 2pi/lambda
const k = Math.PI * 2 / wavelength;
Expand Down
18 changes: 9 additions & 9 deletions js/common/model/SoundParticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ define( require => {

/**
* @param {number} i - horizontal lattice coordinate of the particle
* @param {number} k - vertical lattice coordinate of the particle
* @param {number} j - vertical lattice coordinate of the particle
* @param {number} x - initial x coordinate of the particle, in model coordinates
* @param {number} y - initial y coordinate of the particle, in model coordinates
*/
constructor( i, k, x, y ) {
constructor( i, j, x, y ) {

// @public {number} - horizontal lattice coordinate of the particle
this.i = i;

// @public {number} - vertical lattice coordinate of the particle
this.j = j;

// @public - x coordinate
this.x = x;
Expand All @@ -34,17 +40,11 @@ define( require => {
this.initialY = y;
this.vx = 0;
this.vy = 0;

// @public {number} - horizontal lattice coordinate of the particle
this.i = i;

// @public {number} - vertical lattice coordinate of the particle
this.k = k;
}

/**
* Applies a force toward the given point with the given strength;
* @param {number } fx - sum of applied forces in the x direction
* @param {number} fx - sum of applied forces in the x direction
* @param {number} fy - sum of applied forces in the y direction
* @param {number} dt - time to integrate
* @param {SoundScene} soundScene - to get the frequency range and value
Expand Down
2 changes: 1 addition & 1 deletion js/common/view/SoundParticleLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ define( require => {
paintCanvas( context ) {
context.transform( 1 / RESOLUTION, 0, 0, 1 / RESOLUTION, 0, 0 );
this.model.soundScene.soundParticles.forEach( soundParticle => {
const isRed = ( soundParticle.i % 4 === 2 && soundParticle.k % 4 === 2 );
const isRed = ( soundParticle.i % 4 === 2 && soundParticle.j % 4 === 2 );
const image = isRed ? this.redSphereImage : this.whiteSphereImage;
context.drawImage(
image,
Expand Down

0 comments on commit fa46635

Please sign in to comment.