2010-05-07 63 views
2

我已经编写绘制毕达哥拉斯树分形的程序。任何人都可以看到改进它的任何方法吗?现在它是89 LOC。改善绘制毕达哥拉斯树

import java.awt.*; 
import java.util.Scanner; 
import javax.swing.*; 

public class Main extends JFrame {; 

    public Main(int n) { 
     setSize(900, 900); 
     setTitle("Pythagoras tree"); 
     add(new Draw(n)); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     System.out.print("Give amount of steps: "); 
     new Main(sc.nextInt()); 
    } 
} 

class Draw extends JComponent { 
    private int height = 800; 
    private int width = 800; 
    private int steps; 

    public Draw(int n) { 
     steps = n; 

     Dimension d = new Dimension(width, height); 
     setMinimumSize(d); 
     setPreferredSize(d); 
     setMaximumSize(d); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.white); 
     g.fillRect(0, 0, width, height); 
     g.setColor(Color.black); 

     int x1, x2, x3, y1, y2, y3; 
     int base = width/7; 

     x1 = (width/2)-(base/2); 
     x2 = (width/2)+(base/2); 
     x3 = width/2; 
     y1 = (height-(height/15))-base; 
     y2 = height-(height/15); 
     y3 = (height-(height/15))-(base+(base/2)); 

     g.drawPolygon(new int[]{x1, x1, x2, x2, x1}, new int[]{y1, y2, y2, y1, y1}, 5); 

     int n1 = steps; 
     if(--n1 > 0){ 
      g.drawPolygon(new int[] {x1, x3, x2}, new int[] {y1, y3, y1}, 3); 
      paintMore(n1, g, x1, x3, x2, y1, y3, y1); 
      paintMore(n1, g, x2, x3, x1, y1, y3, y1); 
     } 
    } 

    public void paintMore(int n1, Graphics g, double x1_1, double x2_1, double x3_1, double y1_1, double y2_1, double y3_1){ 
     int x1, x2, x3, y1, y2, y3; 

     x1 = (int)(x1_1 + (x2_1-x3_1)); 
     x2 = (int)(x2_1 + (x2_1-x3_1)); 
     x3 = (int)(((x2_1 + (x2_1-x3_1)) + ((x2_1-x3_1)/2)) + ((x1_1-x2_1)/2)); 
     y1 = (int)(y1_1 + (y2_1-y3_1)); 
     y2 = (int)(y2_1 + (y2_1-y3_1)); 
     y3 = (int)(((y1_1 + (y2_1-y3_1)) + ((y2_1-y1_1)/2)) + ((y2_1-y3_1)/2)); 

     g.setColor(Color.green); 
     g.drawPolygon(new int[] {x1, x2, (int)x2_1, x1}, new int[] {y1, y2, (int)y2_1, y1}, 4); 
     g.drawLine((int)x1, (int)y1, (int)x1_1, (int)y1_1); 
     g.drawLine((int)x2_1, (int)y2_1, (int)x2, (int)y2); 
     g.drawLine((int)x1, (int)y1, (int)x2, (int)y2); 

     if(--n1 > 0){ 
      g.drawLine((int)x1, (int)y1, (int)x3, (int)y3); 
      g.drawLine((int)x2, (int)y2, (int)x3, (int)y3); 
      paintMore(n1, g, x1, x3, x2, y1, y3, y2); 
      paintMore(n1, g, x2, x3, x1, y2, y3, y1); 
     } 
    } 
} 
+0

你在代码中使用'pow()'在哪里? – Eric 2010-05-07 17:23:45

+0

对,我以前用它来打印结果。谢谢。 112 LOC :) – sasquatch90 2010-05-07 17:26:05

+0

您可以摆脱一些变量别名。例如,只是为了有一个名为x2的变量而将x2赋值为x1。你可以将它组合成像int x12 =(w/2) - (base/2);'然后'g.drawLine(x12,y14,x12,y23);'。并通过在定义它们时声明它们来保存一行。当然,如果LOC比算法清晰度更关心的话。 – indiv 2010-05-07 17:40:13

回答

2
  • 使用drawPolygon,而不是乘drawLine
  • 删除不使用的方法pow
  • 清理进口
  • 删除不必要的变量hw
  • --n > 0成一条线
  • 做一些重新格式化

和你去那里,90线(注释仍然计算在内):

import java.awt.*; 
import java.util.Scanner; 
import javax.swing.*; 

public class Main extends JFrame {; 

    public Main(int n) { 
     setSize(900, 900); 
     setTitle("Pythagoras tree"); 
     add(new Draw(n)); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     System.out.print("Give amount of steps: "); 
     new Main(sc.nextInt()); 
    } 
} 

class Draw extends JComponent { 
    private int height = 800; 
    private int width = 800; 
    private int steps; 

    public Draw(int n) { 
     steps = n; 

     Dimension d = new Dimension(width, height); 
     setMinimumSize(d); 
     setPreferredSize(d); 
     setMaximumSize(d); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.white); 
     g.fillRect(0, 0, width, height); 
     g.setColor(Color.black); 

     int x1, x2, x3, y1, y2, y3; 
     int base = width/7; 

     x1 = (width/2)-(base/2); 
     x2 = (width/2)+(base/2); 
     x3 = width/2; 
     y1 = (height-(height/15))-base; 
     y2 = height-(height/15); 
     y3 = (height-(height/15))-(base+(base/2)); 

     //paint 
     g.drawPolygon(new int[]{x1, x1, x2, x2, x1}, new int[]{y1, y2, y2, y1, y1}, 5); 

     int n1 = steps; 
     if(--n1 > 0){ 
      g.drawPolygon(new int[] {x1, x3, x2}, new int[] {y1, y3, y1}, 3); 
      paintMore(n1, g, x1, x3, x2, y1, y3, y1); 
      paintMore(n1, g, x2, x3, x1, y1, y3, y1); 
     } 
    } 

    public void paintMore(int n1, Graphics g, double x1_1, double x2_1, double x3_1, double y1_1, double y2_1, double y3_1){ 
     double x1, x2, x3, y1, y2, y3; 
     //counting 
     x1 = x1_1 + (x2_1-x3_1); 
     x2 = x2_1 + (x2_1-x3_1); 
     x3 = ((x2_1 + (x2_1-x3_1)) + ((x2_1-x3_1)/2)) + ((x1_1-x2_1)/2); 
     y1 = y1_1 + (y2_1-y3_1); 
     y2 = y2_1 + (y2_1-y3_1); 
     y3 = ((y1_1 + (y2_1-y3_1)) + ((y2_1-y1_1)/2)) + ((y2_1-y3_1)/2); 

     //paint 
     g.setColor(Color.green); 
     g.drawPolygon(new int[] {(int)x1, (int)x2, (int)x2_1, (int)x1}, 
         new int[] {(int)y1, (int)y2, (int)y2_1, (int)y1}, 4); 

     g.drawLine((int)x1, (int)y1, (int)x1_1, (int)y1_1); 
     g.drawLine((int)x2_1, (int)y2_1, (int)x2, (int)y2); 
     g.drawLine((int)x1, (int)y1, (int)x2, (int)y2); 

     if(--n1 > 0){ 
      g.drawLine((int)x1, (int)y1, (int)x3, (int)y3); 
      g.drawLine((int)x2, (int)y2, (int)x3, (int)y3); 
      paintMore(n1, g, x1, x3, x2, y1, y3, y2); 
      paintMore(n1, g, x2, x3, x1, y2, y3, y1); 
     } 
    } 
} 
+0

太好了,现在我只会尝试改进编码。我正在考虑在那里更好地使用递归。 – sasquatch90 2010-05-07 17:40:06

0

取代:

private int pow(int n){ 

    int pow = 2; 
    for(int i = 1; i < n; i++){ 
     if(n==0){ 
      pow = 1; 
     } 
     pow = pow*2; 
    } 
    return pow; 
} 

private int pow2(int n) { //Better name, to avoid confusion 
    return 1 << n; 
} 

虽然我不能看到你使用此功能。

0

有许多缩短代码的方法(Eric和Mihir的建议是一个好的开始),但我会专注于清晰度。像x1和y1这样的变量尖叫一个Point结构来将这些对保存在一起。然后,您可以将这些点传递给一个数组。