Skip to content

Commit b4aab49

Browse files
committed
Vicsek fractal drawing completed.
1 parent 0a11850 commit b4aab49

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

09-Fractal-Drawing/03-Fractal-Basics/src/AlgoFrame.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,19 @@ private void drawFractal(Graphics2D g, int x, int y, int w, int h, int depth){
7878
return;
7979
}
8080

81+
if(w <= 1 || h <= 1){
82+
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
83+
AlgoVisHelper.fillRectangle(g, x, y, Math.max(w,1), Math.max(h,1));
84+
return;
85+
}
86+
8187
int w_3 = w/3;
8288
int h_3 = h/3;
83-
drawFractal(g, x, y, w_3, h_3, depth+1);
84-
drawFractal(g, x+2*w_3, y, w_3, h_3, depth+1);
85-
drawFractal(g, x+w_3, y+w_3, w_3, h_3, depth+1);
86-
drawFractal(g, x, y+2*w_3, w_3, h_3, depth+1);
87-
drawFractal(g, x+2*w_3, y+2*w_3, w_3, h_3, depth+1);
89+
drawFractal(g, x, y, w_3, h_3, depth + 1);
90+
drawFractal(g, x + 2 * w_3, y, w_3, h_3, depth + 1);
91+
drawFractal(g, x + w_3, y + h_3, w_3, h_3, depth + 1);
92+
drawFractal(g, x, y + 2 * h_3, w_3, h_3, depth + 1);
93+
drawFractal(g, x + 2 * w_3, y + 2 * h_3, w_3, h_3, depth + 1);
8894

8995
return;
9096
}

09-Fractal-Drawing/04-Fractal-with-Interaction/src/AlgoFrame.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,25 @@ public void paintComponent(Graphics g) {
6868

6969
private void drawFractal(Graphics2D g, int x, int y, int w, int h, int depth){
7070

71-
int w_3 = w/3;
72-
int h_3 = h/3;
73-
74-
if(w_3 <= 0 || h_3 <= 0){
71+
if( depth == data.depth ){
7572
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
76-
AlgoVisHelper.fillRectangle(g, x, y, 1, 1);
73+
AlgoVisHelper.fillRectangle(g, x, y, w, h);
7774
return;
7875
}
7976

80-
if( depth == data.depth ){
77+
if(w <= 1 || h <= 1){
8178
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
82-
AlgoVisHelper.fillRectangle(g, x, y, w, h);
79+
AlgoVisHelper.fillRectangle(g, x, y, Math.max(w,1), Math.max(h,1));
8380
return;
8481
}
8582

86-
drawFractal(g, x, y, w_3, h_3, depth+1);
87-
drawFractal(g, x+2*w_3, y, w_3, h_3, depth+1);
88-
drawFractal(g, x+w_3, y+w_3, w_3, h_3, depth+1);
89-
drawFractal(g, x, y+2*w_3, w_3, h_3, depth+1);
90-
drawFractal(g, x+2*w_3, y+2*w_3, w_3, h_3, depth+1);
83+
int w_3 = w/3;
84+
int h_3 = h/3;
85+
drawFractal(g, x, y, w_3, h_3, depth + 1);
86+
drawFractal(g, x + 2 * w_3, y, w_3, h_3, depth + 1);
87+
drawFractal(g, x + w_3, y + h_3, w_3, h_3, depth + 1);
88+
drawFractal(g, x, y + 2 * h_3, w_3, h_3, depth + 1);
89+
drawFractal(g, x + 2 * w_3, y + 2 * h_3, w_3, h_3, depth + 1);
9190

9291
return;
9392
}

09-Fractal-Drawing/04-Fractal-with-Interaction/src/AlgoVisualizer.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ public class AlgoVisualizer {
88
private FractalData data;
99
private AlgoFrame frame;
1010

11-
public AlgoVisualizer(int depth){
11+
public AlgoVisualizer(int maxDepth){
1212

13-
data = new FractalData(depth);
14-
int sceneWidth = (int)Math.pow(3, depth);
15-
int sceneHeight = (int)Math.pow(3, depth);
13+
data = new FractalData(maxDepth);
14+
int sceneWidth = (int)Math.pow(3, maxDepth);
15+
int sceneHeight = (int)Math.pow(3, maxDepth);
16+
// int sceneWidth = 1024;
17+
// int sceneHeight = 768;
1618

1719
EventQueue.invokeLater(() -> {
1820
frame = new AlgoFrame("Fractal Visualizer", sceneWidth,sceneHeight);
@@ -53,8 +55,8 @@ public void keyReleased(KeyEvent event){
5355

5456
public static void main(String[] args) {
5557

56-
int depth = 6;
58+
int maxDepth = 6;
5759

58-
AlgoVisualizer vis = new AlgoVisualizer(depth);
60+
AlgoVisualizer vis = new AlgoVisualizer(maxDepth);
5961
}
6062
}

09-Fractal-Drawing/Chapter-09.key

9.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)