00001
00002 import java.awt.AlphaComposite;
00003 import java.awt.Color;
00004 import java.awt.Font;
00005 import java.awt.Graphics;
00006 import java.awt.Graphics2D;
00007 import java.awt.GraphicsConfiguration;
00008 import java.awt.RadialGradientPaint;
00009 import java.awt.Transparency;
00010 import java.awt.MultipleGradientPaint.CycleMethod;
00011 import java.awt.geom.Ellipse2D;
00012 import java.awt.geom.Point2D;
00013 import java.awt.image.BufferedImage;
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class Particle implements Runnable
00038 {
00039 private final static double k_e = 5e6;
00040 private final static double k_G = 1e-5;
00041 private final static double k_x = 1e-4;
00042 private final static double minRsq = 91;
00043 private final static double maxVsq = 4e4;
00044
00045
00046
00047
00048 private WorldOfParticles world;
00049
00050
00051
00052
00053 private double mass = 1;
00054 private double charge = 1;
00055 private double radius = 1;
00056
00057
00058
00059 private double xPos = 0;
00060 private double yPos = 0;
00061 private double vx = 0;
00062 private double vy = 0;
00063 private double vsq = 0;
00064 private double ax = 0;
00065 private double ay = 0;
00066 private double asq = 0;
00067 private double Fx = 0;
00068 private double Fy = 0;
00069
00070
00071
00072
00073
00074 private double lifeTime = Double.POSITIVE_INFINITY;
00075
00076
00077
00078
00079
00080 private Thread mainThread;
00081
00082
00083
00084
00085
00086
00087 private BufferedImage image = null;
00088
00089
00090
00091
00092 private Color color = Color.BLUE;
00093
00094
00095
00096
00097 private int aliasedEdges = 10;
00098
00099
00100
00101
00102 private boolean showTrail = false;
00103 private int trailSize = 0;
00104 private int trailRatioC = 0;
00105 private int trailRatio = 0;
00106 private int[] trailX = null;
00107 private int[] trailY = null;
00108 private float[] trailFade = null;
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 public Particle( WorldOfParticles parent,
00124 double M, double Q, double X, double Y, double R, double T
00125 )
00126 {
00127 world = parent;
00128 mass = M;
00129 charge = Q;
00130 lifeTime = T;
00131
00132
00133
00134 xPos = X;
00135 yPos = Y;
00136
00137
00138
00139 vx = -100 + 200 * Math.random ();
00140 vy = -100 + 200 * Math.random ();
00141 vsq = Math.pow( vx, 2 ) + Math.pow( vy, 2 );
00142
00143
00144
00145 ax = 0;
00146 ay = 0;
00147 asq = 0;
00148
00149
00150
00151 radius = R > 0 ? R : 10.0 + ( 10.0 - R ) * Math.random ();
00152
00153
00154
00155 if ( charge == 0 ) {
00156 color = new Color( 0, 0, 1f );
00157 } else if ( charge > 0 ) {
00158 color = new Color( (float)charge, 0, 0 );
00159 } else {
00160 color = new Color( 0, -(float)charge, 0 );
00161 }
00162
00163
00164
00165 initBlurTrail( 10 );
00166
00167
00168
00169 mainThread = new Thread( this );
00170 mainThread.start ();
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 public double getX () { return xPos; }
00180
00181
00182
00183
00184
00185 public double getY () { return yPos; }
00186
00187
00188
00189
00190
00191 public double getKineticEnergy ()
00192 {
00193 return mass * vsq / 2;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203 private void initBlurTrail( int size )
00204 {
00205 showTrail = true;
00206 trailSize = size;
00207 trailRatioC = 3;
00208 trailRatio = 0;
00209
00210
00211
00212 trailX = new int [ trailSize ];
00213 trailY = new int [ trailSize ];
00214 trailFade = new float[ trailSize ];
00215
00216 float incrementalFactor = .2f / ( trailSize + 1 );
00217
00218 for( int i = 0; i < trailSize; ++i )
00219 {
00220
00221
00222
00223 trailX[ i ] = -1;
00224 trailY[ i ] = -1;
00225
00226
00227
00228 trailFade[i] = ( .2f - incrementalFactor ) - i * incrementalFactor;
00229 }
00230 }
00231
00232
00233
00234
00235
00236 public void dump ()
00237 {
00238 System.out.printf( "%8.1f %8.1f %8.1f %8.1f %8.1f %8.1f %8.1f\n",
00239 lifeTime, xPos, yPos, vx, vy, ax, ay );
00240 }
00241
00242
00243
00244
00245 public void resetVelocity ()
00246 {
00247 vx = vy = vsq = 0;
00248 }
00249
00250
00251
00252
00253 public void makeCentripetalVelocity ()
00254 {
00255 vx = -ay;
00256 vy = ax;
00257 }
00258
00259
00260
00261
00262 public void resetForce ()
00263 {
00264 Fx = 0; Fy = 0.0;
00265 }
00266
00267
00268
00269
00270
00271
00272
00273 public void addForceFromParticle( Particle p )
00274 {
00275 if ( p == this ) {
00276 return;
00277 }
00278
00279
00280
00281 double Rsq = Math.pow( p.xPos - xPos, 2 )
00282 + Math.pow( p.yPos - yPos, 2 );
00283
00284
00285
00286
00287
00288 if ( Rsq < minRsq ) {
00289 Rsq = minRsq;
00290 }
00291
00292
00293
00294 double cos = ( p.xPos - xPos ) / Math.sqrt( Rsq );
00295 double sin = ( p.yPos - yPos ) / Math.sqrt( Rsq );
00296
00297
00298
00299 double e_force = -k_e * charge * p.charge / Rsq;
00300 Fx += e_force * cos;
00301 Fy += e_force * sin;
00302
00303
00304
00305 double g_force = k_G * mass * p.mass / Rsq;
00306 Fx += g_force * cos;
00307 Fy += g_force * sin;
00308 }
00309
00310
00311
00312
00313 public void applyForce ()
00314 {
00315 ax = Fx / mass;
00316 ay = Fy / mass;
00317
00318
00319
00320
00321 if ( vsq > maxVsq ) {
00322 ax += -k_x * vsq * vx / Math.sqrt( vsq );
00323 ay += -k_x * vsq * vy / Math.sqrt( vsq );
00324 }
00325
00326 asq = Math.pow( ax, 2 ) + Math.pow( ay, 2 );
00327 }
00328
00329
00330
00331
00332
00333
00334
00335 public void moveParticle( double dx, double dy )
00336 {
00337 xPos += dx; yPos += dy;
00338 resetVelocity ();
00339 }
00340
00341
00342
00343
00344
00345
00346
00347 public void forceBarrier( WorldOfParticles.Barrier barrier )
00348 {
00349 final double k = 0.5;
00350
00351 if ( xPos < barrier.xBeg ) {
00352 xPos = barrier.xBeg;
00353 vx = -k * vx;
00354 } else if ( xPos > barrier.xEnd ) {
00355 xPos = barrier.xEnd;
00356 vx = -k * vx ;
00357 }
00358
00359 if ( yPos < barrier.yBeg ) {
00360 yPos = barrier.yBeg;
00361 vy = -k * vy;
00362 } else if ( yPos > barrier.yEnd ) {
00363 yPos = barrier.yEnd;
00364 vy = -k * vy;
00365 }
00366 }
00367
00368
00369
00370
00371
00372
00373
00374 private void integrateNewtonLaws( double dT )
00375 {
00376 synchronized( world )
00377 {
00378 xPos += vx * dT;
00379 yPos += vy * dT;
00380
00381 vx += ax * dT;
00382 vy += ay * dT;
00383
00384 vsq = Math.pow( vx, 2 ) + Math.pow( vy, 2 );
00385
00386 forceBarrier( world.getBarrier () );
00387 }
00388 }
00389
00390
00391
00392
00393 private void drawGradCircle( Graphics2D g2d,
00394 float x, float y, float radius, Color color
00395 )
00396 {
00397 Point2D center = new Point2D.Float( x, y );
00398
00399 float[] dist = { 0.0f, 1.0f };
00400
00401 float[] c = { color.getRed()/255f, color.getGreen()/255f, color.getBlue()/255f };
00402 Color[] colors = {
00403 new Color( c[0] * 1.0f, c[1] * 1.0f, c[2] * 1.0f, 1.0f ),
00404 new Color( c[0] * 0.8f, c[1] * 0.8f, c[2] * 0.8f, 0.0f )
00405 };
00406
00407 g2d.setPaint(
00408 new RadialGradientPaint(
00409 center, radius, dist, colors, CycleMethod.NO_CYCLE
00410 )
00411 );
00412
00413 g2d.fill( new Ellipse2D.Float( x - radius, y - radius, 2*radius, 2*radius ) );
00414 }
00415
00416
00417
00418
00419 private void createParticleImage ()
00420 {
00421 GraphicsConfiguration gc = world.getGraphicsConfiguration ();
00422 if ( gc == null ) {
00423 return;
00424 }
00425
00426 int diameter = (int)( radius * 2 );
00427
00428 image = gc.createCompatibleImage( diameter, diameter, Transparency.TRANSLUCENT );
00429
00430 Graphics2D gImg = image.createGraphics ();
00431 if ( aliasedEdges > 0 )
00432 {
00433 drawGradCircle(gImg, (int)radius, (int)radius, (int)radius, color );
00434 }
00435 else
00436 {
00437 gImg.setColor( color );
00438 gImg.fillOval( 0, 0, diameter, diameter );
00439 }
00440
00441 gImg.dispose ();
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451 public void paint( Graphics gr, boolean annotate )
00452 {
00453 if ( image == null ) {
00454 createParticleImage();
00455 if ( image == null ) {
00456 return;
00457 }
00458 }
00459
00460 Graphics2D g = (Graphics2D)gr.create ();
00461
00462
00463
00464 int px = (int)xPos;
00465 int py = (int)yPos;
00466 int x = (int)( xPos - radius );
00467 int y = (int)( yPos - radius );
00468
00469
00470
00471 float baseFade = (float)( lifeTime < 0 ? 0 : lifeTime > 1 ? 1 : lifeTime );
00472
00473 if ( showTrail ) {
00474
00475
00476
00477 for( int i = 0; i < trailSize; ++i ) {
00478 if( trailX[i] >= 0 ) {
00479
00480 g.setComposite(
00481 AlphaComposite.SrcOver.derive( baseFade * trailFade[i] )
00482 );
00483 g.drawImage( image, trailX[i], trailY[i], null );
00484 }
00485 }
00486
00487 --trailRatio;
00488
00489 if ( trailRatio <= 0 ) {
00490
00491 trailRatio = trailRatioC;
00492
00493
00494
00495 for( int i = trailSize - 1; i > 0; --i ) {
00496 trailX[ i ] = trailX[ i - 1 ];
00497 trailY[ i ] = trailY[ i - 1 ];
00498 }
00499 trailX[ 0 ] = x;
00500 trailY[ 0 ] = y;
00501 }
00502 }
00503
00504 g.setComposite( AlphaComposite.SrcOver.derive( baseFade ) );
00505
00506 if ( annotate && asq > 0.0 )
00507 {
00508
00509
00510
00511 int dx = asq < 1e-9 ? 0
00512 : (int)( 1e-3 * ax + 3 * ax * Math.log( 1 + asq ) / Math.sqrt( asq ) );
00513 int dy = asq < 1e-9 ? 0
00514 : (int)( 1e-3 * ay + 3 * ay * Math.log( 1 + asq ) / Math.sqrt( asq ) );
00515
00516 Color cText = new Color( color.getRGB () ).darker ();
00517
00518
00519
00520 g.setColor( cText.darker () );
00521 g.drawLine( px, py, px + dx, py + dy );
00522 g.drawArc( px + dx - 4, py + dy - 4, 8, 8, 0, 360 );
00523
00524
00525
00526 g.setColor( cText );
00527 Font f = new Font( Font.MONOSPACED, Font.PLAIN, 14 );
00528 g.setFont( f );
00529 g.drawString( String.format( "%+7.1f", ax ), px + dx - 4, py + dy - 4 - 14 );
00530 g.drawString( String.format( "%+7.1f", ay ), px + dx - 4, py + dy - 4 );
00531 }
00532
00533 g.drawImage( image, x, y, null );
00534
00535 if ( annotate && true )
00536 {
00537 g.setColor( Color.BLACK );
00538 int R = (int)( radius / 3 );
00539 if ( charge != 0 ) {
00540 g.drawLine( px - R, py, px + R, py );
00541 }
00542 if ( charge > 0 ) {
00543 g.drawLine( px, py - R, px, py + R );
00544 }
00545 }
00546
00547 g.dispose ();
00548 }
00549
00550
00551
00552
00553
00554 public void kill ()
00555 {
00556 lifeTime = 0.0;
00557 if ( mainThread != null ) {
00558 mainThread.interrupt ();
00559 }
00560 }
00561
00562
00563
00564
00565 public void interruptibleSleep( long millis )
00566 {
00567 synchronized( this )
00568 {
00569 try {
00570 Thread.sleep( 10 );
00571 }
00572 catch( InterruptedException ie ) {
00573 lifeTime = 0;
00574 }
00575 }
00576 }
00577
00578
00579
00580
00581
00582
00583
00584 @Override
00585 public void run ()
00586 {
00587 final int sleepMillis = 5;
00588
00589 long oldTime = System.nanoTime ();
00590
00591 while( lifeTime >= 0 )
00592 {
00593 interruptibleSleep( sleepMillis );
00594 long currentTime = System.nanoTime ();
00595
00596
00597
00598
00599
00600 double dT = world.getTimeScale () * (double)( currentTime - oldTime ) / 1e9;
00601
00602 if ( ! world.isPaused () ) {
00603 integrateNewtonLaws( dT );
00604 }
00605
00606 lifeTime -= dT;
00607 oldTime = currentTime;
00608 }
00609
00610
00611
00612 world.removeParticle( this );
00613 }
00614 }