Advertisement
_takumi

Vertex.cs

Dec 10th, 2019
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.27 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3.  
  4. namespace PolygonDraft
  5. {
  6.     //TODO: Make state diagrams
  7.     [Serializable]
  8.     public abstract class Vertex // (constructor)
  9.     {
  10.         public Vertex(in int x, in int y, in Color fillColor, in Color hullColor, in int radius)
  11.         {
  12.             Dx = 0;
  13.             Dy = 0;
  14.             IsDragged = false;
  15.             X = x;
  16.             Y = y;
  17.             FillColor = fillColor;
  18.             HullColor = hullColor;
  19.             Radius = radius;
  20.         }
  21.         public bool IsDragged { get; set; }
  22.         public Color FillColor { get; set; }
  23.         public Color HullColor { get; set; }
  24.         public int X { get; set; }
  25.         public int Y { get; set; }
  26.         public int Dx { get; set; } // = 0;
  27.         public int Dy { get; set; } // = 0;
  28.         public int Radius { get; set; }
  29.  
  30.         public abstract bool Check(in int x, in int y);
  31.  
  32.         public abstract void Draw(Graphics graphics);
  33.  
  34.         public static implicit operator Point(Vertex vertex)
  35.             => new Point(vertex.X, vertex.Y);
  36.  
  37.         public override bool Equals(object obj)
  38.         {
  39.             Vertex vertex = obj as Vertex;
  40.             bool colorMatch = (FillColor == vertex.FillColor && HullColor == vertex.HullColor);
  41.             bool radiusMatch = (Radius == vertex.Radius);
  42.             bool posMatch = (X == vertex.X && Y == vertex.Y);
  43.             return colorMatch && radiusMatch && posMatch;
  44.         }
  45.     }
  46.  
  47.     [Serializable]
  48.     public sealed class Circle : Vertex
  49.     {
  50.         public Circle(in int x,
  51.                       in int y,
  52.                       in Color fillColor,
  53.                       in Color hullColor,
  54.                       in int radius)
  55.             : base(x, y, fillColor, hullColor, radius) { }
  56.         public override bool Check(in int x, in int y)
  57.         {
  58.             return Radius >= Math.Sqrt(Math.Pow(x - X, 2) + Math.Pow(y - Y, 2));
  59.         }
  60.         public override void Draw(Graphics graphics)
  61.         {
  62.             graphics.FillEllipse(new SolidBrush(FillColor), new Rectangle(X - Radius, Y - Radius, 2 * Radius, 2 * Radius));
  63.             graphics.DrawEllipse(new Pen(HullColor, 3), new Rectangle(X - Radius, Y - Radius, 2 * Radius, 2 * Radius));
  64.         }
  65.     }
  66.  
  67.     [Serializable]
  68.     public sealed class Square : Vertex
  69.     {
  70.         private RectangleF rectangle;
  71.         public Square(in int x,
  72.                       in int y,
  73.                       in Color fillColor,
  74.                       in Color hullColor,
  75.                       in int radius)
  76.             : base(x, y, fillColor, hullColor, radius)
  77.         {
  78.             rectangle = new RectangleF(Convert.ToInt32(X - Radius / Math.Sqrt(2)),
  79.                             Convert.ToInt32(Y - Radius / Math.Sqrt(2)),
  80.                             1.4f * Radius, 1.4f * Radius);
  81.         }
  82.         public override bool Check(in int x, in int y)
  83.         {
  84.             return rectangle.Contains(x, y);
  85.         }
  86.         public override void Draw(Graphics graphics)
  87.         {
  88.             SolidBrush brush = new SolidBrush(FillColor);
  89.             rectangle = new RectangleF(Convert.ToInt32(X - Radius / Math.Sqrt(2)),
  90.                 Convert.ToInt32(Y - Radius / Math.Sqrt(2)),
  91.                 1.4f * Radius, 1.4f * Radius);
  92.             graphics.FillRectangle(new SolidBrush(FillColor), rectangle);
  93.             graphics.DrawRectangle(new Pen(HullColor, 3), Convert.ToInt32(X - Radius / Math.Sqrt(2)),
  94.                 Convert.ToInt32(Y - Radius / Math.Sqrt(2)),
  95.                 1.4f * Radius, 1.4f * Radius);
  96.         }
  97.     }
  98.  
  99.     [Serializable]
  100.     public sealed class Triangle : Vertex
  101.     {
  102.         public Triangle(in int x,
  103.                         in int y,
  104.                         in Color fillColor,
  105.                         in Color hullColor,
  106.                         in int radius)
  107.             : base(x, y, fillColor, hullColor, radius) { }
  108.         public override bool Check(in int x, in int y)
  109.         {
  110.             Point[] point_array = { new Point(X - Radius, Y + (Radius / 2)),
  111.                 new Point(X, Y - Radius),
  112.                 new Point(X + Radius, Y + (Radius / 2)) };
  113.             bool b1 = (point_array[0].X - x)
  114.                       * (point_array[1].Y - point_array[0].Y)
  115.                       - (point_array[1].X - point_array[0].X)
  116.                       * (point_array[0].Y - y) >= 0;
  117.             bool b2 = (point_array[1].X - x)
  118.                       * (point_array[2].Y - point_array[1].Y)
  119.                       - (point_array[2].X - point_array[1].X)
  120.                       * (point_array[1].Y - y) >= 0;
  121.             bool b3 = (point_array[2].X - x)
  122.                       * (point_array[0].Y - point_array[2].Y)
  123.                       - (point_array[0].X - point_array[2].X)
  124.                       * (point_array[2].Y - y) >= 0;
  125.             if (b1 && b2 && b3 || !b1 && !b2 && !b3)
  126.                 return true;
  127.             return false;
  128.         }
  129.         public override void Draw(Graphics graphics)
  130.         {
  131.             Point[] point_array = { new Point(X - Radius, Y + (Radius / 2)), new Point(X, Y - Radius), new Point(X + Radius, Y + (Radius / 2)) };
  132.             graphics.FillPolygon(new SolidBrush(FillColor), point_array);
  133.             graphics.DrawPolygon(new Pen(HullColor, 3), point_array);
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement