Answer the question
In order to leave comments, you need to log in
Understanding code, new to C#. How?
Hello :) I'm just learning the C# language and taking lessons from the MVA. They gave a DZ there, draw Koch's Snowflake with the help of a turtle, after showing some of the turtle's commands. I did everything in this way (I know that there is a lot of code that could fit in many times less, but I'm only on the threshold of the C # house and therefore, having written this code, albeit a long one, but without anyone's help, I'm glad about this):
static void Main(string[] args)
{
var len = 5; // Размер линии
var ugol_small = -60; // Малый угол
var ugol_big = 120; // Большой угол
var ugol_end = 120; // Угол поворота всей части
Turtle.Speed = 10;
Turtle.TurnRight();
// Полноценная снежинка Коха
Side4(len, ugol_small, ugol_big);
Turtle.Turn(ugol_end);
Side4(len, ugol_small, ugol_big);
Turtle.Turn(ugol_end);
Side4(len, ugol_small, ugol_big);
}
private static void Side4(int len, int ugol_small, int ugol_big) // Четвертый кусок снежинки - Окончательный
{
Side3(len, ugol_small, ugol_big);
Turtle.Turn(ugol_small);
Side3(len, ugol_small, ugol_big);
Turtle.Turn(ugol_big);
Side3(len, ugol_small, ugol_big);
Turtle.Turn(ugol_small);
Side3(len, ugol_small, ugol_big);
}
private static void Side3(int len, int ugol_small, int ugol_big) // Третий кусок снежинки
{
Side2(len, ugol_small, ugol_big);
Turtle.Turn(ugol_small);
Side2(len, ugol_small, ugol_big);
Turtle.Turn(ugol_big);
Side2(len, ugol_small, ugol_big);
Turtle.Turn(ugol_small);
Side2(len, ugol_small, ugol_big);
}
private static void Side2(int len, int ugol_small, int ugol_big) // Второй кусок снежинки
{
Side1(len, ugol_small, ugol_big);
Turtle.Turn(ugol_small);
Side1(len, ugol_small, ugol_big);
Turtle.Turn(ugol_big);
Side1(len, ugol_small, ugol_big);
Turtle.Turn(ugol_small);
Side1(len, ugol_small, ugol_big);
}
private static void Side1(int len, int ugol_small, int ugol_big) // Первый кусок снежинки
{
Turtle.Move(len);
Turtle.Turn(ugol_small);
Turtle.Move(len);
Turtle.Turn(ugol_big);
Turtle.Move(len);
Turtle.Turn(ugol_small);
Turtle.Move(len);
}
}
static void Main(string[] args)
{
Turtle.Speed = 10;
for (int i = 0; i < 3; i++)
{
Draw(150, 3); // Тут по моему понимаю, задались параметры для процедуры Draw
Turtle.Turn(120);
}
}
public static void Draw(int len, int n) // Тут он создал процедуру Draw
{
if (n == 0) Turtle.Move(len); // Тут я понял, что если переменная n равна нулю, то всего лишь рисовать линию длинной равной переменной len
else // А вот тут у меня ступор пошел.
{
Draw(len / 3, n - 1); // Как черепашка рисует, если нет команды Turtle.Move();
Turtle.Turn(-60);
Draw(len / 3, n - 1); // Как черепашка рисует, если нет команды Turtle.Move();
Turtle.Turn(120);
Draw(len / 3, n - 1); // Как черепашка рисует, если нет команды Turtle.Move();
Turtle.Turn(-60);
Draw(len / 3, n - 1); // Как черепашка рисует, если нет команды Turtle.Move();
}
}
Answer the question
In order to leave comments, you need to log in
there is a recursion.
public static void Draw(int len, int n) {
...
Draw(len / 3, n - 1);
...
Here Draw means that the Draw function will be called again, but with "n-1"
If n is equal to 0, and this happens on the third nested Draw call, then the command with move will be called.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question