KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > chaos > KochCurve


1 package JSci.maths.chaos;
2
3 /**
4 * The KochCurve class provides an object that encapsulates the Koch curve.
5 * @version 0.1
6 * @author Mark Hale
7 */

8 public abstract class KochCurve extends Object JavaDoc {
9         public KochCurve() {}
10         public double hausdorffDimension() {
11                 return Math.log(4.0)/Math.log(3.0);
12         }
13         /**
14         * The recursive algorithm for the Koch curve.
15         * @param startX the x-coordinate of the start of the line.
16         * @param startY the x-coordinate of the start of the line.
17         * @param endX the x-coordinate of the end of the line.
18         * @param endY the x-coordinate of the end of the line.
19         * @param n the number of recursions.
20         */

21         public void recurse(double startX, double startY, double endX, double endY, int n) {
22                 if(n==0)
23                         return;
24                 final double l_3X=(endX-startX)/3.0;
25                 final double l_3Y=(endY-startY)/3.0;
26                 eraseLine(startX+l_3X, startY+l_3Y, endX-l_3X, endY-l_3Y);
27                 final double h=Math.sqrt(3.0)/2.0;
28                 final double pX=(startX+endX)/2.0-l_3Y*h;
29                 final double pY=(startY+endY)/2.0+l_3X*h;
30                 drawLine(startX+l_3X, startY+l_3Y, pX, pY);
31                 drawLine(pX, pY, endX-l_3X, endY-l_3Y);
32                 recurse(startX, startY, startX+l_3X, startY+l_3Y, n-1);
33                 recurse(startX+l_3X, startY+l_3Y, pX, pY, n-1);
34                 recurse(pX, pY, endX-l_3X, endY-l_3Y, n-1);
35                 recurse(endX-l_3X, endY-l_3Y, endX, endY, n-1);
36         }
37         /**
38         * Draws a line segment in a 2D plane.
39         * This need not be an actual graphical operation,
40         * but some corresponding abstract operation.
41         */

42         protected abstract void drawLine(double startX, double startY, double endX, double endY);
43         /**
44         * Erases a line segment in a 2D plane.
45         * This need not be an actual graphical operation,
46         * but some corresponding abstract operation.
47         */

48         protected abstract void eraseLine(double startX, double startY, double endX, double endY);
49 }
50
51
Popular Tags