Skip to content

Commit

Permalink
Add type StarViewNode (Closes #375).
Browse files Browse the repository at this point in the history
  • Loading branch information
SunnieShine committed Dec 1, 2022
1 parent 4413764 commit ce2b15c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/Sudoku.Gdip/Gdip/GridImageGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ static void cut(scoped ref PointF pt1, scoped ref PointF pt2, float cw, float ch
{
switch (figureNode)
{
case (TriangleViewNode or DiamondViewNode) and (var cell) { Identifier: var identifier }:
case (TriangleViewNode or DiamondViewNode or StarViewNode) and (var cell) { Identifier: var identifier }:
{
using var brush = new SolidBrush(GetColor(identifier));
var (x, y) = calc.GetMousePointInCenter(cell);
Expand All @@ -926,6 +926,7 @@ figureNode switch
{
TriangleViewNode => triangle,
DiamondViewNode => diamond,
StarViewNode => star,
_ => default(PathCreator?)!
}
)(x, y);
Expand Down Expand Up @@ -982,6 +983,57 @@ GraphicsPath diamond(float x, float y)

return path;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
GraphicsPath star(float x, float y)
{
var angles1 = getAngles(-PI / 2);
var angles2 = getAngles(-PI / 2 + PI / 5);
var points1 = getPoints(x, y, cw / 2 - padding, angles1);
var points2 = getPoints(x, y, (ch / 2 - padding) / 2, angles2);
var points = new PointF[points1.Length + points2.Length];
for (var (i, j) = (0, 0); i < points.Length; i += 2, j++)
{
points[i] = points1[j];
points[i + 1] = points2[j];
}

var path = new GraphicsPath();
for (var i = 0; i < points.Length - 1; i++)
{
path.AddLine(points[i], points[i + 1]);
}
path.AddLine(points[^1], points[0]);

return path;


static float[] getAngles(float startAngle)
{
var result = new[] { startAngle, default, default, default, default };
for (var i = 1; i < 5; i++)
{
result[i] = result[i - 1] + 2 * PI / 5;
}

return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static PointF getPoint(float x, float y, float length, float angle)
=> new(x + length * Cos(angle), y + length * Sin(angle));

static PointF[] getPoints(float x, float y, float length, params float[] angles)
{
var result = new PointF[angles.Length];
for (var i = 0; i < result.Length; i++)
{
result[i] = getPoint(x, y, length, angles[i]);
}

return result;
}
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/Sudoku.Presentation/Presentation/Nodes/StarViewNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Sudoku.Presentation.Nodes;

/// <summary>
/// Defines a star view node.
/// </summary>
public sealed class StarViewNode : FigureViewNode
{
/// <summary>
/// Initializes a <see cref="StarViewNode"/> instance via the specified values.
/// </summary>
/// <param name="identifier">The identifier.</param>
/// <param name="cell">The cell.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public StarViewNode(Identifier identifier, int cell) : base(identifier, cell)
{
}


/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override StarViewNode Clone() => new(Identifier, Cell);
}
15 changes: 15 additions & 0 deletions src/docxml/Sudoku.Presentation.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ce2b15c

Please sign in to comment.