@karacique

Почему рисуется другая фигура?

Следующий код выдаёт правильную картинку
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace RefactorMe
{
    internal class Painter
    {
        private static float x, y;
        private static Graphics graph;

        public static void Initialization(Graphics newGraph)
        {
            graph = newGraph;
            graph.SmoothingMode = SmoothingMode.None;
            graph.Clear(Color.Black);
        }

        public static void Set_position(float x0, float y0)
        { x = x0; y = y0; }

        public static void MakeIt(Pen pen, double lenght, double angle)
        {
            //Делает шаг длиной lenght в направлении angle и рисует пройденную траекторию
            float x1 = (float)(x + (lenght * Math.Cos(angle)));
            float y1 = (float)(y + (lenght * Math.Sin(angle)));
            graph.DrawLine(pen, x, y, x1, y1);
            x = x1;
            y = y1;
        }

        public static void Change(double lenght, double angle)
        {
            x = (float)(x + (lenght * Math.Cos(angle)));
            y = (float)(y + (lenght * Math.Sin(angle)));
        }
    }

    public class ImpossibleSquare
    {
        public static void Draw(int width, int height, double turnAngle, Graphics graph)
        {
            // turnAngle пока не используется, но будет использоваться в будущем
            Painter.Initialization(graph);

            var size = Math.Min(width, height);

            var diagonal_length = Math.Sqrt(2) * (size * 0.375f + size * 0.04f) / 2;
            var x0 = (float)(diagonal_length * Math.Cos(Math.PI / 4 + Math.PI)) + width / 2f;
            var y0 = (float)(diagonal_length * Math.Sin(Math.PI / 4 + Math.PI)) + height / 2f;

            Painter.Set_position(x0, y0);
            //Рисуем 1-ую сторону
            Painter.MakeIt(Pens.Yellow, size * 0.375f, 0);
            Painter.MakeIt(Pens.Yellow, size * 0.04f * Math.Sqrt(2), Math.PI / 4);
            Painter.MakeIt(Pens.Yellow, size * 0.375f, Math.PI);
            Painter.MakeIt(Pens.Yellow, size * 0.375f - size * 0.04f, Math.PI / 2);

            Painter.Change(size * 0.04f, -Math.PI);
            Painter.Change(size * 0.04f * Math.Sqrt(2), 3 * Math.PI / 4);
            
            //Рисуем 2-ую сторону
            Painter.MakeIt(Pens.Yellow, size * 0.375f, -Math.PI / 2);
            Painter.MakeIt(Pens.Yellow, size * 0.04f * Math.Sqrt(2), -Math.PI / 2 + Math.PI / 4);
            Painter.MakeIt(Pens.Yellow, size * 0.375f, -Math.PI / 2 + Math.PI);
            Painter.MakeIt(Pens.Yellow, size * 0.375f - size * 0.04f, -Math.PI / 2 + Math.PI / 2);

            Painter.Change(size * 0.04f, -Math.PI / 2 - Math.PI);
            Painter.Change(size * 0.04f * Math.Sqrt(2), -Math.PI / 2 + 3 * Math.PI / 4);

            //Рисуем 3-ю сторону
            Painter.MakeIt(Pens.Yellow, size * 0.375f, Math.PI);
            Painter.MakeIt(Pens.Yellow, size * 0.04f * Math.Sqrt(2), Math.PI + Math.PI / 4);
            Painter.MakeIt(Pens.Yellow, size * 0.375f, Math.PI + Math.PI);
            Painter.MakeIt(Pens.Yellow, size * 0.375f - size * 0.04f, Math.PI + Math.PI / 2);

            Painter.Change(size * 0.04f, Math.PI - Math.PI);
            Painter.Change(size * 0.04f * Math.Sqrt(2), Math.PI + 3 * Math.PI / 4);

            //Рисуем 4-ую сторону
            Painter.MakeIt(Pens.Yellow, size * 0.375f, Math.PI / 2);
            Painter.MakeIt(Pens.Yellow, size * 0.04f * Math.Sqrt(2), Math.PI / 2 + Math.PI / 4);
            Painter.MakeIt(Pens.Yellow, size * 0.375f, Math.PI / 2 + Math.PI);
            Painter.MakeIt(Pens.Yellow, size * 0.375f - size * 0.04f, Math.PI / 2 + Math.PI / 2);

            Painter.Change(size * 0.04f, Math.PI / 2 - Math.PI);
            Painter.Change(size * 0.04f * Math.Sqrt(2), Math.PI / 2 + 3 * Math.PI / 4);
        }
    }
}

6326ca330820b489834836.jpeg

Я попытался изменить способ рисования сторон, чтобы сократить код.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace RefactorMe
{
    internal class Painter
    {
        private static float x, y;
        private static Graphics graph;

        public static void Initialization(Graphics newGraph)
        {
            graph = newGraph;
            graph.SmoothingMode = SmoothingMode.None;
            graph.Clear(Color.Black);
        }

        public static void Set_position(float x0, float y0)
        { x = x0; y = y0; }

        public static void MakeIt(Pen pen, double lenght, double angle)
        {
            //Делает шаг длиной lenght в направлении angle и рисует пройденную траекторию
            float x1 = (float)(x + (lenght * Math.Cos(angle)));
            float y1 = (float)(y + (lenght * Math.Sin(angle)));
            graph.DrawLine(pen, x, y, x1, y1);
            x = x1;
            y = y1;
        }

        public static void Change(double lenght, double angle)
        {
            x = (float)(x + (lenght * Math.Cos(angle)));
            y = (float)(y + (lenght * Math.Sin(angle)));
        }
    }

    public class ImpossibleSquare
    {
        public static void Draw(int width, int height, double turnAngle, Graphics graph)
        {
            // turnAngle пока не используется, но будет использоваться в будущем
            Painter.Initialization(graph);

            var size = Math.Min(width, height);

            var diagonal_length = Math.Sqrt(2) * (size * 0.375f + size * 0.04f) / 2;
            var x0 = (float)(diagonal_length * Math.Cos(Math.PI / 4 + Math.PI)) + width / 2f;
            var y0 = (float)(diagonal_length * Math.Sin(Math.PI / 4 + Math.PI)) + height / 2f;

            Painter.Set_position(x0, y0);
            for (int side = -1; side < 3; side++)
            {
            
            Painter.MakeIt(Pens.Yellow, size * 0.375f, (side * Math.PI / 2));
            Painter.MakeIt(Pens.Yellow, size * 0.04f * Math.Sqrt(2), (side * Math.PI / 2) + Math.PI / 4);
            Painter.MakeIt(Pens.Yellow, size * 0.375f, (side * Math.PI / 2) + Math.PI);
            Painter.MakeIt(Pens.Yellow, size * 0.375f - size * 0.04f, (side * Math.PI / 2) + Math.PI / 2);

            Painter.Change(size * 0.04f, (side * Math.PI / 2) - Math.PI);
            Painter.Change(size * 0.04f * Math.Sqrt(2), (side * Math.PI / 2) + (3 * Math.PI / 4));
            }

        }
    }
}

Должно строиться то же самое, только в другом порядке (2, 1, 4, 3), но стороны квадрата рисуются не там где надо6326cc5a95b14560074354.jpeg

Как изменить второй код, чтобы рисовалась правильная фигура?
  • Вопрос задан
  • 77 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы