java - Draw koch snowflake using recursion -


i trying write code koch recursion method. made far cant seem figure out how turn turtle , actual snowflake shape.

any sort of explanation appreciated, thanks!

import java.awt.color;   public class triangle {      public static void main(string[] args) {         world myworld = new world(900,900,color.green);         turtle bob = new turtle(myworld);         bob.setdelay(0);         //drawtriangle(bob, 4, -200,-100,405,-100,100,350.75);          koch(bob, 3, 12.0);      }       public static void koch(turtle t, int n, double size) {         if(n==0)             t.forward(size);         else         {             koch(t, n-1, size);             t.left(60);             koch(t, n-1, size);             t.right(120);             koch(t, n-1, size);             t.left(60);             koch(t, n-1, size);         }        } 

one simple approach replace:

koch(bob, 3, 12.0); 

with:

koch(bob, 3, 12.0); bob.right(120); koch(bob, 3, 12.0); bob.right(120); koch(bob, 3, 12.0); 

or equivalent loop:

for (int = 0; < 3; i++) {     koch(bob, 3, 12.0);     bob.right(120); } 

output

enter image description here


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -