Skip to content

Commit

Permalink
change ES5 getter speed to getSpeed, #231
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed May 4, 2024
1 parent d7433b5 commit a02998b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
21 changes: 11 additions & 10 deletions js/common/model/Particle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,12 @@ export default class Particle {

public get vy(): number { return this._vy; }

/**
* Gets the particle's speed, the velocity magnitude, in pm/ps.
*/
public get speed(): number {
return Math.sqrt( this._vx * this._vx + this._vy * this._vy );
}

/**
* Gets the kinetic energy of this particle, in AMU * pm^2 / ps^2.
*/
public getKineticEnergy(): number {
return 0.5 * this._mass * this.speed * this.speed; // KE = (1/2) * m * |v|^2
const speed = this.getSpeed();
return 0.5 * this._mass * speed * speed; // KE = (1/2) * m * |v|^2
}

/**
Expand Down Expand Up @@ -228,11 +222,18 @@ export default class Particle {
}

/**
* Sets this particle's speed (velocity magnitude).
* Gets the particle's speed (velocity magnitude) in pm/ps.
*/
public getSpeed(): number {
return Math.sqrt( this._vx * this._vx + this._vy * this._vy );
}

/**
* Sets this particle's speed (velocity magnitude) in pm/ps.
*/
public setSpeed( speed: number ): void {
assert && assert( speed >= 0, `invalid magnitude: ${speed}` );
this.scaleVelocity( speed / this.speed );
this.scaleVelocity( speed / this.getSpeed() );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/energy/model/AverageSpeedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function getAverageSpeed( particles: Particle[] ): number {
if ( particles.length > 0 ) {
let totalSpeed = 0;
for ( let i = particles.length - 1; i >= 0; i-- ) {
totalSpeed += particles[ i ].speed;
totalSpeed += particles[ i ].getSpeed();
}
averageSpeed = totalSpeed / particles.length;
}
Expand Down
2 changes: 1 addition & 1 deletion js/energy/model/HistogramsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export default class HistogramsModel {
function getSpeedValues( particles: Particle[] ): number[] {
const values = [];
for ( let i = particles.length - 1; i >= 0; i-- ) {
values.push( particles[ i ].speed );
values.push( particles[ i ].getSpeed() );
}
return values;
}
Expand Down

0 comments on commit a02998b

Please sign in to comment.