使用 Java 在处理中构建 HTree
Posted
技术标签:
【中文标题】使用 Java 在处理中构建 HTree【英文标题】:Building HTree in Processing using Java 【发布时间】:2022-01-02 22:20:45 【问题描述】:我需要为练习构建一个 HTree。我很迷茫,因为我老师的教学方式有问题。无论如何,我们要做的是使用递归方法制作一个 HTree。我不知道如何开始,所以我在网上找到了一个方法,但是当我粘贴它时,它有一些错误。所以我设法删除了所有错误(至少是带下划线的错误)。当我运行代码时,它只是一个空白块。
void drawH(double x, double y, double size)
// compute the coordinates of the 4 tips of the H
double x0 = x - size/2;
double x1 = x + size/2;
double y0 = y - size/2;
double y1 = y + size/2;
// draw the 3 line segments of the H
draw(x0, y0, x0, y1);
draw(x1, y0, x1, y1);
draw(x0, y, x1, y);
void draw(int n, double x, double y, double size)
if (n == 0) return;
drawH(x, y, size);
// compute x- and y-coordinates of the 4 half-size H-trees
double x0 = x - size/2;
double x1 = x + size/2;
double y0 = y - size/2;
double y1 = y + size/2;
// recursively draw 4 half-size H-trees of order n-1
draw(n-1, x0, y0, size/2); // lower left H-tree
draw(n-1, x0, y1, size/2); // upper left H-tree
draw(n-1, x1, y0, size/2); // lower right H-tree
draw(n-1, x1, y1, size/2); // upper right H-tree
// reads an integer command-line argument n and plots an order n H-tree
void main()
int n = Integer.parseInt(args[0]);
double x = 0.5, y = 0.5; // center of H-tree
double size = 0.5; // side length of H-tree
draw(n, x, y, size);
【问题讨论】:
line
定义在哪里?
不是在底部吗(倒数第二行)?我们还需要定义 in,因为我认为它只是一个处理中的函数,它会产生一条线。
不,不是。您在倒数第二行调用 line
,您正在使用一个名为 line
的方法,该方法已在某处定义。
哦,那我用draw代替吗?
我的老师也给了我们他使用线的例子。我没有看到任何地方定义的线。我应该把它寄到这里吗?我不知道它看起来有多漂亮。
【参考方案1】:
在我看来,使用其他人的代码没有错,只要你:
-
相信人民
了解您尝试使用的代码
可选:愉快地破解/调整代码并将其与您自己的东西重新混合
您的代码有几个问题,很遗憾,我没有时间详细审查每个问题,但我会提到一些:
main()
没有被调用,也许你的意思是从 Processing 的 setup()
调用它?
最好检查args
是否不是null
并且它的长度至少为1 以便能够解析n
的值。
您不小心创建了无限递归循环(这将导致堆栈溢出):main()
调用 draw()
、draw()
调用 drawH()
、drawH()
调用 draw()
多次(这将调用 drawH()
将调用draw()
,依此类推)。
我假设您使用的是Princeton H-Tree example by Robert Sedgewick and Kevin Wayne。如果是这种情况,我只需将 double
交换为 float
类型,以便在处理中更轻松地绘制:
/*
* Based on Htree.java demo by Robert Sedgewick and Kevin Wayne
* original URL: https://introcs.cs.princeton.edu/java/23recursion/Htree.java.html
*/
void setup()
size(600, 600);
background(255);
main();
void drawH(float x, float y, float size)
// compute the coordinates of the 4 tips of the H
float x0 = x - size/2;
float x1 = x + size/2;
float y0 = y - size/2;
float y1 = y + size/2;
// draw the 3 line segments of the H
line(x0, y0, x0, y1);
line(x1, y0, x1, y1);
line(x0, y, x1, y);
void draw(int n, float x, float y, float size)
if (n == 0) return;
drawH(x, y, size);
// compute x- and y-coordinates of the 4 half-size H-trees
float x0 = x - size/2;
float x1 = x + size/2;
float y0 = y - size/2;
float y1 = y + size/2;
// recursively draw 4 half-size H-trees of order n-1
draw(n-1, x0, y0, size/2); // lower left H-tree
draw(n-1, x0, y1, size/2); // upper left H-tree
draw(n-1, x1, y0, size/2); // lower right H-tree
draw(n-1, x1, y1, size/2); // upper right H-tree
// reads an integer command-line argument n and plots an order n H-tree
void main()
// start with a default value
int n = 6;
// override that value with args if they exisst (e.g. sketch is launched via command line)
if(args != null && args.length > 0)
n = Integer.parseInt(args[0]);
println("parsed n=", n);
float x = width * 0.5, y = height * 0.5; // center of H-tree
int size = 600; // side length of H-tree
draw(n, x, y, size);
如果您想测试命令行参数,您需要:
将包含 processing-java 的文件夹(它位于 Processing 可执行文件所在的同一文件夹中)添加到PATH
环境变量(或者将 cd
作为临时选项添加到该目录中)
通过整数参数运行您的草图:例如processing-java --sketch=path/to/yourHTreeSketch --run 3
(其中 3 是示例递归级别)
(或者,您可以导出应用程序(通过 File > Export Application (Ctrl+Shift+E / CMD+Shift+E
) 并通过传递 int 参数的命令行运行)
我希望我有时间解释递归:这是一个非常有趣的话题!
那里确实有很好的资源。查看 Daniel Shiffman 的:
Recursive Tree example(请注意,当您将光标移动到右边缘时,草图看起来像 H-Tree:希望这在视觉上很直观) Fractal Trees - Recursive Coding Train youtube video祝你好运,玩得开心!
【讨论】:
是的,我正在使用该代码。对不起,下次我发布别人的代码时,我会给予表扬。以上是关于使用 Java 在处理中构建 HTree的主要内容,如果未能解决你的问题,请参考以下文章