Skip to content

Commit

Permalink
Use Util.roundSymmetric, see #191
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Nov 28, 2018
1 parent 311c93d commit 92fc7ec
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
3 changes: 2 additions & 1 deletion js/common/model/Lattice.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define( require => {
const Bounds2 = require( 'DOT/Bounds2' );
const Emitter = require( 'AXON/Emitter' );
const Matrix = require( 'DOT/Matrix' );
const Util = require( 'DOT/Util' );
const waveInterference = require( 'WAVE_INTERFERENCE/waveInterference' );

// constants
Expand Down Expand Up @@ -127,7 +128,7 @@ define( require => {
if ( array.length !== samplingWidth ) {
array.length = 0;
}
const samplingVerticalLocation = Math.round( this.height / 2 );
const samplingVerticalLocation = Util.roundSymmetric( this.height / 2 );
for ( let i = 0; i < this.width - this.dampX * 2; i++ ) {
array[ i ] = this.getCurrentValue( i + this.dampX, samplingVerticalLocation );
}
Expand Down
11 changes: 6 additions & 5 deletions js/common/model/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ define( require => {
const SceneType = require( 'WAVE_INTERFERENCE/common/model/SceneType' );
const StringUtils = require( 'PHETCOMMON/util/StringUtils' );
const Vector2 = require( 'DOT/Vector2' );
const Util = require( 'DOT/Util' );
const waveInterference = require( 'WAVE_INTERFERENCE/waveInterference' );
const WaveInterferenceConstants = require( 'WAVE_INTERFERENCE/common/WaveInterferenceConstants' );
const WaveSpatialType = require( 'WAVE_INTERFERENCE/common/model/WaveSpatialType' );
Expand Down Expand Up @@ -299,7 +300,7 @@ define( require => {

const frontTime = this.timeProperty.value - this.button1PressTime;
const frontPosition = this.modelToLatticeTransform.modelToViewX( this.waveSpeed * frontTime );
const barrierLatticeX = Math.round( this.modelToLatticeTransform.modelToViewX( this.getBarrierLocation() ) );
const barrierLatticeX = Util.roundSymmetric( this.modelToLatticeTransform.modelToViewX( this.getBarrierLocation() ) );

// if the wave had passed by the barrier, then repropagate from the barrier. This requires back-computing the
// time the button would have been pressed to propagate the wave to the barrier. Hence this is the inverse of
Expand Down Expand Up @@ -342,13 +343,13 @@ define( require => {
// assumes a square lattice
const sourceSeparation = this.sourceSeparationProperty.get();
const separationInLatticeUnits = sourceSeparation / this.waveAreaWidth * this.lattice.visibleBounds.width;
const distanceAboveAxis = Math.round( separationInLatticeUnits / 2 );
const distanceAboveAxis = Util.roundSymmetric( separationInLatticeUnits / 2 );

// Named with a "J" suffix instead of "Y" to remind us we are working in integral (i,j) lattice coordinates.
// To understand why we subtract 1 here, imagine for the sake of conversation that the lattice is 5 units wide
// so the cells are indexed 0,1,2,3,4. 5/2 === 2.5, rounded up that is 3, so we must subtract 1 to find the
// center of the lattice.
const latticeCenterJ = Math.round( this.lattice.height / 2 ) - 1;
const latticeCenterJ = Util.roundSymmetric( this.lattice.height / 2 ) - 1;

// Point source
if ( this.continuousWave1OscillatingProperty.get() || this.pulseFiringProperty.get() ) {
Expand All @@ -371,14 +372,14 @@ define( require => {
const lattice = this.lattice;

// Round this to make sure it appears at an integer cell column
let barrierLatticeX = Math.round( this.modelToLatticeTransform.modelToViewX( this.getBarrierLocation() ) );
let barrierLatticeX = Util.roundSymmetric( this.modelToLatticeTransform.modelToViewX( this.getBarrierLocation() ) );
const slitSeparationModel = this.slitSeparationProperty.get();

const frontTime = time - this.button1PressTime;
const frontPosition = this.modelToLatticeTransform.modelToViewX( this.waveSpeed * frontTime );

const slitWidthModel = this.slitWidthProperty.get();
const slitWidth = Math.round( this.modelToLatticeTransform.modelToViewDeltaY( slitWidthModel ) );
const slitWidth = Util.roundSymmetric( this.modelToLatticeTransform.modelToViewDeltaY( slitWidthModel ) );
const latticeCenterY = this.lattice.height / 2;

// Take the desired frequency for the water scene, or the specified frequency of any other scene
Expand Down
4 changes: 2 additions & 2 deletions js/common/model/SoundParticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ define( require => {
// @public {number} - vertical lattice coordinate of the particle
this.j = j;

// @public - x coordinate
// @public {number} - x coordinate
this.x = x;

// @public - y coordinate
// @public {number} - y coordinate
this.y = y;

// @private
Expand Down
6 changes: 3 additions & 3 deletions js/common/model/SoundScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ define( require => {
constructor( showSoundParticles, config ) {
super( config );

// @public (read-only) true if SoundParticles should be created and displayed. They are not displayed
// @public (read-only) {boolean} - true if SoundParticles should be created and displayed. They are not displayed
// on the Slits screen, see https://github.com/phetsims/wave-interference/issues/109
this.showSoundParticles = showSoundParticles;

Expand Down Expand Up @@ -82,8 +82,8 @@ define( require => {

// Find the lattice coordinate of the current location of the particle
const latticeCoordinate = this.modelToLatticeTransform.modelToViewXY( soundParticle.x, soundParticle.y );
const latticeX = Math.round( latticeCoordinate.x );
const latticeY = Math.round( latticeCoordinate.y );
const latticeX = Util.roundSymmetric( latticeCoordinate.x );
const latticeY = Util.roundSymmetric( latticeCoordinate.y );

// Estimate the numerical gradient in the neighborhood of the particle
// https://en.wikipedia.org/wiki/Pressure-gradient_force
Expand Down
6 changes: 6 additions & 0 deletions js/common/view/LightScreenNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ define( require => {
// Use a single column of pixels, then stretch them to the right (since that is a constant)
const width = 1;
const height = this.lattice.height - this.lattice.dampY * 2;

// @private
this.directCanvas = document.createElement( 'canvas' );
this.directCanvas.width = width;
this.directCanvas.height = height;

// @private
this.directContext = this.directCanvas.getContext( '2d' );

// @private
this.imageData = this.directContext.createImageData( width, height );

// Invalidate paint when model indicates changes
Expand Down
2 changes: 1 addition & 1 deletion js/diffraction/view/DiffractionScreenView.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ define( require => {
const idxInPixels = 4 * ( dims[ 0 ] * k + l );
currImageData.data[ idxInPixels + 3 ] = 255; // full alpha
let color = Math.log( cc * $h( l, k ).magnitude() + 1 );
color = Math.round( 255 * ( color / logOfMaxMag ) );
color = Util.roundSymmetric( 255 * ( color / logOfMaxMag ) );
// RGB are the same -> gray
for ( let c = 0; c < 3; c++ ) { // lol c++
currImageData.data[ idxInPixels + c ] = color;
Expand Down

0 comments on commit 92fc7ec

Please sign in to comment.