Advertisement
Guest User

Untitled

a guest
Aug 13th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3.  
  4. namespace IMJunior
  5. {
  6.     class Program
  7.     {
  8.         static Bitmap Canvas = new Bitmap(100, 100);
  9.         static Graphics Drawer;
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             Drawer = Graphics.FromImage(Canvas);
  14.             Drawer.FillRectangle(Brushes.White, new Rectangle(0, 0, 100, 100));
  15.  
  16.             PyramidCreator creator = new PyramidCreator();            
  17.             IDrawMesh[] meshes = creator.CreatePyramid(4, 98, 100, Color.Blue);
  18.             for (int i = 0; i < meshes.Length; i++)
  19.             {
  20.                 meshes[i].Draw(Drawer);
  21.             }
  22.  
  23.             Canvas.Save("canvas.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
  24.         }
  25.  
  26.        
  27.     }
  28.  
  29.     public interface IDrawLine
  30.     {
  31.         void Draw(Point startPoint, Point endPoint, Graphics drawer);
  32.     }
  33.  
  34.     public interface IDrawMesh
  35.     {
  36.         void Draw(Graphics drawer);
  37.     }
  38.    
  39.  
  40.     public class Point
  41.     {
  42.         public int coordX;
  43.         public int coordY;
  44.         public Color color;
  45.  
  46.         public Point(int coordX, int coordY, Color color)
  47.         {
  48.             this.coordX = coordX;
  49.             this.coordY = coordY;
  50.             this.color = color;
  51.         }
  52.     }
  53.  
  54.     public class Line : IDrawLine
  55.     {        
  56.         public void Draw(Point startPoint, Point endPoint, Graphics drawer)
  57.         {
  58.             Pen pen = new Pen(startPoint.color, 2);
  59.             drawer.DrawLine(pen, startPoint.coordX, startPoint.coordY, endPoint.coordX, endPoint.coordY);
  60.         }
  61.     }
  62.  
  63.     public class Triangle : IDrawMesh
  64.     {
  65.         Point[] points = new Point[3];
  66.  
  67.         public Triangle(Point[] points)
  68.         {
  69.             this.points = points;
  70.         }
  71.  
  72.         public void Draw(Graphics drawer)
  73.         {
  74.             throw new NotImplementedException();
  75.         }
  76.     }
  77.  
  78.     public class Circle : IDrawMesh
  79.     {
  80.         Point center;
  81.         int radius;
  82.         Color color;
  83.  
  84.         public Circle(Point center, int radius, Color color)
  85.         {
  86.             this.center = center;
  87.             this.radius = radius;
  88.             this.color = color;
  89.         }
  90.  
  91.         public void Draw(Graphics drawer)
  92.         {
  93.             throw new NotImplementedException();
  94.         }
  95.     }
  96.  
  97.     public class Square : IDrawMesh
  98.     {
  99.         Point[] points = new Point[4];
  100.  
  101.         public Square(Point pointFirst, Point pointSecond, Point pointThird, Point pointFourth)
  102.         {
  103.             points[0] = pointFirst;
  104.             points[1] = pointSecond;
  105.             points[2] = pointThird;
  106.             points[3] = pointFourth;
  107.         }
  108.  
  109.         public void Draw(Graphics drawer)
  110.         {
  111.             Line line = new Line();
  112.             for (int i = 0; i < points.Length - 1; i++)
  113.             {
  114.                 line.Draw(points[i], points[i + 1], drawer);
  115.             }
  116.             line.Draw(points[points.Length - 1], points[0], drawer);
  117.         }
  118.     }
  119.  
  120.     public class PyramidCreator
  121.     {
  122.        
  123.  
  124.         public IDrawMesh[] CreatePyramid(int cubsCount, int height, int canvWidth, Color color)
  125.         {
  126.            
  127.             int posY = 0;
  128.             int cubHeight = height / cubsCount;
  129.             int summ = (cubsCount*(cubsCount + 1)) / 2;
  130.             int canvCenter = canvWidth / 2;
  131.  
  132.             IDrawMesh[] meshes = new Square[cubsCount];
  133.  
  134.             for (int i = 1; i <= cubsCount; i++)
  135.             {
  136.                 int cubWidth = (height / summ) * i;
  137.                 meshes[i - 1] = new Square
  138.                 (
  139.                     new Point((canvCenter - (cubWidth/2)), posY, color),
  140.                     new Point((canvCenter + (cubWidth / 2)), posY, color),
  141.                     new Point((canvCenter - (cubWidth / 2)), (posY+ cubWidth), color),
  142.                     new Point((canvCenter + (cubWidth / 2)), (posY + cubWidth), color)
  143.                 );
  144.                 posY += cubWidth * i;
  145.             }
  146.  
  147.             return meshes;
  148.         }
  149.     }
  150.  
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement