使用递归算法绘制分形图形

3

我写了以下代码来绘制一棵分形树,像照片一样。但是在第二个递归方法中遇到了问题(用于控制中间分支的长度)。我该如何改进和纠正它?

我的代码:

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;

public class FractalTree1 extends Canvas {

// fields for drawing
private final JFrame frame;
private final int WINDOW_WIDTH = 1280;
private final int WINDOW_HEIGHT = 720;

public FractalTree1() {
    frame = new JFrame("Fractal Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    frame.setLocationRelativeTo(null);
    frame.setResizable(true);
    Component add = frame.add(this);
    frame.setVisible(true);
}

public static void main(String[] args) {
    FractalTree1 ft = new FractalTree1();
    ft.setVisible(true);
    ft.setBackground(Color.black);
}

@Override
public void paint(Graphics g) {
    g.setColor(Color.green);
    drawFractalTree(g, WINDOW_WIDTH / 2, WINDOW_HEIGHT - 75, -90, 2, true);
}

public void drawFractalTree(Graphics g, int x1, int y1, double angle, int depth, boolean value) {
    if (depth == 0) {
    } else {
        int x2, y2;
        if (value) {
            x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 100.0);
            y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 100.0);
        } else {
            x2 = (int) ((x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 100.0)) * 0.3);
            y2 = (int) (y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 100.0) * 0.3);
        }

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setStroke(new BasicStroke(0.5f * depth));
        g2d.drawLine(x1, y1, x2, y2);

        drawFractalTree(g, x2, y2, angle + 10, depth - 1, true);
        drawFractalTree(g, x2, y2, angle - 35, depth - 1, false);
        drawFractalTree(g, x2, y2, angle - 70, depth - 1, true);
    }
}

}

我的目标照片: 我的目标照片

我更新了代码:

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;

public class FractalTree1 extends Canvas {

// fields for drawing
private final JFrame frame;
private final int WINDOW_WIDTH = 1280;
private final int WINDOW_HEIGHT = 720;

public FractalTree1() {
    frame = new JFrame("Fractal Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    frame.setLocationRelativeTo(null);
    frame.setResizable(true);
    Component add = frame.add(this);
    frame.setVisible(true);
}

public static void main(String[] args) {
    FractalTree1 ft = new FractalTree1();
    ft.setVisible(true);
    ft.setBackground(Color.black);
}

@Override
public void paint(Graphics g) {
    g.setColor(Color.green);
    drawFractalTree((Graphics2D) g, WINDOW_WIDTH / 2, WINDOW_HEIGHT - 75, -90, 3,100, 1.0);
}

public void drawFractalTree(Graphics2D g, int x1, int y1, double angle, int depth, double size, double factor) {
if (depth > 0) {
    int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * size * factor);
    int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * size * factor);

    g.setStroke(new BasicStroke(0.5f * depth));
    g.drawLine(x1, y1, x2, y2);

    drawFractalTree(g, x2, y2, angle + 10, depth - 1, size, 1.0);
    drawFractalTree(g, x2, y2, angle - 35, depth - 1, size, 0.3);
    drawFractalTree(g, x2, y2, angle - 70, depth - 1, size, 1.0);
} }}

我在这个照片链接中展示了我的新成果: new

但第二个分支的长度是错误的。我该如何像第一张照片(目标链接)那样进行更正?


@tobias_k 这段代码的结果是 false :( - masoodtav
1
是的,我也猜到了。假的怎么做?顺便说一句,在y2 =行中你缺少一对括号,但似乎还有更多问题... - tobias_k
1个回答

5

你的else情况中的计算是错误的:

  • y2 = ... 行缺少一些括号
  • 你将因子0.3应用于分支的端点而不是它的长度

尝试这样做:

} else {
    x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 100.0 * 0.3);
    y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 100.0 * 0.3);
}

这可能需要微调不同大小和角度参数,但需要与之配合。
此外,您可以通过提供更多参数并将抗锯齿设置移出方法来简化您的方法:
public void drawFractalTree(Graphics2D g, int x1, int y1, double angle, int depth, double size, double factor) {
    if (depth > 0) {
        int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * size * factor);
        int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * size * factor);

        g.setStroke(new BasicStroke(0.5f * depth));
        g.drawLine(x1, y1, x2, y2);

        drawFractalTree(g, x2, y2, angle + 10, depth - 1, size, 1.0);
        drawFractalTree(g, x2, y2, angle - 35, depth - 1, size, 0.3);
        drawFractalTree(g, x2, y2, angle - 70, depth - 1, size, 1.0);
    }
}

谢谢 :) 但是我还有其他问题。如果你用深度为3或5绘制这段代码,你会发现中间的分支是错误的。因为它的长度不正确(请参见我的目标图片:http://i.stack.imgur.com/Tn2Q7.png)。 - masoodtav
这对我来说看起来没问题。你确定你把因子移到圆括号里了,这样它只适用于长度吗?也许你应该发布一个截图,看看现在的效果如何。 - tobias_k
@tobais_k 我更新了我的代码,也更新了我的问题。我还上传了新代码的结果。 - masoodtav
@masoodtav 正如我所说,您需要稍微调整角度、长度和因素。 - tobias_k

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接