From 9b0fd8892907a596f208d5b72aa1df7adb77bd66 Mon Sep 17 00:00:00 2001 From: Luisav1 Date: Tue, 1 Aug 2023 09:23:37 -0600 Subject: [PATCH] Create function to extract particle closest to atom center. See https://github.com/phetsims/build-a-nucleus/issues/97. --- js/model/ParticleAtom.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/js/model/ParticleAtom.ts b/js/model/ParticleAtom.ts index 2ba7ad8..987b889 100644 --- a/js/model/ParticleAtom.ts +++ b/js/model/ParticleAtom.ts @@ -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. */