pevel

Untitled

Sep 20th, 2022
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | Source Code | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4.  
  5.  
  6. namespace RefactorMe
  7. {
  8.     class Painter
  9.     {
  10.         static float penСoordinateX, penСoordinateY;
  11.         static Graphics graphics;
  12.  
  13.         public static void Initialize(Graphics newGraphics)
  14.         {
  15.             graphics = newGraphics;
  16.             graphics.SmoothingMode = SmoothingMode.None;
  17.             graphics.Clear(Color.Black);
  18.         }
  19.  
  20.         public static void SetPosition(float x0, float y0)
  21.         {
  22.             penСoordinateX = x0;
  23.             penСoordinateY = y0;
  24.         }
  25.  
  26.         public static void Draw(Pen pen, double length, double angle)
  27.         {
  28.             //Делает шаг длиной length в направлении angle и рисует пройденную траекторию
  29.             var x1 = (float)(penСoordinateX + length * Math.Cos(angle));
  30.             var y1 = (float)(penСoordinateY + length * Math.Sin(angle));
  31.             graphics.DrawLine(pen, penСoordinateX, penСoordinateY, x1, y1);
  32.             penСoordinateX = x1;
  33.             penСoordinateY = y1;
  34.         }
  35.  
  36.         public static void ChangeСoordinatesPen(double length, double angle)
  37.         {
  38.             penСoordinateX = (float)(penСoordinateX + length * Math.Cos(angle));
  39.             penСoordinateY = (float)(penСoordinateY + length * Math.Sin(angle));
  40.         }
  41.     }
  42.  
  43.     public class ImpossibleSquare
  44.     {
  45.         const float SizeWidth = 0.375f;
  46.         const float FrameSize = 2f;
  47.         const float SizeCornerLine = 0.04f;
  48.  
  49.         public static void DrawSide(double drawSize, double angle)
  50.         {
  51.             var penColor = Pens.Red;
  52.             var drawingWidth = drawSize * SizeWidth;
  53.             var cornerDrawingWidth = drawSize * SizeCornerLine;
  54.  
  55.             Painter.Draw(penColor, drawingWidth, angle);
  56.             Painter.Draw(penColor, cornerDrawingWidth * Math.Sqrt(2), angle + Math.PI / 4);
  57.             Painter.Draw(penColor, drawingWidth, angle + Math.PI);
  58.             Painter.Draw(penColor, drawingWidth - cornerDrawingWidth, angle + Math.PI / 2);
  59.  
  60.             Painter.ChangeСoordinatesPen(cornerDrawingWidth, angle + Math.PI);
  61.             Painter.ChangeСoordinatesPen(cornerDrawingWidth * Math.Sqrt(2), angle + 3 * Math.PI / 4);
  62.         }
  63.         public static void Draw(int width, int height, double angleRotate, Graphics graphics)
  64.         {
  65.             Painter.Initialize(graphics);
  66.  
  67.             var minSide = Math.Min(width, height);
  68.  
  69.             var diagonalLength = Math.Sqrt(2) * (minSide * (SizeWidth + SizeCornerLine)) / 2;
  70.             var x0 = (float)(diagonalLength * Math.Cos(5 * Math.PI / 4)) + width / FrameSize;
  71.             var y0 = (float)(diagonalLength * Math.Sin(5 * Math.PI / 4)) + height / FrameSize;
  72.  
  73.             //var radius = (float)diagonalLength/2;  
  74.             Painter.SetPosition(x0, y0);
  75.  
  76.             DrawSide(minSide, 0 + angleRotate);
  77.             DrawSide(minSide, -Math.PI / 2 + angleRotate);
  78.             DrawSide(minSide, Math.PI + angleRotate);
  79.             DrawSide(minSide, Math.PI / 2 + angleRotate);
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment