用递归算法绘制分形图



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

我的代码:

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);
    }
}

}

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

I更新代码:

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);
} }}

我在此照片链接中显示我的新结果:新

但是第二个分支长度很长。我如何纠正它,就像我的第一张照片(目标链接)

else情况下的计算错误:

  • y2 = ...行中缺少一些parent
  • 将因子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);
    }
}

最新更新