0

私は最初のフラクタル(ピタゴラスの木)を構築しようとしています:

代替テキストhttp://img13.imageshack.us/img13/926/lab6e.jpg

Graphics2Dを使用したJavaで。これが私が今持っているものです:

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

public class Main {

public static void main(String[] args) {

    int i=0;
    Scanner scanner = new Scanner(System.in);

    System.out.println("Give amount of steps: ");
    i = scanner.nextInt();

    new Pitagoras(i);
    }
}

class Pitagoras extends JFrame {

private int powt, counter;

public Pitagoras(int i) {
    super("Pythagoras Tree.");
    setSize(1000, 1000);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    powt = i;
}

private void paintIt(Graphics2D g) {

    double p1=450, p2=800, size=200;

    for (int i = 0; i < powt; i++) {
        if (i == 0) {
            g.drawRect((int)p1, (int)p2, (int)size, (int)size);
            counter++;
        }
        else{
            if( i%2 == 0){
                //here I must draw two squares
            }
            else{
                //here I must draw right triangle
            }
        }
    }
}

@Override
public void paint(Graphics graph) {

    Graphics2D g = (Graphics2D)graph;
    paintIt(g);

}

つまり、基本的にステップ数を設定してから、最初の正方形(p1、p2、サイズ)を描画します。次に、ステップが奇妙な場合は、正方形の上に直角三角形を作成する必要があります。ステップが均等な場合は、三角形の自由辺に2つの正方形を作成する必要があります。三角形と正方形の両方を描画するには、今どの方法を選択する必要がありますか?単純な線で三角形を描画してAffineTransformで変換することを考えていましたが、それが実行可能かどうかはわかりません。また、正方形の描画は解決されません。

4

2 に答える 2

5

このツリーでは、三角形を描く必要はなく、四角形 (四角形の端が三角形) だけを描く必要があります。

再帰を調べると、物事がずっと簡単になります (これらのタイプのフラクタルは、再帰の標準的な例です)。

疑似コードで

drawSquare(coordinates) {
    // Check break condition (e.g. if square is very small)
    // Calculate coordinates{1|2} of squares on top of this square -> Pythagoras
    drawSquare(coordinates1)
    drawSquare(coordinates2)
}

私はよくフラクタルをプログラミングしているので、ヒント: フラクタル自体を BufferedImage に描画し、ペイント メソッドで画像のみをペイントします。paint-Method は 1 秒間に数回呼び出される可能性があるため、非常に高速である必要があります。

また、JFrame を直接描画するのではなく、Canvas (awt を使用する場合) または JPanel (swing を使用する場合) を使用してください。

于 2010-04-26T14:32:59.170 に答える
1

私の最終的な解決策:

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);
        }
    }
}
于 2010-05-22T17:11:10.673 に答える