/* * frds.java - revised 17 Apr 05 - width 301, height 201 * @author jackord@kw.igs.net * gravitational force between identical rods of length D: * rod A lies along the x-axis with its center at (D/2, 0) * rod B is perpendicular to the x-axis with its center at (2*D, D/2) * each rod is made up of N spheres of diameter D/N * in the program, D=128, and N=8 or N=16 * the rods are released from rest and their motion is followed * until they collide */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class frds extends Applet implements ActionListener { String b1s="Run"; Button b1=new Button(b1s); String b2s="8/16"; Button b2=new Button(b2s); int kk=0; int n=8; int d=128; int M=128; int first=0; int r, m, mm; double xac, yac, xbc, ybc, fax, fay, Ta, Tb, rm; double [] xa=new double[33]; double [] ya=new double[33]; double [] xb=new double[33]; double [] yb=new double[33]; Image bim; Graphics bgr; public void init() { setLayout(new FlowLayout(FlowLayout.LEFT)); add(b1); b1.addActionListener(this); add(b2); b2.addActionListener(this); setBackground(new Color(211, 211, 211)); } public void paint(Graphics g) { double Ic, dt, vax, vay, wa, wb, aa, ab; int lim; g.setColor(Color.black); g.drawRect(0, 0, 300, 200); if (first==0) { bim=createImage(265, 161); // Animation buffer bgr=bim.getGraphics(); first=1; } int r=d/n/2; lim=8*r*r*r; rm=2*lim; m=M/n; mm=m*m; dt=.1; aa=0; ab=Math.PI/2; if (kk==0) { bgr.clearRect(0, 0, 265, 161); bgr.setColor(Color.red); for (int i=1; i<=n; i=i+1) { // Define and plot rods xa[i]=r*(2*i-1); ya[i]=128; xa[i+n]=xa[i]; ya[i+n]=ya[i]; xb[i]=256; yb[i]=r*(2*i-1); xb[i+n]=xb[i]; yb[i+n]=yb[i]; bgr.fillArc((int)(xa[i])-r, (int)(ya[i])-r, 2*r, 2*r, 0, 360); bgr.fillArc((int)(xb[i])-r, (int)(yb[i])-r, 2*r, 2*r, 0, 360); } bgr.drawLine(155, 96, 166, 96); bgr.drawLine(160, 91, 160, 102); g.drawImage(bim, 20, 30, null); } Ic=.4*M*r*r; // Moment of Inertia for (int i=1; i<=n/2; i=i+1) { Ic=Ic+2*m*(xa[i]-d/2)*(xa[i]-d/2); } if (kk==1) { force(); // Feynman vax=fax/M*dt/2; vay=fay/M*dt/2; // half wa=Ta/Ic*dt/2; wb=Tb/Ic*dt/2; // step while (rm>lim) { // Loop until collision xac=xac+vax*dt; yac=yac+vay*dt; // Translation xbc=xbc-vax*dt; ybc=ybc-vay*dt; aa=aa+wa*dt; ab=ab+wb*dt; // Rotation for (int i=1; i<=n; i=i+1) { // New rod positions xa[i]=xac+(2*i-1-n)*r*Math.cos(aa); ya[i]=yac+(2*i-1-n)*r*Math.sin(aa); xb[i]=xbc+(2*i-1-n)*r*Math.cos(ab); yb[i]=ybc+(2*i-1-n)*r*Math.sin(ab); } bgr.clearRect(0, 0, 265, 161); bgr.setColor(Color.red); bgr.drawLine(155, 96, 166, 96); bgr.drawLine(160, 91, 160, 102); for (int i=2*n; i>=1; i=i-1) { // Plot rods if (i==n) { bgr.setColor(Color.blue); } bgr.fillArc((int)(xa[i])-r, (int)(ya[i])-r, 2*r, 2*r, 0, 360); bgr.fillArc((int)(xb[i])-r, (int)(yb[i])-r, 2*r, 2*r, 0, 360); } g.drawImage(bim, 20, 30, null); force(); // Newton's law vax=vax+fax/M*dt; vay=vay+fay/M*dt; wa=wa+Ta/Ic*dt; wb=wb+Tb/Ic*dt; } aa=-(int)(aa*18000/Math.PI); ab=(int)(ab*18000/Math.PI-9000); g.drawString("Rotates "+(aa/100)+" degrees", 20, 120); g.drawString("Rotates "+(ab/100)+" degrees", 130, 40); } } public void force() { double dx, dy, dfx, dfy, fx, fy, rcube; xac=(xa[1]+xa[n])/2; yac=(ya[1]+ya[n])/2; // C of M xbc=(xb[1]+xb[n])/2; ybc=(yb[1]+yb[n])/2; fax=0; fay=0; Ta=0; Tb=0; for (int i=1; i<=n; i=i+1) { // Rod A fx=0; fy=0; for (int j=1; j<=n; j=j+1) { // Rod B dx=xb[j]-xa[i]; dy=yb[j]-ya[i]; rcube=Math.pow(dx*dx+dy*dy, 1.5); dfx=mm*dx/rcube; // Force between spheres dfy=mm*dy/rcube; fx=fx+dfx; fy=fy+dfy; Tb=Tb-(xb[j]-xbc)*dfy+(yb[j]-ybc)*dfx; // Torque on rod B if (rcube