Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit adb9591

Browse files
Fractal drawing codes updated.
1 parent d9e62fb commit adb9591

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

‎09-Fractal-Drawing/03-Vicsek-Fractal/src/AlgoFrame.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ public void paintComponent(Graphics g) {
7272

7373
private void drawFractal(Graphics2D g, int x, int y, int w, int h, int depth){
7474

75-
if(depth == data.getDepth() ){
75+
if(w <= 1 || h <= 1){
7676
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
77-
AlgoVisHelper.fillRectangle(g, x, y, w, h);
77+
AlgoVisHelper.fillRectangle(g, x, y, Math.max(w,1), Math.max(h,1));
7878
return;
7979
}
8080

81-
if(w <= 1 || h <= 1){
81+
if(depth == data.getDepth() ){
8282
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
83-
AlgoVisHelper.fillRectangle(g, x, y, Math.max(w,1), Math.max(h,1));
83+
AlgoVisHelper.fillRectangle(g, x, y, w, h);
8484
return;
8585
}
8686

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

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

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

71-
if(depth == data.depth){
71+
if(w <= 1 || h <= 1){
7272
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
73-
AlgoVisHelper.fillRectangle(g, x, y, w, h);
73+
AlgoVisHelper.fillRectangle(g, x, y, Math.max(w,1), Math.max(h,1));
7474
return;
7575
}
7676

77-
if(w <= 1 || h <= 1){
77+
if(depth == data.depth){
7878
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
79-
AlgoVisHelper.fillRectangle(g, x, y, Math.max(w,1), Math.max(h,1));
79+
AlgoVisHelper.fillRectangle(g, x, y, w, h);
8080
return;
8181
}
8282

‎09-Fractal-Drawing/05-Sierpinski-Carpet/src/AlgoFrame.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public void paintComponent(Graphics g) {
6464

6565
private void drawFractal(Graphics2D g, int x, int y, int w, int h, int depth){
6666

67+
if(w <= 1 || h <= 1)
68+
return;
69+
6770
int w_3 = w / 3;
6871
int h_3 = h / 3;
6972

@@ -73,9 +76,6 @@ private void drawFractal(Graphics2D g, int x, int y, int w, int h, int depth){
7376
return;
7477
}
7578

76-
if(w <= 1 || h <= 1)
77-
return;
78-
7979
for(int i = 0 ; i < 3 ; i ++)
8080
for(int j = 0 ; j < 3 ; j ++)
8181
if(i == 1 && j == 1){

‎09-Fractal-Drawing/06-Sierpinski-Triangle/src/AlgoFrame.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void paintComponent(Graphics g) {
6363
g2d.addRenderingHints(hints);
6464

6565
// 具体绘制
66-
drawFractal(g2d, 0, canvasWidth, canvasHeight, 0);
66+
drawFractal(g2d, 0, canvasHeight, canvasWidth, 0);
6767
}
6868

6969
private void drawFractal(Graphics2D g, int Ax, int Ay, int side, int depth){
@@ -81,20 +81,20 @@ private void drawFractal(Graphics2D g, int Ax, int Ay, int side, int depth){
8181
int Cx = Ax + side/2;
8282
int Cy = Ay - h;
8383

84-
if(depth == data.depth){
84+
if(depth == data.depth){
8585
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
8686
AlgoVisHelper.fillTriangle(g, Ax, Ay, Bx, By, Cx, Cy);
8787
return;
8888
}
8989

90-
int AB_centerx = (Ax+Bx)/2;
91-
int AB_centery = (Ay+By)/2;
90+
int AB_centerx = (Ax + Bx) / 2;
91+
int AB_centery = (Ay + By) / 2;
9292

93-
int AC_centerx = (Ax+Cx)/2;
94-
int AC_centery = (Ay+Cy)/2;
93+
int AC_centerx = (Ax + Cx) / 2;
94+
int AC_centery = (Ay + Cy) / 2;
9595

96-
int BC_centerx = (Bx+Cx)/2;
97-
int BC_centery = (By+Cy)/2;
96+
// int BC_centerx = (Bx + Cx) / 2;
97+
// int BC_centery = (By + Cy) / 2;
9898

9999
drawFractal(g, Ax, Ay, side/2, depth+1);
100100
drawFractal(g, AC_centerx, AC_centery, side/2, depth+1);

‎09-Fractal-Drawing/06-Sierpinski-Triangle/src/AlgoVisualizer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ 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(2, maxDepth);
15+
int sceneHeight = (int)Math.pow(2, maxDepth);
1616

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

5454
public static void main(String[] args) {
5555

56-
int depth = 6;
56+
int maxDepth = 9;
5757

58-
AlgoVisualizer vis = new AlgoVisualizer(depth);
58+
AlgoVisualizer vis = new AlgoVisualizer(maxDepth);
5959
}
6060
}

‎09-Fractal-Drawing/Chapter-09.key

90.3 KB
Binary file not shown.

‎09-Fractal-Drawing/Optional-01-Another-Vicsek-Fractal/src/AlgoFrame.java

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

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

71-
if(depth == data.depth){
71+
if(w <= 1 || h <= 1){
7272
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
73-
AlgoVisHelper.fillRectangle(g, x, y, w, h);
73+
AlgoVisHelper.fillRectangle(g, x, y, Math.max(w,1), Math.max(h,1));
7474
return;
7575
}
7676

77-
if(w <= 1 || h <= 1){
77+
if(depth == data.depth){
7878
AlgoVisHelper.setColor(g, AlgoVisHelper.Indigo);
79-
AlgoVisHelper.fillRectangle(g, x, y, Math.max(w,1), Math.max(h,1));
79+
AlgoVisHelper.fillRectangle(g, x, y, w, h);
8080
return;
8181
}
8282

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /