using System.Diagnostics; using System.Numerics; using Avalonia; using Avalonia.Media; using Shapes.Shapes; namespace Shapes; /// /// A random shape generator /// 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 }; /// /// Creates a new generator which will create random shapes within the canvas area /// /// The max x-axis value where shapes may be drawn /// The max y-axis value where shapes may be drawn public ShapeGenerator(double maxX, double maxY) { _maxX = maxX; _maxY = maxY; _nextShape = MinShapeIdx; _random = Random.Shared; } /// /// 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. /// /// Location of the center point of the new shape /// The newly created shape 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; } }