// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System;
using System.Numerics;
namespace SixLabors.ImageSharp.Drawing {
///
/// A shape made up of a single path made up of one of more s
///
public class RegularPolygon : Polygon
{
///
/// Initializes a new instance of the class.
///
/// The location the center of the polygon will be placed.
/// The number of vertices the should have.
/// The radius of the circle that would touch all vertices.
/// The angle of rotation in degrees.
public RegularPolygon(PointF location, int vertices, float radius, float angle)
: base(CreateSegment(location, radius, vertices, angle))
{
}
///
/// Initializes a new instance of the class.
///
/// The location the center of the polygon will be placed.
/// The number of vertices the should have.
/// The radius of the circle that would touch all vertices.
public RegularPolygon(PointF location, int vertices, float radius)
: this(location, vertices, radius, 0)
{
}
///
/// Initializes a new instance of the class.
///
/// The x-coordinate of the center of the polygon.
/// The y-coordinate of the center of the polygon.
/// The number of vertices the should have.
/// The radius of the circle that would touch all vertices.
/// The angle of rotation in degrees.
public RegularPolygon(float x, float y, int vertices, float radius, float angle)
: this(new PointF(x, y), vertices, radius, angle)
{
}
///
/// Initializes a new instance of the class.
///
/// The x-coordinate of the center of the polygon.
/// The y-coordinate of the center of the polygon.
/// The number of vertices the should have.
/// The radius of the circle that would touch all vertices.
public RegularPolygon(float x, float y, int vertices, float radius)
: this(new PointF(x, y), vertices, radius)
{
}
private static LinearLineSegment CreateSegment(PointF location, float radius, int vertices, float angle)
{
Guard.MustBeGreaterThan(vertices, 2, nameof(vertices));
Guard.MustBeGreaterThan(radius, 0, nameof(radius));
PointF distanceVector = new(0, radius);
float anglePerSegments = (float)(2 * Math.PI / vertices);
float current = GeometryUtilities.DegreeToRadian(angle);
PointF[] points = new PointF[vertices];
for (int i = 0; i < vertices; i++)
{
PointF rotated = PointF.Transform(distanceVector, Matrix4x4.CreateRotationZ(current));
points[i] = rotated + location;
current += anglePerSegments;
}
return new LinearLineSegment(points);
}
}
}