From 976b0b564e78f1911b0eb9cb08d4a3968cabdf6d Mon Sep 17 00:00:00 2001 From: Debug_pro Date: Thu, 26 Feb 2026 01:07:50 +0300 Subject: [PATCH] Create PolygonFigure.cs --- DrawFigureLibrary/Figures/PolygonFigure.cs | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 DrawFigureLibrary/Figures/PolygonFigure.cs diff --git a/DrawFigureLibrary/Figures/PolygonFigure.cs b/DrawFigureLibrary/Figures/PolygonFigure.cs new file mode 100644 index 0000000..5699139 --- /dev/null +++ b/DrawFigureLibrary/Figures/PolygonFigure.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; + +namespace DrawFigureLibrary.Figures +{ + public class PolygonFigure : Figure + { + public Point[] Points { get; private set; } + + public PolygonFigure(string name, Point[] points) : base(name, 0, 0, 0, 0) + { + if (points == null || points.Length < 3) throw new ArgumentException("Многоугольник должен иметь минимум 3 точки."); + + Points = points; + RecalcBounds(); + } + + private void RecalcBounds() + { + double minX = Points.Min(p => p.X); + double minY = Points.Min(p => p.Y); + double maxX = Points.Max(p => p.X); + double maxY = Points.Max(p => p.Y); + + x = (int)minX; + y = (int)minY; + w = (int)(maxX - minX); + h = (int)(maxY - minY); + } + + public override void Draw(DrawingContext drawingContext) + { + StreamGeometry g = new StreamGeometry(); + using (var ctx = g.Open()) + { + ctx.BeginFigure(Points[0], isFilled: true, isClosed: true); + ctx.PolyLineTo(Points.Skip(1).ToArray(), isStroked: true, isSmoothJoin: false); + } + drawingContext.DrawGeometry(null, Init.pen, g); + } + + public override void MoveBy(int deltaX, int deltaY) + { + if (Init.DrawingImage == null) return; + + if (Check(this, deltaX, deltaY, Init.DrawingImage)) + { + for (int i = 0; i < Points.Length; i++) Points[i] = new Point( Points[i].X + deltaX, Points[i].Y + deltaY ); + + RecalcBounds(); + } + } + } +}