/* * h1b.java - revised 18 Ap5 05 - width 301, height 241 * hanging chain (n mass points on an n+1 segment string) * chain length = 400, n=64 * support separation: horizontal 240, vertical 120 * the string tension and angle at the left support are varied * to bring the end of the string to the right support * the analytic curve is a catenary (with integer constants) * @author jackord@kw.igs.net */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class h1b extends Applet implements ActionListener { int n, phi; int kk=0; int show=1; int first=0; // Declarations double tns, d; String b1s="Vary"; Button b1=new Button(b1s); TextField tx=new TextField(10); Image bim; Graphics bgr; public void init() { add(b1); add(tx); b1.addActionListener(this); setBackground(new Color(211, 211, 211)); tx.setBackground(getBackground()); } double hit(Graphics g) { // Trace out string int x1, y1, x2, y2; double x, y, q, t, tn, tc; x=10; y=10; q=phi*Math.PI/18000; t=tns; // First angle & tension tn=Math.tan(q); tc=t*Math.cos(q); x=x+d*Math.cos(q); y=y-d*Math.sin(q); x1=(int)(x); y1=(int)(y); if (show==1) { g.drawLine(10, 10, x1, y1);} for (int i=1; i<=n; i=i+1) { // Loop tn=tn+1/tc; q=Math.atan(tn); t=tc/Math.cos(q); // Next angle & tension x=x+d*Math.cos(q); y=y-d*Math.sin(q); x2=(int)(x); y2=(int)(y); if (show==1) { g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } } return Math.sqrt((x-250)*(x-250)+(y-130)*(y-130)); // Return deviation } double ang(Graphics g) { // Find optimum angle double s, so; int dph=-256; show=0; s=hit(g); do { phi=phi+dph; so=s; s=hit(g); if (s>so) { dph=-dph/2; } } while (Math.abs(dph)>1); phi=phi+2*dph; show=1; s=hit(g); return s; } double cosh(double x) { // Cosh(x) return (Math.exp(x)+Math.exp(-x))/2; } double sinh(double x) { // Sinh(x) return (Math.exp(x)-Math.exp(-x))/2; } public void paint(Graphics g) { // Paint double dtns=2; double s, so, a, b, c, y; int y1=0; int y2, del; if (first==0) { bim=createImage(301, 241); bgr=bim.getGraphics(); first=1; } bgr.setColor(Color.black); bgr.drawRect(0, 0, 300, 240); bgr.setColor(Color.red); bgr.fillArc(5, 5, 11, 11, 0, 360); bgr.drawArc(5, 5, 10, 10, 0, 360); bgr.fillArc(245, 125, 11, 11, 0, 360); bgr.drawArc(245, 125, 10, 10, 0, 360); if (kk==1) { a=152; c=69; b=27-c; // Catenary constants y=c*(sinh((250-a)/c)-sinh((10-a)/c)); // Catenary length for (int i=10; i<=250; i=i+1) { // Plot catenary y=b+c*cosh((i-a)/c); y2=240-(int)(y); if (i>10) { bgr.drawLine(i-1, y1, i, y2); } y1=y2; } } g.drawImage(bim, 0, 0, null); n=64; d=400./(n+1); phi=-6000; tns=48; g.setColor(Color.blue); s=hit(g); tx.setText("Dev = "+.01*(int)(100*s)); if (kk==1) { del=500; long tt=System.currentTimeMillis(); // Initialize timing do { // Vary initial tension tt=tt+del; while (System.currentTimeMillis()