java hilbert空间算法_Java中空间填充Hilbert曲线的递推算法

我正在学习用

Java编写代码,我已经学习了java编程的递归部分.我理解了递归方法的基础知识,并且我正在尝试编写填充希尔伯特曲线(和Levy C曲线)的空间,到目前为止,一切都顺利进行,直到实际的递归部分.我在提出递归方法时遇到了麻烦,想知道是否有人可以帮助我.我也知道它需要在DrawHilbert方法中.

public class HilbertCurve extends JPanel {

int N;

/**

* Constructor for Hilbert Curve

*/

public HilbertCurve ()

{

Scanner myKeyboard = new Scanner(System.in);

System.out.println("Enter an integer number to indicate the level of recursive depth: ");

N = myKeyboard.nextInt();

// Create a JFrame - a window that will appear on your screen

JFrame f = new JFrame();

// Tells the program to quit if you close the window

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Puts your drawing into the window (the JFrame)

f.add(new JScrollPane(this));

// Changes the size of the window on the screen

f.setSize(600, 600);

// Changes where the window will appear on your screen

f.setLocation(200, 200);

// Makes the window appear

f.setVisible(true);

}

public void setupHilbert (Turtle turtle)

{

turtle.penup();

turtle.setXY(0,0);

// draw a simple rectangle that is 100x50 pixels

turtle.pendown();

drawHilbert(turtle, N);

}

public void drawHilbert(Turtle turtle, int n) {

if (n == 0) return;

turtle.changeColor(Color.GREEN);

turtle.changeWidth(2);

turtle.left(-90);

turtle.forward(100);

turtle.left(90);

turtle.forward(100);

turtle.left(90);

turtle.forward(100);

turtle.left(-90);

turtle.penup();

}

protected void paintComponent(Graphics g)

{

Turtle turtle = new Turtle((Graphics2D) g, getBounds());

turtle.setHeadingMode(Turtle.DEGREE);

setupHilbert(turtle);

}

// plot a Hilbert curve of order N

public static void main(String[] args)

{

Scanner myKeyboard = new Scanner(System.in);

HilbertCurve test = new HilbertCurve();

}

}

递归的标志是一个自称的函数.我希望在某个地方看到drawHilbert()的调用,但我没有.

密切注意你的停止条件,否则你最终会得到一个OutOfMemoryError,因为递归调用会永久地添加到堆栈中.

我不熟悉你的问题,但这会是你错过的吗?

public void drawHilbert(Turtle turtle, int n) {

if (n == 0) return;

turtle.changeColor(Color.GREEN);

turtle.changeWidth(2);

turtle.left(-90);

turtle.forward(100);

turtle.left(90);

turtle.forward(100);

turtle.left(90);

turtle.forward(100);

turtle.left(-90);

turtle.penup();

drawHilbert(turtle, --n); // is this where your recursion should go?

}

更新:这个网站看起来很相关.

http://people.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-2_themes-hilbert-sec.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值