Recursive algorithm of space filled Hilbert curve in Java

I'm learning to write code in Java. I've learned the recursive part of Java programming I understand the basics of recursive methods, and I'm trying to write a space to fill the Hilbert curve (and levy C curve). So far, everything has gone well until the actual recursive part I'm having trouble proposing recursive methods and wonder if anyone can help me I also know that it needs to be in the drawhilbert method

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

}

Solution

The flag of recursion is a self - proclaimed function I wanted to see a call to drawhilbert () somewhere, but I didn't

Pay close attention to your stop condition, otherwise you will eventually get an outofmemoryerror, because the recursive call will be permanently added to the stack

I'm not familiar with your problem, but will you miss it?

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?
}

Update: this website looks very relevant

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>