ex-inh-03-shapes/Shapes/Shapes/Square.cs
2025-03-13 22:40:24 +01:00

51 lines
1.5 KiB
C#

using System.Numerics;
using SimpleDrawing;
namespace Shapes.Shapes;
public sealed class Square : Shape
{
private const int SideLengthMin = 10;
private const int SideLengthMax = 120;
public Square(Vector2 position, Vector2 scale)
: base(position, new Vector2(
Math.Clamp(scale.X, SideLengthMin, SideLengthMax),
Math.Clamp(scale.Y, SideLengthMin, SideLengthMax)))
{
// Clamp the scale values directly in the base ctor
}
public override bool DrawSelf(Vector2 position = default, Vector2 scale = default)
{
if (position == default(Vector2))
{
position = Position;
}
if (scale == default(Vector2))
{
scale = Scale;
}
// TODO
return LeoCanvas.DrawRectangle(new(position.X, position.Y), new(scale.X, scale.Y));
}
public override bool PointInShape(Vector2 mousePoint)
{
// Calculate half height & width
float halfWidth = Scale.X / 2;
float halfHeight = Scale.Y / 2;
// Calculate
float left = Position.X - halfWidth;
float right = Position.X + halfWidth;
float top = Position.Y - halfHeight;
float bottom = Position.Y + halfHeight;
// If point is inside shape
return mousePoint.X >= left && mousePoint.X <= right &&
mousePoint.Y >= top && mousePoint.Y <= bottom;
}
}