Hi,everyone Im new to JAVA and not sure how does recursion work in JAVA
I have to use JAVA Turtle to draw snowflake
here is the pseudocode.
procedure snowflakePart( S, N ) :
if N is greater than 0:
move turtle forward S units
if N is greater than 1:
# generate the 5 sub-branches of this snowflake part
turn turtle left 120 degrees
loop 5 times:
call snowflakePart( S/3, N-1 )
turn turtle right 60 degrees
turn turtle right 180 degrees
move turtle backward S units
procedure drawSnowflake( S, N ) :
loop 6 times:
call snowflakePart( S, N )
turn turtle left 60 degrees
public static void main(String[] args){
double x0 = 10;
double y0 = 10.0;
double a0 = 0.0;
Turtle turtle = new Turtle(x0, y0, a0);
double S;
double N;
Scanner sc= new Scanner(System.in);
System.out.print("Enter S: ");
S=sc.nextInt();
Scanner bc= new Scanner(System.in);
System.out.print("Enter N: ");
N=bc.nextInt();
snowflake(S,N,turtle);
}
public static void snowflake(double S, double N,Turtle turtle){
if (N>0){
turtle.goForward(S);
if(N>1){
turtle.turnLeft(120);
for (int g=1; g<=5; g++){
snowflake(S/3,N-1,turtle);
turtle.turnRight(60);
}
turtle.turnRight(180);
}
}
drawSnowflake(S, N,turtle);
}
public static void drawSnowflake(double S, double N,Turtle turtle){
for(int h=1; h<=6; h++){
snowflake(S, N,turtle);
turtle.turnLeft(60);
}
}
This is what I have so far. I also tried the pseudocode in python. I got it works fine.
here are the errors in JAVA.
at Turtle.drawSnowflake(Turtle.java:166)
at Turtle.snowflake(Turtle.java:161)
at Turtle.drawSnowflake(Turtle.java:166)
at Turtle.snowflake(Turtle.java:161)
at Turtle.drawSnowflake(Turtle.java:166)
Thanks.