Repository navigation
Expand file tree
/
Copy pathFibonacciSquare.java
More file actions
138 lines (126 loc) · 3.12 KB
/
Copy pathFibonacciSquare.java
File metadata and controls
138 lines (126 loc) · 3.12 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
/**
* Constructs a Fibonacci spiral in one of four quadrants (bound within a
* square)
*
* @author codes
*
*/
public class FibonacciSquare extends AbstractShape {
private int quadrant;
private int fibLevel;
private static final int FIBMAX = 9;
private static final int SCALE = 10;
/**
* Creates a FibonacciSquare using the AbstractShape super constructor
*
* @param x
* @param y
* @param fibLevel
* @param quadrant
*/
public FibonacciSquare(int x, int y, int fibLevel, int quadrant) {
super(x, y, fib(fibLevel) * SCALE, Color.RED); // Added a constant multiplier to scale up size
this.quadrant = quadrant;
this.fibLevel = fibLevel;
childShapes = new Shape[1];
boundingBox = new Rectangle(0, 0, 400, 800);
}
/**
* Returns the value of the nth Fibonacci number
*
* @param n
* @return
*/
private static int fib(int n) {
int prevNum;
int prevPrevNum;
int fibNum;
prevPrevNum = prevNum = fibNum = 1;
if (n == 1 || n == 2) {
return 1;
}
for (int fibIndex = 2; fibIndex < n; fibIndex++) {
fibNum = prevNum + prevPrevNum;
prevPrevNum = prevNum;
prevNum = fibNum;
}
return fibNum;
}
/**
* Paints the FibonacciSquare, else loop through children array and paint the
* childShape
*/
@Override
public void draw(Graphics g) {
if (childShapes[0] == null) {
drawFibonacciSquare(g);
} else {
drawFibonacciSquare(g);
childShapes[0].draw(g);
}
}
/**
* Draws the enclosing rectangle and arc depending on the quadrant of the
* FibonacciSquare
*
* @param g
*/
public void drawFibonacciSquare(Graphics g) {
g.setColor(color);
g.drawRect(x, y, size, size);
// Draw an arc
switch ((quadrant + 1) % 4) {
case 1:
g.drawArc(x - size, y, 2 * size, 2 * size, 0, 90);
break;
case 2:
g.drawArc(x, y, 2 * size, 2 * size, 90, 90);
break;
case 3:
g.drawArc(x, y - size, 2 * size, 2 * size, 180, 90);
break;
default:
g.drawArc(x - size, y - size, 2 * size, 2 * size, 270, 90);
break;
}
}
/**
* Creates one child in the FibonacciSquare class, depending on the last shape's
* quadrant and coordinates
*/
@Override
public boolean createChildren() {
if (fibLevel < FIBMAX) {
int newSize = fib(fibLevel + 1) * SCALE;
int prevSize = fib(fibLevel - 1) * SCALE;
switch ((quadrant + 1) % 4) {
case 1:
childShapes[0] = new FibonacciSquare(x - newSize, y, fibLevel + 1, 1);
break;
case 2:
childShapes[0] = new FibonacciSquare(x, y + size, fibLevel + 1, 2);
break;
case 3:
childShapes[0] = new FibonacciSquare(x + size, y - prevSize, fibLevel + 1, 3);
break;
default:
childShapes[0] = new FibonacciSquare(x - newSize + size, y - newSize, fibLevel + 1, 4);
break;
}
return true;
} else {
// cannot draw another FibonacciSquare
return false;
}
}
/**
* Used to reset the level when the reset button is pressed
*/
@Override
public void resetLevel() {
level = 1;
}
}