From 1e62ef56883c04181db9f6801433b2d62b42ade8 Mon Sep 17 00:00:00 2001 From: MarcUs7i Date: Fri, 14 Mar 2025 14:25:20 +0100 Subject: [PATCH] Minor changes to Rectangle.cs and Square.cs --- Shapes/Shapes/Rectangle.cs | 17 +++++++++++++---- Shapes/Shapes/Square.cs | 9 +++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Shapes/Shapes/Rectangle.cs b/Shapes/Shapes/Rectangle.cs index 4b475bc..8bf619e 100644 --- a/Shapes/Shapes/Rectangle.cs +++ b/Shapes/Shapes/Rectangle.cs @@ -1,4 +1,5 @@ using System.Numerics; +using SimpleDrawing; namespace Shapes.Shapes; @@ -27,13 +28,21 @@ public sealed class Rectangle : Shape scale = Scale; } - // TODO - return false; + return LeoCanvas.DrawRectangle(new(position.X - scale.X/2, position.Y - scale.Y/2), new(scale.X, scale.Y)); } public override bool PointInShape(Vector2 mousePoint) { - // TODO - return false; + float halfWidth = Scale.X / 2; + float halfHeight = Scale.Y / 2; + + 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/Square.cs b/Shapes/Shapes/Square.cs index a2f4b57..c775fb4 100644 --- a/Shapes/Shapes/Square.cs +++ b/Shapes/Shapes/Square.cs @@ -18,27 +18,24 @@ public sealed class Square : Shape public override bool DrawSelf(Vector2 position = default, Vector2 scale = default) { - if (position == default(Vector2)) + if (position == default) { position = Position; } - if (scale == default(Vector2)) + if (scale == default) { scale = Scale; } - // TODO - return LeoCanvas.DrawRectangle(new(position.X, position.Y), new(scale.X, scale.Y)); + return LeoCanvas.DrawRectangle(new(position.X - scale.X/2, position.Y - scale.Y/2), 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;