From 4fc6f62b57b85a4aee5ad990c4d5e1f7b334c166 Mon Sep 17 00:00:00 2001 From: MarcUs7i <96580944+MarcUs7i@users.noreply.github.com> Date: Thu, 13 Mar 2025 22:40:24 +0100 Subject: [PATCH] Implemented starter code --- Shapes/Shapes/Circle.cs | 38 ++++++++++++++++++++++++++--- Shapes/Shapes/Rectangle.cs | 38 ++++++++++++++++++++++++++--- Shapes/Shapes/Shape.cs | 17 +++++++------ Shapes/Shapes/Square.cs | 50 +++++++++++++++++++++++++++++++++++--- Shapes/Shapes/Triangle.cs | 40 +++++++++++++++++++++++++++--- 5 files changed, 164 insertions(+), 19 deletions(-) diff --git a/Shapes/Shapes/Circle.cs b/Shapes/Shapes/Circle.cs index 3872c66..c0989eb 100644 --- a/Shapes/Shapes/Circle.cs +++ b/Shapes/Shapes/Circle.cs @@ -1,7 +1,39 @@ -namespace Shapes.Shapes; +using System.Numerics; + +namespace Shapes.Shapes; public sealed class Circle : Shape { - const int RadiusMin = 5; - const int RadiusMax = 100; + private const int RadiusMin = 5; + private const int RadiusMax = 100; + + public Circle(Vector2 position, Vector2 scale) + : base(position, new Vector2( + Math.Clamp(scale.X, RadiusMin, RadiusMax), + Math.Clamp(scale.Y, RadiusMin, RadiusMax))) + { + // Clamp the scale values directly in the base ctor + } + + public override bool DrawSelf(Vector2 position = default, Vector2 scale = default) + { + if (position == default) + { + position = Position; + } + + if (scale == default) + { + scale = Scale; + } + + // TODO + return false; + } + + public override bool PointInShape(Vector2 mousePoint) + { + // TODO + return false; + } } diff --git a/Shapes/Shapes/Rectangle.cs b/Shapes/Shapes/Rectangle.cs index 92ca65e..4b475bc 100644 --- a/Shapes/Shapes/Rectangle.cs +++ b/Shapes/Shapes/Rectangle.cs @@ -1,7 +1,39 @@ -namespace Shapes.Shapes; +using System.Numerics; + +namespace Shapes.Shapes; public sealed class Rectangle : Shape { - const int SideLengthMin = 10; - const int SideLengthMax = 150; + private const int SideLengthMin = 10; + private const int SideLengthMax = 150; + + public Rectangle(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) + { + position = Position; + } + + if (scale == default) + { + scale = Scale; + } + + // TODO + return false; + } + + public override bool PointInShape(Vector2 mousePoint) + { + // TODO + return false; + } } diff --git a/Shapes/Shapes/Shape.cs b/Shapes/Shapes/Shape.cs index 05b91de..5757fba 100644 --- a/Shapes/Shapes/Shape.cs +++ b/Shapes/Shapes/Shape.cs @@ -1,18 +1,21 @@ -namespace Shapes.Shapes; +using System.Numerics; + +namespace Shapes.Shapes; public abstract class Shape { private static int _nextId = 1; public int Id { get; } - public int Height { get; set; } - public int Width { get; set; } + protected Vector2 Scale; + protected Vector2 Position; - protected Shape() + protected Shape(Vector2 position, Vector2 scale) { Id = _nextId++; + Position = position; + Scale = scale; } - public virtual bool DrawSelf() => true; - public virtual bool Draw(int height, int width) => true; - public virtual bool Delete() => true; + public abstract bool DrawSelf(Vector2 position = default, Vector2 scale = default); + public abstract bool PointInShape(Vector2 mousePoint); } diff --git a/Shapes/Shapes/Square.cs b/Shapes/Shapes/Square.cs index 4b2bfec..a2f4b57 100644 --- a/Shapes/Shapes/Square.cs +++ b/Shapes/Shapes/Square.cs @@ -1,7 +1,51 @@ -namespace Shapes.Shapes; +using System.Numerics; +using SimpleDrawing; + +namespace Shapes.Shapes; public sealed class Square : Shape { - const int SideLengthMin = 10; - const int SideLengthMax = 120; + 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; + } } diff --git a/Shapes/Shapes/Triangle.cs b/Shapes/Shapes/Triangle.cs index ea18a01..54439f0 100644 --- a/Shapes/Shapes/Triangle.cs +++ b/Shapes/Shapes/Triangle.cs @@ -1,7 +1,41 @@ -namespace Shapes.Shapes; +using System.Numerics; +using Avalonia; +using SimpleDrawing; + +namespace Shapes.Shapes; public sealed class Triangle : Shape { - const int SideLengthMin = 10; - const int SideLengthMax = 150; + private const int SideLengthMin = 10; + private const int SideLengthMax = 150; + + public Triangle(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) + { + position = Position; + } + + if (scale == default) + { + scale = Scale; + } + + // TODO + return false; + } + + public override bool PointInShape(Vector2 mousePoint) + { + // TODO + return false; + } }