Skip to content

Commit

Permalink
Create function to extract particle closest to atom center. See phets…
Browse files Browse the repository at this point in the history
  • Loading branch information
Luisav1 committed Aug 1, 2023
1 parent 0329c95 commit 9b0fd88
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions js/model/ParticleAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,41 @@ class ParticleAtom extends PhetioObject {
return particle as unknown as Particle;
}

public extractParticleClosestToCenter( particleType: ParticleTypeString ): Particle {
let particle = null;
switch( particleType ) {
case 'proton':
if ( this.protons.length > 0 ) {
particle = _.sortBy( [ ...this.protons ], proton =>
proton.positionProperty.value.distance( this.positionProperty.value ) )[ 0 ];
}
break;

case 'neutron':
if ( this.neutrons.length > 0 ) {
particle = _.sortBy( [ ...this.neutrons ], neutron =>
neutron.positionProperty.value.distance( this.positionProperty.value ) )[ 0 ];
}
break;

case 'electron':
if ( this.electrons.length > 0 ) {
particle = _.sortBy( [ ...this.electrons ], electron =>
electron.positionProperty.value.distance( this.positionProperty.value ) )[ 0 ];
}
break;

default:
throw new Error( 'Attempt to remove unknown particle type.' );
}

if ( particle !== null ) {
this.removeParticle( particle );
}

return particle as unknown as Particle;
}

/**
* Remove all the particles but don't reconfigure the nucleus as they go. This makes it a quicker operation.
*/
Expand Down

0 comments on commit 9b0fd88

Please sign in to comment.