01: import java.applet.*;
02: import java.awt.*;
03: import JSci.maths.chaos.*;
04:
05: /**
06: * Plot of the Koch snowflake.
07: * @author Mark Hale
08: * @version 1.0
09: */
10: public final class KochSnowflakePlot extends Applet {
11: private final int N = 5;
12: private Image img;
13: private KochCurveGraphic curve;
14: private int width, height;
15:
16: public void init() {
17: width = getSize().width;
18: height = getSize().height;
19: img = createImage(width, height);
20: curve = new KochCurveGraphic(img.getGraphics());
21: final int len = width / 2;
22: final int h_2 = (int) Math.round(len * Math.sqrt(3.0) / 4.0);
23: curve.draw((width - len) / 2, height / 2 - h_2, width / 2,
24: height / 2 + h_2, N);
25: curve.draw(width / 2, height / 2 + h_2, (width + len) / 2,
26: height / 2 - h_2, N);
27: curve.draw((width + len) / 2, height / 2 - h_2,
28: (width - len) / 2, height / 2 - h_2, N);
29: }
30:
31: public void paint(Graphics g) {
32: g.drawImage(img, 0, 0, this );
33: }
34:
35: class KochCurveGraphic extends KochCurve {
36: private final Graphics g;
37:
38: public KochCurveGraphic(Graphics grafixs) {
39: g = grafixs;
40: }
41:
42: public void draw(int startX, int startY, int endX, int endY,
43: int n) {
44: g.setColor(Color.black);
45: g.drawLine(startX, height - startY, endX, height - endY);
46: recurse(startX, startY, endX, endY, n);
47: }
48:
49: protected void drawLine(double startX, double startY,
50: double endX, double endY) {
51: g.drawLine((int) Math.round(startX), height
52: - (int) Math.round(startY), (int) Math.round(endX),
53: height - (int) Math.round(endY));
54: }
55:
56: protected void eraseLine(double startX, double startY,
57: double endX, double endY) {
58: g.setColor(getBackground());
59: drawLine(startX, startY, endX, endY);
60: g.setColor(Color.black);
61: }
62: }
63: }
|