gpt4 book ai didi

java - 使用递归绘制树

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:32:11 25 4
gpt4 key购买 nike

我正在尝试使用递归绘制一棵树。树需要看起来像这样:

Desired output

关于我应该如何做的简短总结:

  • 树干的长度为length,宽度为width
  • 树干分成两个 Twig
  • 左边的是躯干长度的 3/4,右边的是躯干长度的 2/3
  • 左枝宽度为树干宽度的3/4,右枝宽度为树干宽度的1/2
  • 我们收到的参数是长度、最小长度、宽度、alpha(所有 double )
  • Twig 生长直到 Twig 长于最小长度

这是我解决问题的方法。我只想画树干,左分支和右分支。我设法做到了这一点,具有以下功能:

public void drawTree(double length, double min_length, double width, double alpha) {
//Draws the trunk of the tree
StdDraw.setPenRadius(width);
StdDraw.line(250, 150, 250, 150+length);

//Left branch
double hypotenuse = (3.0/4.0)*length;
double opposite = Math.sin(alpha) * hypotenuse;
double adjacent = Math.sqrt(Math.pow(hypotenuse, 2)-Math.pow(opposite, 2));
StdDraw.setPenRadius(width*3.0/4.0);
StdDraw.line(250,150+length,250-adjacent,150+length+opposite);

//Right branch
double hypotenuse2 = (2.0/3.0)*length;
double opposite2 = Math.sin(alpha) * hypotenuse2;
double adjacent2 = Math.sqrt(Math.pow(hypotenuse2, 2)-Math.pow(opposite2, 2));
StdDraw.setPenRadius(width*1.0/2.0);
StdDraw.line(250,150+length,250+adjacent2,150+length+opposite2);

}

这是输出,正如我想要的那样:

First part

我以为剩下的会很容易,但在过去的 3 小时里我没有取得任何进展:/。我包括了停止条件的 if 语句。但是我对递归部分一无所知。我试过这个:

if (length > min_length) {
//Left branch
double hypotenuse = (3.0/4.0)*length;
double opposite = Math.sin(alpha) * hypotenuse;
double adjacent = Math.sqrt(Math.pow(hypotenuse, 2)-Math.pow(opposite, 2));
StdDraw.setPenRadius(width*3.0/4.0);
StdDraw.line(250,150+length,250-adjacent,150+length+opposite);
//Right branch
double hypotenuse2 = (2.0/3.0)*length;
double opposite2 = Math.sin(alpha) * hypotenuse2;
double adjacent2 = Math.sqrt(Math.pow(hypotenuse2, 2)-Math.pow(opposite2, 2));
StdDraw.setPenRadius(width*1.0/2.0);
StdDraw.line(250,150+length,250+adjacent2,150+length+opposite2);
//My first attempt
drawTree(hypotenuse*hypotenuse, min_length, width, alpha);
drawTree(hypotenuse2*hypotenuse2, min_length, width, alpha);
//My second attempt
drawTree(hypotenuse, min_length, width, alpha);
drawTree(hypotenuse2, min_length, width, alpha);
}

我理解简单的递归,例如阶乘、回文等,但我仍然坚持这一点,非常感谢任何帮助。

最佳答案

正如评论和当前答案中已经指出的那样,让 drawTree 方法不知道当前绘制的是树的哪一部分是至关重要的。

您不能在此方法中使用绝对坐标。而且您必须跟踪您当前所在的位置。例如,这可以通过将 Point2D 传递给描述当前树部分起点的递归方法来完成。

您甚至不需要明确的代码来绘制 Twig :请注意,单行 已经是一棵树。 Twig 只是“小树”:同样,它们是单行,但具有不同的长度和宽度。

(并且与之前的树相比有一定的角度。你没有提到这个,但是根据屏幕截图,角度似乎是Math.PI/5)

RecursiveTree

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.function.DoubleConsumer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class RecursiveTreeDrawing
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}

private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new BorderLayout());

RecursiveTreeDrawingPanel p = new RecursiveTreeDrawingPanel();
p.setPreferredSize(new Dimension(500,500));
f.getContentPane().add(p, BorderLayout.CENTER);

JPanel c = new JPanel(new GridLayout(0,1));

c.add(createControl("left length", 0, 0.9,
d -> p.setLeftLengthFactor(d)));
c.add(createControl("left width", 0, 0.9,
d -> p.setLeftWidthFactor(d)));
c.add(createControl("left angle", 0, Math.PI,
d -> p.setLeftAngleDelta(d)));

c.add(createControl("right length", 0, 0.9,
d -> p.setRightLengthFactor(d)));
c.add(createControl("right width", 0, 0.9,
d -> p.setRightWidthFactor(d)));
c.add(createControl("right angle", -Math.PI, 0,
d -> p.setRightAngleDelta(d)));
f.getContentPane().add(c, BorderLayout.SOUTH);

f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

private static JPanel createControl(
String name, double min, double max, DoubleConsumer doubleConsumer)
{
JPanel p = new JPanel(new GridLayout(1,0));
p.add(new JLabel(name));
JSlider slider = new JSlider(0, 100, 0);
slider.addChangeListener(new ChangeListener()
{

@Override
public void stateChanged(ChangeEvent e)
{
int value = slider.getValue();
double v = value / 100.0;
double d = min + v * (max - min);
doubleConsumer.accept(d);
}
});
p.add(slider);

return p;
}

}


class RecursiveTreeDrawingPanel extends JPanel
{
private double leftLengthFactor = 3.0 / 4.0;
private double leftWidthFactor = 3.0 / 4.0;
private double leftAngleDelta = Math.PI / 5.0;
private double rightLengthFactor = 2.0 / 3.0;
private double rightWidthFactor = 1.0 / 2.0;
private double rightAngleDelta = - Math.PI / 5.0;

@Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Point2D start = new Point2D.Double(
getWidth() * 0.5,
getHeight() * 0.7);
g.setColor(Color.GRAY);
drawTree(g, start, 100.0, 2.0, 10.0, 0);
}

private void drawTree(Graphics2D g,
Point2D start, double length, double minLength,
double width, double alpha)
{
if (length < minLength)
{
return;
}
g.setStroke(new BasicStroke((float)width,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
Point2D end = new Point2D.Double(
start.getX() + Math.sin(alpha + Math.PI) * length,
start.getY() + Math.cos(alpha + Math.PI) * length);
g.draw(new Line2D.Double(start, end));
drawTree(g, end, length * leftLengthFactor, minLength,
width * leftWidthFactor, alpha + leftAngleDelta);
drawTree(g, end, length * rightLengthFactor, minLength,
width * rightWidthFactor, alpha + rightAngleDelta);
}

public void setLeftLengthFactor(double leftLengthFactor)
{
this.leftLengthFactor = leftLengthFactor;
repaint();
}

public void setLeftWidthFactor(double leftWidthFactor)
{
this.leftWidthFactor = leftWidthFactor;
repaint();
}

public void setLeftAngleDelta(double leftAngleDelta)
{
this.leftAngleDelta = leftAngleDelta;
repaint();
}

public void setRightLengthFactor(double rightLengthFactor)
{
this.rightLengthFactor = rightLengthFactor;
repaint();
}

public void setRightWidthFactor(double rightWidthFactor)
{
this.rightWidthFactor = rightWidthFactor;
repaint();
}

public void setRightAngleDelta(double rightAngleDelta)
{
this.rightAngleDelta = rightAngleDelta;
repaint();
}

}

关于java - 使用递归绘制树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com