Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- namespace PolygonDraft
- {
- //TODO: Make state diagrams
- [Serializable]
- public abstract class Vertex // (constructor)
- {
- public Vertex(in int x, in int y, in Color fillColor, in Color hullColor, in int radius)
- {
- Dx = 0;
- Dy = 0;
- IsDragged = false;
- X = x;
- Y = y;
- FillColor = fillColor;
- HullColor = hullColor;
- Radius = radius;
- }
- public bool IsDragged { get; set; }
- public Color FillColor { get; set; }
- public Color HullColor { get; set; }
- public int X { get; set; }
- public int Y { get; set; }
- public int Dx { get; set; } // = 0;
- public int Dy { get; set; } // = 0;
- public int Radius { get; set; }
- public abstract bool Check(in int x, in int y);
- public abstract void Draw(Graphics graphics);
- public static implicit operator Point(Vertex vertex)
- => new Point(vertex.X, vertex.Y);
- public override bool Equals(object obj)
- {
- Vertex vertex = obj as Vertex;
- bool colorMatch = (FillColor == vertex.FillColor && HullColor == vertex.HullColor);
- bool radiusMatch = (Radius == vertex.Radius);
- bool posMatch = (X == vertex.X && Y == vertex.Y);
- return colorMatch && radiusMatch && posMatch;
- }
- }
- [Serializable]
- public sealed class Circle : Vertex
- {
- public Circle(in int x,
- in int y,
- in Color fillColor,
- in Color hullColor,
- in int radius)
- : base(x, y, fillColor, hullColor, radius) { }
- public override bool Check(in int x, in int y)
- {
- return Radius >= Math.Sqrt(Math.Pow(x - X, 2) + Math.Pow(y - Y, 2));
- }
- public override void Draw(Graphics graphics)
- {
- graphics.FillEllipse(new SolidBrush(FillColor), new Rectangle(X - Radius, Y - Radius, 2 * Radius, 2 * Radius));
- graphics.DrawEllipse(new Pen(HullColor, 3), new Rectangle(X - Radius, Y - Radius, 2 * Radius, 2 * Radius));
- }
- }
- [Serializable]
- public sealed class Square : Vertex
- {
- private RectangleF rectangle;
- public Square(in int x,
- in int y,
- in Color fillColor,
- in Color hullColor,
- in int radius)
- : base(x, y, fillColor, hullColor, radius)
- {
- rectangle = new RectangleF(Convert.ToInt32(X - Radius / Math.Sqrt(2)),
- Convert.ToInt32(Y - Radius / Math.Sqrt(2)),
- 1.4f * Radius, 1.4f * Radius);
- }
- public override bool Check(in int x, in int y)
- {
- return rectangle.Contains(x, y);
- }
- public override void Draw(Graphics graphics)
- {
- SolidBrush brush = new SolidBrush(FillColor);
- rectangle = new RectangleF(Convert.ToInt32(X - Radius / Math.Sqrt(2)),
- Convert.ToInt32(Y - Radius / Math.Sqrt(2)),
- 1.4f * Radius, 1.4f * Radius);
- graphics.FillRectangle(new SolidBrush(FillColor), rectangle);
- graphics.DrawRectangle(new Pen(HullColor, 3), Convert.ToInt32(X - Radius / Math.Sqrt(2)),
- Convert.ToInt32(Y - Radius / Math.Sqrt(2)),
- 1.4f * Radius, 1.4f * Radius);
- }
- }
- [Serializable]
- public sealed class Triangle : Vertex
- {
- public Triangle(in int x,
- in int y,
- in Color fillColor,
- in Color hullColor,
- in int radius)
- : base(x, y, fillColor, hullColor, radius) { }
- public override bool Check(in int x, in int y)
- {
- Point[] point_array = { new Point(X - Radius, Y + (Radius / 2)),
- new Point(X, Y - Radius),
- new Point(X + Radius, Y + (Radius / 2)) };
- bool b1 = (point_array[0].X - x)
- * (point_array[1].Y - point_array[0].Y)
- - (point_array[1].X - point_array[0].X)
- * (point_array[0].Y - y) >= 0;
- bool b2 = (point_array[1].X - x)
- * (point_array[2].Y - point_array[1].Y)
- - (point_array[2].X - point_array[1].X)
- * (point_array[1].Y - y) >= 0;
- bool b3 = (point_array[2].X - x)
- * (point_array[0].Y - point_array[2].Y)
- - (point_array[0].X - point_array[2].X)
- * (point_array[2].Y - y) >= 0;
- if (b1 && b2 && b3 || !b1 && !b2 && !b3)
- return true;
- return false;
- }
- public override void Draw(Graphics graphics)
- {
- Point[] point_array = { new Point(X - Radius, Y + (Radius / 2)), new Point(X, Y - Radius), new Point(X + Radius, Y + (Radius / 2)) };
- graphics.FillPolygon(new SolidBrush(FillColor), point_array);
- graphics.DrawPolygon(new Pen(HullColor, 3), point_array);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement