forked from Toyoyou/homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibSpiral.java
More file actions
55 lines (45 loc) · 1.38 KB
/
Copy pathFibSpiral.java
File metadata and controls
55 lines (45 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import objectdraw.*;
import java.awt.*;
public class FibSpiral {
public static int scale = 10;
private int nn;
private FibSpiral child;
public FibSpiral(Location point, int n, int direction, DrawingCanvas canvas){
nn = n;
double angle = 90;
angle -= direction * 90;
int f = fib(n);
Location origin = new Location(point);
switch(direction%4){
case 0:
origin.translate(f*scale,0);
break;
case 1:
origin.translate(fib(n-2)*scale,f*scale);
point.translate(-f*scale,0);
break;
case 2:
origin.translate(-fib(n-1)*scale,fib(n-2)*scale);
point.translate(-f*scale,-f*scale);
break;
case 3:
origin.translate(0,-fib(n-1)*scale);
point.translate(0,-f*scale);
}
new FramedArc(point,f*2*scale,f*2*scale,angle,90,canvas);
if(n>1){
child = new FibSpiral(origin,n-1,direction+1,canvas);
}
}
public static int fib(int n){
if(n<=1)return 1;
return fib(n-1)+fib(n-2);
}
public double getLength(){
double l = Math.PI*fib(nn)/2;
if(nn>1){
l+=child.getLength();
}
return l;
}
}