1
0
Files
OAiP-Presnyakov_Ilya-Labora…/DrawFigureLibrary/Figures/PolygonFigure.cs
2026-02-26 12:12:38 +03:00

93 lines
2.8 KiB
C#

using System;
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();
}
protected 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();
}
}
public override void ResizeTo(int newW, int newH)
{
if (newW <= 0 || newH <= 0) throw new ArgumentException("Размеры должны быть > 0");
double oldW = w;
double oldH = h;
if (oldW <= 0 || oldH <= 0)
{
RecalcBounds();
oldW = w;
oldH = h;
if (oldW <= 0 || oldH <= 0) throw new ArgumentException("Нельзя изменить размер: вырожденный многоугольник.");
}
double sx = newW / oldW;
double sy = newH / oldH;
double ox = x;
double oy = y;
for (int i = 0; i < Points.Length; i++)
{
double px = Points[i].X;
double py = Points[i].Y;
double nx = ox + (px - ox) * sx;
double ny = oy + (py - oy) * sy;
Points[i] = new Point(nx, ny);
}
RecalcBounds();
}
}
}