Java applet turtle -


so got turtle applet , saw code on web.

private void tree(int s) {    if (s < 8)       return;     forward(s);    left(45);    tree(s / 2);    right(90);    tree(s / 2);    left(45);    back(s); } 

this result should like, in mind turtle stops red circle is...

enter image description here

can explain why turtle going further and why turtle starts 2 subtrees ?

because if understood code right turtle move forward s steps , turn left 45 degrees not right...

the turtle should end started. let's dissect code line-by-line:

forward(s); 

this draws vertical trunk of tree.

left(45); 

we turn left we're pointing upper-left corner.

tree(s / 2); 

we recursively call tree half base length, causing new tree branch off our trunk in direction specified (45 degrees left). notice tree continue recursively branch out until base length becomes less 8 units (pixels?) long. afterwards, returns place split from, base of branch.

right(90); 

after finish drawing left branch, turn right 90 degrees we're pointing upper-right corner.

tree(s / 2); 

like before, split off , begin drawing tree in direction half base length.

left(45); 

we turn left 45 degrees reorient ourselves point straight up.

back(s); 

the turtle returns base of tree.


altogether, code branches out left @ 45 degree angles half base length until branches become short; afterward, spirals outward while drawing remaining branches (left first, right).


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -