M
M
Michael Sne Bjorn Palagin2017-07-02 04:27:19
C++ / C#
Michael Sne Bjorn Palagin, 2017-07-02 04:27:19

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):

My code
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);
        }
    }

Further in the next lesson, the teacher showed how he made this snowflake and then I had a stupor and not understanding. In the teacher's code, I left comments, there are questions!
Teacher code
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();
            }

        }

And if someone does not understand, the question specifically concerns this: Draw(len / 3, n - 1); // How the turtle draws if there is no command Turtle.Move();
How does the turtle draw a line if there is no Turtle.Move() command for it;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2017-07-02
@ven000mus

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 question

Ask a Question

731 491 924 answers to any question