Java Programs for P1
// P1A
// Draws a logarithmic spiral with variable color..
import java.io.*;
import java.awt.*;
public class P1A {
public static void main(String args[]) throws IOException {
Drawing d = new Drawing();
d.resize(600,500);
d.move(0,75);
d.setTitle("Logarithmic Spiral");
d.show();
}
}
public class Drawing extends Frame {
final int hc = 300;
final int vc = 250; // (hc,vc) = center of the spiral
final int n = 1000; // The total number of "legs" in the spiral.
final int turn = 137; // Turn factor. Each leg turns "left" this amount in degrees
final double d = .4; // The length of the kth leg is k*d
public void paint(Graphics g) {
int h, v; // The horizontal and vertical coordinates of the current vertex.
int h_next, v_next; // The horizontal and vertical coordinates of the next vertex.
int theta; // The "angle" of the current leg.
int k; // The index of the current leg.
double L; // The length of the current leg.
h = hc;
v = vc;
for (k=1;k<=n;k++) {
if (k%3==0)
g.setColor(Color.blue);
else if (k%3==1)
g.setColor(Color.red);
else
g.setColor(Color.green);
theta = (k*turn) % 360;
L = k*d;
h_next = (int) Math.round(h + L*Math.cos(theta*Math.PI/180));
v_next = (int) Math.round(v + L*Math.sin(theta*Math.PI/180));
g.drawLine(h,v,h_next,v_next); // Draw the current leg.
h = h_next;
v = v_next;
}
}
}
// P1B
// Computes pi as a limit of polygon areas.
import java.io.*;
public class P1B {
public static void main( String args[]) {
// initialize Text object in to read from standard input.
TokenReader in = new TokenReader(System.in);
int n; // The number of sides in the approximating n-gons.
double OuterA, InnerA; // The areas of the circumscibed and inscribed n-gons.
double Error; // OuterA - InnerA
double c, s; // The cosine and sine of pi/n.
n = 4;
OuterA = 4;
InnerA = 2;
c = 1.0/Math.sqrt(2.0);
System.out.println("\n n A(n) B(n) B(n)-A(n)");
System.out.println("-----------------------------------------------------------------");
while (OuterA - InnerA > 0.00000001) {
n = 2*n;
s = Math.sqrt((1.0 - c) / 2.0);
c = Math.sqrt((1.0 + c) / 2.0);
InnerA = n*s*c;
OuterA = n*s/c;
Error = OuterA - InnerA;
System.out.println(n + " " + InnerA + " " + OuterA + " " + Error);
}
// Wait for user to enter input to ensure console window remains visible
in.waitUntilEnter();
}
}
// P1C
// Estimates pi via Monte Carlo.
import java.io.*;
import java.awt.*;
public class P1C {
public static void main(String args[]) {
Drawing d = new Drawing();
d.resize(600,500);
d.move(0,75);
d.setTitle("P1C: The Dart Board");
d.show();
}
}
public class Drawing extends Frame {
final int hc = 300;
final int vc = 250; // (hc,vc) = center of circle
final int r = 200; // the radius
public void paint(Graphics g) {
int Left = hc-r; // Left edge of square
int Top = vc-r; // Top edge of square
long k; // Dart throw index
long hits; // Number of darts that land in circle.
long n; // Total number of darts thrown.
double x,y; // (x,y) = coordinates of current dart throw
double dist; // Distance of current dart throw to center.
double MyPi; // The pi-estimate.
// Draw the target.
g.drawOval(hc-r,vc-r,2*r,2*r);
g.drawRect(hc-r,vc-r,2*r,2*r);
// --------------- Comment Line A ----------------------
hits = 0;
n = 1000;
for (k=1;k<=n;k++){
// Generate the next dart throw and plot.
x = Left + 2*r*Math.random();
y = Top + 2*r*Math.random();
g.drawOval((int)x,(int)y,1,1);
// Check to see if the throw lands inside the circle and tabulate.
dist = Math.sqrt((x-hc)*(x-hc) + (y-vc)*(y-vc));
if (dist<=r)
hits++;
}
MyPi = ((double) hits/n)*4;
Format.println(System.out,"Number of dart throws = %6d\n",n);
Format.println(System.out,"Appx Pi = %20.15f",MyPi);
Format.println(System.out,"True Pi = %20.15f",Math.PI);
System.out.println();
// --------------- Comment Line B ----------------------
// --------------- Comment Line C ----------------------
}
}