From 6f961f908b36cdb86931f32660dfaef848a4b778 Mon Sep 17 00:00:00 2001 From: Lukasz Baldyga Date: Wed, 4 Nov 2020 18:13:02 +0000 Subject: [PATCH] Made Rectangle work properly --- .../Verbs/DrawingVerbs/Rectangle.cs | 72 ++++++++----------- .../Verbs/VerbFactory.cs | 10 +-- 2 files changed, 32 insertions(+), 50 deletions(-) diff --git a/Advanced Software Engineering/Advanced Software Engineering/Verbs/DrawingVerbs/Rectangle.cs b/Advanced Software Engineering/Advanced Software Engineering/Verbs/DrawingVerbs/Rectangle.cs index dbbca0f..cb5da1e 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/Verbs/DrawingVerbs/Rectangle.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/Verbs/DrawingVerbs/Rectangle.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Forms.VisualStyles; namespace Advanced_Software_Engineering.Verbs.DrawingVerbs { /// @@ -16,71 +17,58 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs { Verb verb; Drawer drawer; - List points; - bool correctForOrigin; + PointF[] points; + + double width; + double height; /// - /// Create a rectangle instance + /// Create a rectangle instance. Makes sure that /// /// drawer /// rectangle width /// rectangle height - /// weather to center the rectangle on the pen - public Rectangle(Drawer drawer, double width, double height, bool center = true) { - this.drawer = drawer; - correctForOrigin = true; - - PointF[] points = new PointF[] - { - new PointF((float) -width, (float) -height), - new PointF((float) width, (float) -height), - new PointF((float) width, (float) height), - new PointF((float) -width, (float) height) - }; - - this.points = new List(); - foreach (PointF pointF in points) this.points.Add(new Point((int)Math.Round(pointF.X), (int)Math.Round(pointF.Y))); - } - - /// - /// Create a rectangle instance - /// - /// - /// Unlike the above, this instance creates points based on widths and heights of the two points - /// - /// drawer - /// point spawn - /// point spawn - public Rectangle(Drawer drawer, Point point1, Point point2) { + public Rectangle(Drawer drawer, double width, double height) { this.drawer = drawer; - PointF[] points = new PointF[] + float root2 = (float) Math.Sqrt(2) / 2; + + points = new PointF[] { - new PointF((float) point1.X, (float) point1.Y), - new PointF((float) point1.X, (float) point2.Y), - new PointF((float) point2.X, (float) point2.Y), - new PointF((float) point2.X, (float) point1.Y) + new PointF((float) -width * root2, (float) -height * root2), + new PointF((float) width * root2, (float) -height * root2), + new PointF((float) width * root2, (float) height * root2), + new PointF((float) -width * root2, (float) height * root2) }; - this.points = new List(); - foreach (PointF pointF in points) this.points.Add(new Point((int)Math.Round(pointF.X), (int)Math.Round(pointF.Y))); - - correctForOrigin = false; - + this.width = width; + this.height = height; } + /// /// Gets the description from the rectangle /// /// Describes the rectangle public string GetDescription() { - return ""; + return "Draws a rectangle with the width of" + width + " and the height of " + height + " with the pen in the center"; } /// /// Draws the rectangle /// - public void ExecuteVerb() => drawer.DrawLines(points.ToArray()); + public void ExecuteVerb() { + //adjust for current pen position + Point position = drawer.GetPenPosition(); + + List morePoints = new List(); + + foreach (PointF point in points) { + morePoints.Add(new Point((int)(point.X + position.X), (int)(point.Y + position.Y))); + } + + drawer.DrawLines(morePoints.ToArray()); + } } } diff --git a/Advanced Software Engineering/Advanced Software Engineering/Verbs/VerbFactory.cs b/Advanced Software Engineering/Advanced Software Engineering/Verbs/VerbFactory.cs index 1f9c9e4..c0fe2f2 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/Verbs/VerbFactory.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/Verbs/VerbFactory.cs @@ -1,7 +1,6 @@ using System; using System.CodeDom; using System.Collections.Generic; -using System.Drawing; using System.Linq; using System.Security.Cryptography; using System.Windows.Forms; @@ -41,7 +40,7 @@ namespace Advanced_Software_Engineering { Value.ConvertToInt(parameters[0]), Value.ConvertToInt(parameters[1])); - } else throw new Exception("Command has an incorrect number of parameters"); + } else throw new Exception(command + " has an incorrect number of parameters"); case "drawto": case "line": @@ -95,7 +94,7 @@ namespace Advanced_Software_Engineering { case "rectangle": //Check parameters if (parameters.Length == 2) { - return new Advanced_Software_Engineering.Verbs.DrawingVerbs.Rectangle(drawer, + return new Rectangle(drawer, Value.ConvertToDouble(parameters[0]), Value.ConvertToDouble(parameters[1]) ); @@ -106,11 +105,6 @@ namespace Advanced_Software_Engineering { Value.ConvertToPoint(parameters[4], parameters[5]), Value.ConvertToPoint(parameters[6], parameters[7]) ); - } else if (parameters.Length == 4) { - return new Advanced_Software_Engineering.Verbs.DrawingVerbs.Rectangle(drawer, - Value.ConvertToPoint(parameters[0], parameters[1]), - Value.ConvertToPoint(parameters[2], parameters[3]) - ); } else throw new Exception("Command has an incorrect number of parameters"); case "circle": //Check parameters