78 lines
No EOL
2.6 KiB
C#
78 lines
No EOL
2.6 KiB
C#
using System.Diagnostics;
|
|
using System.Numerics;
|
|
using Avalonia;
|
|
using Avalonia.Media;
|
|
using Shapes.Shapes;
|
|
|
|
namespace Shapes;
|
|
|
|
/// <summary>
|
|
/// A random shape generator
|
|
/// </summary>
|
|
public sealed class ShapeGenerator
|
|
{
|
|
private const int MinShapeIdx = 1;
|
|
private const int MaxShapeIdx = 4;
|
|
private readonly double _maxX;
|
|
private readonly double _maxY;
|
|
private readonly Random _random;
|
|
private int _nextShape;
|
|
|
|
private static readonly IBrush[] colors =
|
|
{
|
|
Brushes.Blue,
|
|
Brushes.Cyan,
|
|
Brushes.DarkGreen,
|
|
Brushes.Firebrick,
|
|
Brushes.Lime,
|
|
Brushes.Orange,
|
|
Brushes.Plum,
|
|
Brushes.Yellow
|
|
};
|
|
|
|
/// <summary>
|
|
/// Creates a new generator which will create random shapes within the canvas area
|
|
/// </summary>
|
|
/// <param name="maxX">The max x-axis value where shapes may be drawn</param>
|
|
/// <param name="maxY">The max y-axis value where shapes may be drawn</param>
|
|
public ShapeGenerator(double maxX, double maxY)
|
|
{
|
|
_maxX = maxX;
|
|
_maxY = maxY;
|
|
_nextShape = MinShapeIdx;
|
|
_random = Random.Shared;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new random shape at the specified location.
|
|
/// A circle, rectangle, square or triangle will be created.
|
|
/// The size of the shape is randomly chosen, but will not exceed the max. drawing
|
|
/// boundaries - except for at most 5 pixels to allow rendering in any case.
|
|
/// Shapes may overlap each other.
|
|
/// </summary>
|
|
/// <param name="atLocation">Location of the center point of the new shape</param>
|
|
/// <returns>The newly created shape</returns>
|
|
public Shape CreateNewShape(Point atLocation)
|
|
{
|
|
int shape = _nextShape++;
|
|
if (_nextShape > MaxShapeIdx)
|
|
{
|
|
_nextShape = MinShapeIdx;
|
|
}
|
|
|
|
var randomSize = new Point(_random.NextDouble() * _maxX, _random.NextDouble() * _maxY);
|
|
var randomThickness = _random.NextDouble() * 3 + 1;
|
|
var randomColor = colors[_random.Next(colors.Length)];
|
|
|
|
Shape generatedShape = shape switch
|
|
{
|
|
1 => new Circle(atLocation, randomSize, randomThickness, randomColor, randomColor),
|
|
2 => new Rectangle(atLocation, randomSize, randomThickness, randomColor, randomColor),
|
|
3 => new Square(atLocation, randomSize, randomThickness, randomColor, randomColor),
|
|
4 => new Triangle(atLocation, randomSize, randomThickness, randomColor, randomColor),
|
|
_ => new Square(atLocation, randomSize, randomThickness, randomColor, randomColor)
|
|
};
|
|
|
|
return generatedShape;
|
|
}
|
|
} |