Create PolygonFigure.cs
This commit is contained in:
59
DrawFigureLibrary/Figures/PolygonFigure.cs
Normal file
59
DrawFigureLibrary/Figures/PolygonFigure.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user