-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
RegularPolygon.cs
82 lines (70 loc) · 3.45 KB
/
RegularPolygon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
namespace SixLabors.ImageSharp.Drawing;
/// <summary>
/// A shape made up of a single path made up of one of more <see cref="ILineSegment"/>s
/// </summary>
public class RegularPolygon : Polygon
{
/// <summary>
/// Initializes a new instance of the <see cref="RegularPolygon" /> class.
/// </summary>
/// <param name="location">The location the center of the polygon will be placed.</param>
/// <param name="vertices">The number of vertices the <see cref="RegularPolygon"/> should have.</param>
/// <param name="radius">The radius of the circle that would touch all vertices.</param>
/// <param name="angle">The angle of rotation in Radians</param>
public RegularPolygon(PointF location, int vertices, float radius, float angle)
: base(CreateSegment(location, radius, vertices, angle))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RegularPolygon" /> class.
/// </summary>
/// <param name="location">The location the center of the polygon will be placed.</param>
/// <param name="vertices">The number of vertices the <see cref="RegularPolygon"/> should have.</param>
/// <param name="radius">The radius of the circle that would touch all vertices.</param>
public RegularPolygon(PointF location, int vertices, float radius)
: this(location, vertices, radius, 0)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RegularPolygon" /> class.
/// </summary>
/// <param name="x">The x-coordinate of the center of the polygon.</param>
/// <param name="y">The y-coordinate of the center of the polygon.</param>
/// <param name="vertices">The number of vertices the <see cref="RegularPolygon" /> should have.</param>
/// <param name="radius">The radius of the circle that would touch all vertices.</param>
/// <param name="angle">The angle of rotation in Radians</param>
public RegularPolygon(float x, float y, int vertices, float radius, float angle)
: this(new PointF(x, y), vertices, radius, angle)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RegularPolygon" /> class.
/// </summary>
/// <param name="x">The x-coordinate of the center of the polygon.</param>
/// <param name="y">The y-coordinate of the center of the polygon.</param>
/// <param name="vertices">The number of vertices the <see cref="RegularPolygon"/> should have.</param>
/// <param name="radius">The radius of the circle that would touch all vertices.</param>
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));
var distanceVector = new PointF(0, radius);
float anglePerSegments = (float)(2 * Math.PI / vertices);
float current = angle;
var points = new PointF[vertices];
for (int i = 0; i < vertices; i++)
{
var rotated = PointF.Transform(distanceVector, Matrix3x2.CreateRotation(current));
points[i] = rotated + location;
current += anglePerSegments;
}
return new LinearLineSegment(points);
}
}