void applyFreshParticleOffScreen ( @ NonNull final Scene scene , final int position ) { final int w = scene . getWidth ( ) ; final int h = scene . getHeight ( ) ; if ( w == 0 || h == 0 ) { throw new IllegalStateException ( "Cannot generate particles if scene width or height is 0" ) ; } float x = random . nextInt ( w ) ; float y = random . nextInt ( h ) ; // The offset to make when creating point of out bounds final short offset = ( short ) ( scene . getParticleRadiusMin ( ) + scene . getLineLength ( ) ) ; // Point angle range final float startAngle ; float endAngle ; // Make random offset and calulate angles so that the direction of travel will always be // towards our View switch ( random . nextInt ( 4 ) ) { case 0 : // offset to left x = ( short ) - offset ; startAngle = angleDeg ( pcc , pcc , x , y ) ; endAngle = angleDeg ( pcc , h - pcc , x , y ) ; break ; case 1 : // offset to top y = ( short ) - offset ; startAngle = angleDeg ( w - pcc , pcc , x , y ) ; endAngle = angleDeg ( pcc , pcc , x , y ) ; break ; case 2 : // offset to right x = ( short ) ( w + offset ) ; startAngle = angleDeg ( w - pcc , h - pcc , x , y ) ; endAngle = angleDeg ( w - pcc , pcc , x , y ) ; break ; case 3 : // offset to bottom y = ( short ) ( h + offset ) ; startAngle = angleDeg ( pcc , h - pcc , x , y ) ; endAngle = angleDeg ( w - pcc , h - pcc , x , y ) ; break ; default : throw new IllegalArgumentException ( "Supplied value out of range" ) ; } if ( endAngle < startAngle ) { endAngle += 360 ; } // Get random angle from angle range final float randomAngleInRange = startAngle + ( random . nextInt ( ( int ) Math . abs ( endAngle - startAngle ) ) ) ; final double direction = Math . toRadians ( randomAngleInRange ) ; final float dCos = ( float ) Math . cos ( direction ) ; final float dSin = ( float ) Math . sin ( direction ) ; final float speedFactor = newRandomIndividualParticleSpeedFactor ( ) ; final float radius = newRandomIndividualParticleRadius ( scene ) ; scene . setParticleData ( position , x , y , dCos , dSin , radius , speedFactor ) ; }
Set new particle coordinates somewhere off screen and apply new direction towards the screen