使用 Java 在 Processing 中构建 HTree
Building HTree in Processing using Java
我需要为练习构建一个 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);
}
在我看来使用其他人的代码没有错,只要你:
- 相信人民
- 了解您尝试使用的代码
- 可选:享受 hacking/tweaking 代码并将其与您自己的东西重新混合
你的代码有几个问题,遗憾的是我没有时间详细检查每一个,但我会提到几个:
main()
未被调用,也许您打算从 Processing 的 setup()
中调用它?
- 最好检查
args
是否不是 null
并且它的长度至少为 1 以便能够解析 n
. 的值
- 您不小心创建了无限递归循环(这将导致 Whosebug):
main()
调用 draw()
、draw()
调用 drawH()
、drawH()
调用 draw()
几次(将调用 drawH()
,将调用 draw()
,依此类推)。
我假设您正在使用 Princeton H-Tree example by Robert Sedgewick and Kevin Wayne。如果是这种情况,我只是将 double
交换为 float
类型,以便在 Processing 中更轻松地绘制:
/*
* 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 树:希望这是直观的)
- Fractal Trees - Recursive Coding Train youtube video
祝你好运,玩得开心!
我需要为练习构建一个 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);
}
在我看来使用其他人的代码没有错,只要你:
- 相信人民
- 了解您尝试使用的代码
- 可选:享受 hacking/tweaking 代码并将其与您自己的东西重新混合
你的代码有几个问题,遗憾的是我没有时间详细检查每一个,但我会提到几个:
main()
未被调用,也许您打算从 Processing 的setup()
中调用它?- 最好检查
args
是否不是null
并且它的长度至少为 1 以便能够解析n
. 的值
- 您不小心创建了无限递归循环(这将导致 Whosebug):
main()
调用draw()
、draw()
调用drawH()
、drawH()
调用draw()
几次(将调用drawH()
,将调用draw()
,依此类推)。
我假设您正在使用 Princeton H-Tree example by Robert Sedgewick and Kevin Wayne。如果是这种情况,我只是将 double
交换为 float
类型,以便在 Processing 中更轻松地绘制:
/*
* 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 树:希望这是直观的)
- Fractal Trees - Recursive Coding Train youtube video
祝你好运,玩得开心!