I can not understand by what principle this code works. This is the first part of the code:

* @author ZTILabPI */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here TurtleAlgorithms alg = new TurtleAlgorithms(); alg.positioningTurtle(30); // alg. sierpinski(bok, minBok); alg.sierpinski(200, 50); // alg.callSnowFlake(3, 400); // alg.snowFlake(3, 400); } } 

This is the second part of the code:

 package Turtle; import turtlePck.TurtleGraphicsWindow; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author ZTILabPI */ public class TurtleAlgorithms extends TurtleGraphicsWindow{ public void positioningTurtle(int angle){ right(angle); } public void sierpinski(int bok, int minBok ){ if (bok<minBok) { return; } for(int i=1;i<4;i++){ sierpinski(bok/2, minBok); forward(bok); right(120); } } } 

It turns out that this code works, it draws the Sierpinski triangle, but I cannot understand by what principle.

  • Change the different parameters, see what happens - michael_best
  • @Misha Kotor Yes, I already did it, and I tried to use it gradually, but I still get some kind of nonsense, but for the university I need to draw a certain figure. And without understanding the principle of operation of the code, I will not be able to do this - BraFik

1 answer 1

What will the sierpinski procedure sierpinski if you remove the recursive call from it?

With the help of turtle methods, she draws an equilateral triangle with side bok . It's clear?

What does a recursive call do? He draws a triangle in each vertex half as much.

And what does a recursive call inside a recursive call do? He draws a quarter-sized triangle at each vertex of the half triangle.

And so it continues until the size becomes too small (the condition for stopping the recursion works).

In what order are the triangles drawn? First the smallest inside the first vertex. Then the side of the triangle is slightly larger. Then again the smallest, then the bigger side, again the smaller one, and the completion of the triangle is bigger, then the third side from the end, etc.
This order (at least the size) should be displayed in the console or a text box (which is available there) in order to better understand how recursion works.

  • What is a debugging printout? - Anton Sorokin
  • @Anton Sorokin Debug Print - output text, values ​​of variables to the console, text box, to a file. The term unsuccessful? - MBo
  • I do not even know. By the "debug print" in the morning Google did not give anything, by the "debug print" - issued info. Those. like norms. On the other hand, it is not used anywhere, and I would not have understood without Google what it is (and newbies in Google do not climb). - Anton Sorokin
  • @Anton Sorokin OK, reformulated. - MBo
  • @MBo How do I get the NetBeans console data available to me? - BraFik