From 93b22ef2ecbcbe39de58c8c9625fcf9259bc819b Mon Sep 17 00:00:00 2001 From: bbod Date: Fri, 25 Oct 2019 03:16:17 -0600 Subject: [PATCH] first unit test --- .idea/.idea.Shapes/.idea/contentModel.xml | 6 +- Shapes/Circle.cs | 46 ++++++------- Shapes/CompositeShape.cs | 35 ++++------ Shapes/FileIO.cs | 9 +-- Shapes/GeometricShape.cs | 32 ++++----- Shapes/Image.cs | 9 ++- Shapes/ImageManager.cs | 15 ++--- Shapes/Line.cs | 13 ++-- Shapes/Point.cs | 25 ++++--- Shapes/Rectangle.cs | 63 ++++++++--------- Shapes/Shape.cs | 21 +++--- Shapes/ShapeException.cs | 6 +- Shapes/ShapeFactory.cs | 3 +- Shapes/Triangle.cs | 78 ++++++++++------------ Shapes/Validator.cs | 35 ++++------ Temp/Program.cs | 60 +++++++++-------- UnitTests/{UnitTest1.cs => CircleTests.cs} | 2 +- UnitTests/RectangleTests.cs | 34 ++++++++++ UnitTests/ShapeFactoryTests.cs | 18 +++++ UnitTests/TriangleTests.cs | 18 +++++ UnitTests/UnitTests.csproj | 1 + 21 files changed, 275 insertions(+), 254 deletions(-) rename UnitTests/{UnitTest1.cs => CircleTests.cs} (87%) create mode 100644 UnitTests/RectangleTests.cs create mode 100644 UnitTests/ShapeFactoryTests.cs create mode 100644 UnitTests/TriangleTests.cs diff --git a/.idea/.idea.Shapes/.idea/contentModel.xml b/.idea/.idea.Shapes/.idea/contentModel.xml index 0dc8d45..9bacea3 100644 --- a/.idea/.idea.Shapes/.idea/contentModel.xml +++ b/.idea/.idea.Shapes/.idea/contentModel.xml @@ -26,7 +26,6 @@ - @@ -49,6 +48,7 @@ + @@ -57,7 +57,9 @@ - + + + diff --git a/Shapes/Circle.cs b/Shapes/Circle.cs index b3838dd..f82a601 100644 --- a/Shapes/Circle.cs +++ b/Shapes/Circle.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; @@ -11,23 +9,6 @@ namespace Shapes [DataContract] public class Circle : GeometricShape { - internal override void ComputeCenter() - { - //Does nothing to a circle - } - [DataMember] - public override Color Fill { get; set; } - [DataMember] - public override Color Stroke { get; set; } - [DataMember] - public override Point CenterPoint { get; protected set; } - [DataMember] - public double Radius { get; private set; } - [DataMember] - public override double Height { get; internal set; } - [DataMember] - public override double Width { get; internal set; } - /** * Constructor with x-y Location for center * @@ -53,7 +34,8 @@ namespace Shapes * @param radius The radius of the circle -- must be greater or equal to zero. * @throws ShapeException The exception thrown if the x, y, or z are not valid */ - public Circle(Point center, double radius) { + public Circle(Point center, double radius) + { Stroke = Color.Black; Validator.ValidatePositiveDouble(radius, "Invalid radius"); if (center == null) @@ -64,7 +46,23 @@ namespace Shapes Width = radius * 2; } - + [DataMember] public override Color Fill { get; set; } + + [DataMember] public override Color Stroke { get; set; } + + [DataMember] public override Point CenterPoint { get; protected set; } + + [DataMember] public double Radius { get; private set; } + + [DataMember] public override double Height { get; internal set; } + + [DataMember] public override double Width { get; internal set; } + + internal override void ComputeCenter() + { + //Does nothing to a circle + } + /** * Scale the circle @@ -83,19 +81,18 @@ namespace Shapes public override void Save(Stream stream) { var fileWriter = new FileIO(); - fileWriter.SaveShape(stream, this); + fileWriter.SaveShape(stream, this); } public override void Draw(Stream stream) { var tmp = new Bitmap((int) Radius * 4, (int) Radius * 4); - Pen blackPen = new Pen(Color.Bisque, 3); + var blackPen = new Pen(Color.Bisque, 3); // Draw line to screen. using (var graphics = Graphics.FromImage(tmp)) { graphics.DrawEllipse(blackPen, (float) CenterPoint.X, (float) CenterPoint.Y, (float) Radius * 2, (float) Radius * 2); - } tmp.Save(stream, ImageFormat.Jpeg); @@ -108,6 +105,5 @@ namespace Shapes { return Math.PI * Math.Pow(Radius, 2); } - } } \ No newline at end of file diff --git a/Shapes/CompositeShape.cs b/Shapes/CompositeShape.cs index 4cbd936..41a895b 100644 --- a/Shapes/CompositeShape.cs +++ b/Shapes/CompositeShape.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; @@ -9,22 +8,21 @@ namespace Shapes { public class CompositeShape : Shape { - private List thisShapesList = new List(); - + private readonly List thisShapesList = new List(); + public override Color Fill { get; set; } public override Color Stroke { get; set; } public void Add(Shape shape) { - if(shape.CompositeShape) - throw new ShapeException($"This shape has already been added to a composite"); - if(shape == this) - throw new ShapeException($"You cant add a shape to itself"); + if (shape.CompositeShape) + throw new ShapeException("This shape has already been added to a composite"); + if (shape == this) + throw new ShapeException("You cant add a shape to itself"); thisShapesList.Add(shape); shape.CompositeShape = true; } - public override double ComputeArea() { @@ -33,32 +31,27 @@ namespace Shapes public override void Scale(double scaleFactor) { - foreach (var shape in thisShapesList) - { - shape.Scale(scaleFactor); - } + foreach (var shape in thisShapesList) shape.Scale(scaleFactor); } public override void Save(Stream stream) { var fileWriter = new FileIO(); - fileWriter.SaveShape(stream, this); + fileWriter.SaveShape(stream, this); } public override void Draw(Stream stream) { var tmp = new Bitmap(1000, 1000); - Pen blackPen = new Pen(Stroke, 3); + var blackPen = new Pen(Stroke, 3); // Draw line to screen. using (var graphics = Graphics.FromImage(tmp)) { foreach (var shape in thisShapesList) - { - for (int i = 1; i < shape.Points.Count; i++) - graphics.DrawLine(blackPen, (float) shape.Points[i - 1].X, (float) shape.Points[i - 1].Y, (float) shape.Points[i].X, + for (var i = 1; i < shape.Points.Count; i++) + graphics.DrawLine(blackPen, (float) shape.Points[i - 1].X, (float) shape.Points[i - 1].Y, + (float) shape.Points[i].X, (float) shape.Points[i].Y); - } - } tmp.Save(stream, ImageFormat.Jpeg); @@ -67,11 +60,10 @@ namespace Shapes public void RemoveShape(Shape shape) { - if(!thisShapesList.Contains(shape)) + if (!thisShapesList.Contains(shape)) throw new ShapeException($"{shape.GetType().Name} is not part of the composite shape."); thisShapesList.Remove(shape); shape.CompositeShape = false; - } public void RemoveAllShapes() @@ -82,6 +74,5 @@ namespace Shapes thisShapesList.Remove(thisShapesList[0]); } } - } } \ No newline at end of file diff --git a/Shapes/FileIO.cs b/Shapes/FileIO.cs index a689b32..78a1959 100644 --- a/Shapes/FileIO.cs +++ b/Shapes/FileIO.cs @@ -1,9 +1,5 @@ -using System; using System.IO; -using System.Runtime.CompilerServices; using System.Runtime.Serialization.Json; -using System.Xml.Serialization; -using static System.Runtime.Serialization.Json.JsonReaderWriterFactory; namespace Shapes { @@ -11,7 +7,7 @@ namespace Shapes { public void SaveShape(Stream stream, Shape shape) { - DataContractJsonSerializer a = new DataContractJsonSerializer(typeof(Shape)); + var a = new DataContractJsonSerializer(typeof(Shape)); a.WriteObject(stream, shape); // DataContractJsonSerializer j = new DataContractJsonSerializer(typeof(Shape)); // j.WriteObject(stream, shape); @@ -20,8 +16,7 @@ namespace Shapes public T GetShapeFromFile(Stream stream) { var a = new DataContractJsonSerializer(typeof(T)); - return (T) a.ReadObject(stream); + return (T) a.ReadObject(stream); } - } } \ No newline at end of file diff --git a/Shapes/GeometricShape.cs b/Shapes/GeometricShape.cs index 06c75b6..99574bf 100644 --- a/Shapes/GeometricShape.cs +++ b/Shapes/GeometricShape.cs @@ -1,7 +1,4 @@ using System; -using System.Linq; -using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.Serialization; namespace Shapes @@ -12,20 +9,19 @@ namespace Shapes [KnownType(typeof(Triangle))] public abstract class GeometricShape : Shape { - [DataMember] - public abstract double Width { get; internal set; } - [DataMember] - public abstract double Height { get; internal set; } + [DataMember] public abstract double Width { get; internal set; } + + [DataMember] public abstract double Height { get; internal set; } + + [DataMember] public abstract Point CenterPoint { get; protected set; } - [DataMember] - public abstract Point CenterPoint { get; protected set; } internal abstract void ComputeCenter(); - + public void Rotate(double degrees) { - double radians = degrees * (Math.PI / 180); - double cosTheta = Math.Cos(radians); - double sinTheta = Math.Sin(radians); + var radians = degrees * (Math.PI / 180); + var cosTheta = Math.Cos(radians); + var sinTheta = Math.Sin(radians); foreach (var point in Points) { var oldPoint = point; @@ -34,15 +30,14 @@ namespace Shapes var tempX = oldPoint.X - CenterPoint.X; var tempY = oldPoint.Y - CenterPoint.X; - var rotatedX = tempX*cosTheta - tempY*sinTheta; - var rotatedY = tempX*sinTheta + tempY*cosTheta; + var rotatedX = tempX * cosTheta - tempY * sinTheta; + var rotatedY = tempX * sinTheta + tempY * cosTheta; point.X = rotatedX + CenterPoint.X; point.Y = rotatedY + CenterPoint.Y; - } } - + public void Move(double deltaX, double deltaY) { var tmpCenter = CenterPoint; @@ -50,7 +45,7 @@ namespace Shapes var property = GetType().GetProperties().Equals(nameof(Points)); Console.WriteLine(property.GetType()); - if(property) + if (property) { Console.WriteLine(property.GetType()); foreach (var point in Points) @@ -61,6 +56,7 @@ namespace Shapes point.Y = deltaY + tmpPoint.Y; } } + CenterPoint = new Point(deltaX, deltaY); } } diff --git a/Shapes/Image.cs b/Shapes/Image.cs index ad8971b..8338057 100644 --- a/Shapes/Image.cs +++ b/Shapes/Image.cs @@ -6,13 +6,14 @@ namespace Shapes { public class Image : Rectangle { - public Bitmap Picture { get; private set; } - public Image(Point point1, Point point2, Point point3, Point point4, Stream stream) : base(point1, point2, point3, point4) + public Image(Point point1, Point point2, Point point3, Point point4, Stream stream) : base(point1, point2, + point3, point4) { Picture = GetImage(stream); } - public Image(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, Stream stream) : base(x1, y1, x2, y2, x3, y3, x4, y4) + public Image(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, + Stream stream) : base(x1, y1, x2, y2, x3, y3, x4, y4) { Picture = GetImage(stream); } @@ -21,5 +22,7 @@ namespace Shapes { Picture = GetImage(stream); } + + public Bitmap Picture { get; } } } \ No newline at end of file diff --git a/Shapes/ImageManager.cs b/Shapes/ImageManager.cs index 4c197e1..aa9f6f5 100644 --- a/Shapes/ImageManager.cs +++ b/Shapes/ImageManager.cs @@ -6,7 +6,8 @@ namespace Shapes { public static class ImageManager { - private static Hashtable _images; + private static readonly Hashtable _images; + static ImageManager() { _images = new Hashtable(); @@ -18,16 +19,10 @@ namespace Shapes { return (Bitmap) _images[stream]; } - else - { - Bitmap bitmap = new Bitmap(stream); - _images.Add(stream, bitmap); - return bitmap; - } + var bitmap = new Bitmap(stream); + _images.Add(stream, bitmap); + return bitmap; } - - } - } \ No newline at end of file diff --git a/Shapes/Line.cs b/Shapes/Line.cs index 5df5269..c649f32 100644 --- a/Shapes/Line.cs +++ b/Shapes/Line.cs @@ -1,5 +1,4 @@ using System; -using System.Drawing; using System.Runtime.Serialization; namespace Shapes @@ -7,11 +6,6 @@ namespace Shapes [DataContract(Name = "Line", Namespace = "Shapes")] public class Line { - [DataMember] - public Point Point1 { get; private set; } - [DataMember] - public Point Point2 { get; private set; } - /** * Constructor based on x-y Locations * @param x1 The x-location of first point -- must be a valid double. @@ -34,12 +28,16 @@ namespace Shapes */ public Line(Point point1, Point point2) { - if (point1==null || point2==null) + if (point1 == null || point2 == null) throw new ShapeException("Invalid point"); Point1 = point1; Point2 = point2; } + [DataMember] public Point Point1 { get; private set; } + + [DataMember] public Point Point2 { get; private set; } + /** * Move a line * @@ -69,6 +67,5 @@ namespace Shapes { return (Point2.Y - Point1.Y) / (Point2.X - Point1.X); } - } } \ No newline at end of file diff --git a/Shapes/Point.cs b/Shapes/Point.cs index efff4b3..89d1fe7 100644 --- a/Shapes/Point.cs +++ b/Shapes/Point.cs @@ -6,29 +6,29 @@ namespace Shapes [DataContract(Name = "Points", Namespace = "Shapes")] public class Point { - [DataMember] - public double X { get; internal set; } - [DataMember] - public double Y { get; internal set; } - [DataMember] - public Color Color { get; set; } - public Point(double x, double y) { Validator.ValidateDouble(x, "Invalid x-location point"); Validator.ValidateDouble(y, "Invalid y-location point"); X = x; - Y = y; + Y = y; Color = Color.Black; } + [DataMember] public double X { get; internal set; } + + [DataMember] public double Y { get; internal set; } + + [DataMember] public Color Color { get; set; } + /** * Move the point in the x direction * * @param deltaX The delta amount to move the point -- must be a valid double * @throws ShapeException Exception thrown if the parameter is invalid */ - public void MoveX(double deltaX) { + public void MoveX(double deltaX) + { Validator.ValidateDouble(deltaX, "Invalid delta-x value"); X += deltaX; } @@ -51,7 +51,7 @@ namespace Shapes * @param deltaX The delta amount to move the point in the x direction -- must be a valid double * @param deltaY The delta amount to move the point in the y direction -- must be a valid double * @throws ShapeException Exception throw if any parameter is invalid - */ + */ public void Move(double deltaX, double deltaY) { MoveX(deltaX); @@ -74,20 +74,19 @@ namespace Shapes var y = point1.Y - point2.Y; return new Point(x, y); } - + public static Point operator +(Point point1, Point point2) { var x = point1.X + point2.X; var y = point1.Y + point2.Y; return new Point(x, y); } - + public static Point operator /(Point point1, double divisor) { var x = point1.X / divisor; var y = point1.Y / divisor; return new Point(x, y); } - } } \ No newline at end of file diff --git a/Shapes/Rectangle.cs b/Shapes/Rectangle.cs index 3599ea9..57b3b1d 100644 --- a/Shapes/Rectangle.cs +++ b/Shapes/Rectangle.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; @@ -10,33 +9,17 @@ namespace Shapes [DataContract] public class Rectangle : GeometricShape { - [DataMember] - public override List Points { get; internal set; } - [DataMember] - public override Color Fill { get; set; } - [DataMember] - public override Color Stroke { get; set; } - [DataMember] - public List Lines { get; } - [DataMember] - public override Point CenterPoint { get; protected set; } - [DataMember] - public sealed override double Width { get; internal set; } - [DataMember] - public sealed override double Height { get; internal set; } - - public Rectangle(Point point1, Point point2, Point point3, Point point4) { Stroke = Color.Black; Points = new List(); Lines = new List(); - + Points.Add(point1); Points.Add(point2); Points.Add(point3); Points.Add(point4); - + Lines.Add(new Line(point1, point2)); Lines.Add(new Line(point2, point3)); Lines.Add(new Line(point3, point4)); @@ -45,8 +28,8 @@ namespace Shapes Height = new Line(point1, point4).ComputeLength(); Width = new Line(point1, point2).ComputeLength(); - CenterPoint = new Point(point1.X + (Width/2), point1.Y + (Height/2)); - Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {this.GetType()}"); + CenterPoint = new Point(point1.X + Width / 2, point1.Y + Height / 2); + Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {GetType()}"); } public Rectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) @@ -64,9 +47,10 @@ namespace Shapes Height = new Line(point1, point2).ComputeLength(); Width = new Line(point1, point4).ComputeLength(); ComputeCenter(); - Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {this.GetType()}"); + Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {GetType()}"); } + /// public Rectangle(Point point, Size size) { Stroke = Color.Black; @@ -82,9 +66,23 @@ namespace Shapes Height = new Line(point1, point2).ComputeLength(); Width = new Line(point1, point4).ComputeLength(); ComputeCenter(); - Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {this.GetType()}"); + Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {GetType()}"); } - + + [DataMember] public override List Points { get; internal set; } + + [DataMember] public override Color Fill { get; set; } + + [DataMember] public override Color Stroke { get; set; } + + [DataMember] public List Lines { get; } + + [DataMember] public override Point CenterPoint { get; protected set; } + + [DataMember] public sealed override double Width { get; internal set; } + + [DataMember] public sealed override double Height { get; internal set; } + public override double ComputeArea() { return Height * Width; @@ -97,6 +95,7 @@ namespace Shapes point.X *= scaleFactor; point.Y *= scaleFactor; } + foreach (var line in Lines) { line.Point1.X *= scaleFactor; @@ -114,14 +113,14 @@ namespace Shapes public override void Draw(Stream stream) { - var tmp = new Bitmap(1000, 1000); - Pen blackPen = new Pen(Stroke, 3); + var tmp = new Bitmap((int) Width * 2, (int) Height * 2); + var blackPen = new Pen(Stroke, 3); // Draw line to screen. using (var graphics = Graphics.FromImage(tmp)) { - for (int i = 1; i < Points.Count; i++) - graphics.DrawLine(blackPen, - (float) Points[i - 1].X, + for (var i = 1; i < Points.Count; i++) + graphics.DrawLine(blackPen, + (float) Points[i - 1].X, (float) Points[i - 1].Y, (float) Points[i].X, (float) Points[i].Y); @@ -133,7 +132,6 @@ namespace Shapes public double CalculateWidth() { return new Line(Points[0], Points[3]).ComputeLength(); - } public double CalculateHeight() @@ -143,10 +141,7 @@ namespace Shapes internal override void ComputeCenter() { - CenterPoint = new Point((Points[0].X + Width)/2, (Points[0].Y + Height)/2); + CenterPoint = new Point((Points[0].X + Width) / 2, (Points[0].Y + Height) / 2); } - - - } } \ No newline at end of file diff --git a/Shapes/Shape.cs b/Shapes/Shape.cs index 210328d..669f754 100644 --- a/Shapes/Shape.cs +++ b/Shapes/Shape.cs @@ -1,8 +1,6 @@ -using System; using System.Collections.Generic; using System.Drawing; using System.IO; -using System.Reflection; using System.Runtime.Serialization; namespace Shapes @@ -11,21 +9,19 @@ namespace Shapes [KnownType(typeof(CompositeShape))] public abstract class Shape { - [DataMember] - public virtual List Points { get; internal set; } - - [DataMember] - public abstract Color Fill { get; set; } - [DataMember] - public abstract Color Stroke { get; set; } - [DataMember] - protected internal bool CompositeShape { get; set; } - public Shape() { CompositeShape = false; } + [DataMember] public virtual List Points { get; internal set; } + + [DataMember] public abstract Color Fill { get; set; } + + [DataMember] public abstract Color Stroke { get; set; } + + [DataMember] protected internal bool CompositeShape { get; set; } + public abstract double ComputeArea(); @@ -33,6 +29,5 @@ namespace Shapes public abstract void Save(Stream stream); public abstract void Draw(Stream stream); - } } \ No newline at end of file diff --git a/Shapes/ShapeException.cs b/Shapes/ShapeException.cs index 2f3459e..a1e24c1 100644 --- a/Shapes/ShapeException.cs +++ b/Shapes/ShapeException.cs @@ -1,11 +1,11 @@ +using System; + namespace Shapes { - - public class ShapeException : System.Exception + public class ShapeException : Exception { public ShapeException(string message) : base(message) { } } - } \ No newline at end of file diff --git a/Shapes/ShapeFactory.cs b/Shapes/ShapeFactory.cs index 022744c..e1c503c 100644 --- a/Shapes/ShapeFactory.cs +++ b/Shapes/ShapeFactory.cs @@ -1,4 +1,3 @@ -using System.ComponentModel.DataAnnotations; using System.IO; namespace Shapes @@ -7,7 +6,7 @@ namespace Shapes { public T GetShapeFromFile(FileStream fileStream) { - FileIO fi = new FileIO(); + var fi = new FileIO(); return fi.GetShapeFromFile(fileStream); } } diff --git a/Shapes/Triangle.cs b/Shapes/Triangle.cs index 7d8406c..0f9ba03 100644 --- a/Shapes/Triangle.cs +++ b/Shapes/Triangle.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; using System.Drawing; using System.Drawing.Imaging; using System.IO; -using System.Linq; using System.Runtime.Serialization; namespace Shapes @@ -12,46 +10,29 @@ namespace Shapes [DataContract(Name = "Triangle", Namespace = "Shapes")] public class Triangle : GeometricShape { - [DataMember] - public sealed override List Points { get; internal set; } - [DataMember] - public override Color Fill { get; set; } - [DataMember] - public override Color Stroke { get; set; } - [DataMember] - public override double Height { get; internal set; } - [DataMember] - public override double Width { get; internal set; } - [DataMember] - public sealed override Point CenterPoint { get; protected set; } - [DataMember] - public List Lines { get; internal set; } - public Triangle(Point point1, Point point2, Point point3) { Stroke = Color.Black; Points = new List(); Lines = new List(); - + Points.Add(point1); Points.Add(point2); Points.Add(point3); - + Lines.Add(new Line(point1, point2)); Lines.Add(new Line(point2, point3)); Lines.Add(new Line(point3, point1)); - Point centerOfLine3 = ((point1 - point3) / 2) + point1; - var centerTriangle = ((point1 - centerOfLine3) / 2) + point1; - - + var centerOfLine3 = (point1 - point3) / 2 + point1; + var centerTriangle = (point1 - centerOfLine3) / 2 + point1; + + CenterPoint = centerTriangle; Height = new Line(point1, centerOfLine3).ComputeLength(); Width = new Line(point1, point2).ComputeLength(); - - } public Triangle(double x1, double y1, double x2, double y2, double x3, double y3) @@ -62,27 +43,40 @@ namespace Shapes var point2 = new Point(x2, y2); var point3 = new Point(x3, y3); - + Points = new List(); Lines = new List(); - + Points.Add(point1); Points.Add(point2); Points.Add(point3); - + Lines.Add(new Line(point1, point2)); Lines.Add(new Line(point2, point3)); Lines.Add(new Line(point3, point1)); - - Point centerOfLine3 = ((point1 - point3) / 2) + point1; - var centerTriangle = ((point1 - centerOfLine3) / 2) + point1; + + var centerOfLine3 = (point1 - point3) / 2 + point1; + var centerTriangle = (point1 - centerOfLine3) / 2 + point1; CenterPoint = centerTriangle; } - + + [DataMember] public sealed override List Points { get; internal set; } + + [DataMember] public override Color Fill { get; set; } + + [DataMember] public override Color Stroke { get; set; } + + [DataMember] public override double Height { get; internal set; } + + [DataMember] public override double Width { get; internal set; } + + [DataMember] public sealed override Point CenterPoint { get; protected set; } + + [DataMember] public List Lines { get; internal set; } + public override double ComputeArea() { - var a = Lines[0].ComputeLength(); var b = Lines[1].ComputeLength(); var c = Lines[2].ComputeLength(); @@ -99,6 +93,7 @@ namespace Shapes point.X *= scaleFactor; point.Y *= scaleFactor; } + foreach (var line in Lines) { line.Point1.X *= scaleFactor; @@ -117,25 +112,26 @@ namespace Shapes public override void Draw(Stream stream) { - var tmp = new Bitmap(1000, 1000); - Pen blackPen = new Pen(Stroke, 3); + var tmp = new Bitmap((int) Width * 2, (int) Height * 2); + var blackPen = new Pen(Stroke, 3); // Draw line to screen. using (var graphics = Graphics.FromImage(tmp)) { - for (int i = 1; i < Points.Count; i++) - graphics.DrawLine(blackPen, - (float) Points[i - 1].X, + for (var i = 1; i < Points.Count; i++) + graphics.DrawLine(blackPen, + (float) Points[i - 1].X, (float) Points[i - 1].Y, (float) Points[i].X, (float) Points[i].Y); } - tmp.Save(stream, ImageFormat.Jpeg); } + tmp.Save(stream, ImageFormat.Jpeg); + } internal override void ComputeCenter() { - Point centerOfLine3 = ((Points[0] - Points[2]) / 2) + Points[0]; - var centerTriangle = ((Points[0] - centerOfLine3) / 2) + Points[0]; + var centerOfLine3 = (Points[0] - Points[2]) / 2 + Points[0]; + var centerTriangle = (Points[0] - centerOfLine3) / 2 + Points[0]; CenterPoint = centerTriangle; } } diff --git a/Shapes/Validator.cs b/Shapes/Validator.cs index f32e42d..1ec606f 100644 --- a/Shapes/Validator.cs +++ b/Shapes/Validator.cs @@ -1,56 +1,45 @@ using System; using System.Collections.Generic; -using System.Globalization; namespace Shapes { public class Validator { - public static void ValidateDouble(double value, string errorMessage) { if (double.IsInfinity(value) || double.IsNaN(value)) throw new ShapeException(errorMessage); } - public static void ValidatePositiveDouble(double value, String errorMessage) + public static void ValidatePositiveDouble(double value, string errorMessage) { ValidateDouble(value, errorMessage); if (value < 0) throw new ShapeException(errorMessage); } - public static void ValidateRectangle(List points, String errorMessage) - { - List pointList = new List(points); - - var TOLERANCE = Double.Epsilon + Double.Epsilon; + public static void ValidateRectangle(List points, string errorMessage) + { + var pointList = new List(points); + + var TOLERANCE = double.Epsilon + double.Epsilon; var plumLine1 = new Line(points[0], points[2]); var plumLine2 = new Line(points[1], points[3]); var heightLine1 = new Line(points[0], points[3]); - var heightLine2 = new Line( points[1], points[2]); + var heightLine2 = new Line(points[1], points[2]); var lengthLine1 = new Line(points[0], points[1]); var lengthLine2 = new Line(points[3], points[2]); - if (Math.Abs(plumLine1.ComputeLength() - plumLine2.ComputeLength()) > TOLERANCE - || Math.Abs(heightLine1.ComputeLength() - heightLine2.ComputeLength()) > TOLERANCE - || Math.Abs(lengthLine1.ComputeLength() - lengthLine2.ComputeLength()) > TOLERANCE ) - { + if (Math.Abs(plumLine1.ComputeLength() - plumLine2.ComputeLength()) > TOLERANCE + || Math.Abs(heightLine1.ComputeLength() - heightLine2.ComputeLength()) > TOLERANCE + || Math.Abs(lengthLine1.ComputeLength() - lengthLine2.ComputeLength()) > TOLERANCE) throw new ShapeException(errorMessage); - } - while(pointList.Count > 0) + while (pointList.Count > 0) { var tmp = pointList[0]; pointList.Remove(tmp); - if (pointList.Contains(tmp)) - { - throw new ShapeException(errorMessage); - } + if (pointList.Contains(tmp)) throw new ShapeException(errorMessage); } - } - - - } } \ No newline at end of file diff --git a/Temp/Program.cs b/Temp/Program.cs index 08cb31f..53c8030 100644 --- a/Temp/Program.cs +++ b/Temp/Program.cs @@ -1,7 +1,6 @@ using System; using System.Drawing; using System.IO; -using System.IO.Compression; using Shapes; using Image = Shapes.Image; using Point = Shapes.Point; @@ -9,60 +8,63 @@ using Rectangle = Shapes.Rectangle; namespace Temp { - class Program + internal class Program { - static void Main(string[] args) + private static void Main(string[] args) { var circle = new Circle(new Point(20, 20), 4); - + Console.WriteLine($"({circle.CenterPoint.X}, {circle.CenterPoint.Y})"); - circle.Move(5,5); + circle.Move(5, 5); Console.WriteLine($"({circle.CenterPoint.X}, {circle.CenterPoint.Y})"); - - + var line = new Line(3, 4, 4, 5); var tmp = line.ComputeLength(); var point = new Point(3, 3); - var rectangle = new Rectangle(new Point(3,3), new Point(5,3), new Point(5,5), new Point(3,5)); + var rectangle = new Rectangle(new Point(3, 3), new Point(5, 3), new Point(5, 5), new Point(3, 5)); //var rectangle2 = new Rectangle(new Point(3,0), new Point(3,0), new Point(3,2), new Point(0,3)); - Console.WriteLine($"({rectangle.Points[0].X}, {rectangle.Points[0].Y}), ({rectangle.Points[1].X}, {rectangle.Points[1].Y}), ({rectangle.Points[2].X}, {rectangle.Points[2].Y}), ({rectangle.Points[3].X}, {rectangle.Points[3].Y}) Height:{rectangle.CalculateHeight()} Width:{rectangle.CalculateWidth()} Center: {rectangle.CenterPoint.X}, {rectangle.CenterPoint.Y}"); + Console.WriteLine( + $"({rectangle.Points[0].X}, {rectangle.Points[0].Y}), ({rectangle.Points[1].X}, {rectangle.Points[1].Y}), ({rectangle.Points[2].X}, {rectangle.Points[2].Y}), ({rectangle.Points[3].X}, {rectangle.Points[3].Y}) Height:{rectangle.CalculateHeight()} Width:{rectangle.CalculateWidth()} Center: {rectangle.CenterPoint.X}, {rectangle.CenterPoint.Y}"); // rectangle.Rotate(180); // Console.WriteLine($"({rectangle.Points[0].X}, {rectangle.Points[0].Y}), ({rectangle.Points[1].X}, {rectangle.Points[1].Y}), ({rectangle.Points[2].X}, {rectangle.Points[2].Y}), ({rectangle.Points[3].X}, {rectangle.Points[3].Y}) Height:{rectangle.CalculateHeight()} Width:{rectangle.CalculateWidth()} Center: {rectangle.CenterPoint.X}, {rectangle.CenterPoint.Y}"); // rectangle.Rotate(180); // Console.WriteLine($"({rectangle.Points[0].X}, {rectangle.Points[0].Y}), ({rectangle.Points[1].X}, {rectangle.Points[1].Y}), ({rectangle.Points[2].X}, {rectangle.Points[2].Y}), ({rectangle.Points[3].X}, {rectangle.Points[3].Y}) Height:{rectangle.CalculateHeight()} Width:{rectangle.CalculateWidth()} Center: {rectangle.CenterPoint.X}, {rectangle.CenterPoint.Y}"); - rectangle.Move(15,10); - // Console.WriteLine($"({rectangle.Points[50].X}, {rectangle.Points[50].Y}), ({rectangle.Points[100].X}, {rectangle.Points[50].Y}), ({rectangle.Points[100].X}, {rectangle.Points[100].Y}), ({rectangle.Points[50].X}, {rectangle.Points[100].Y}) Height:{rectangle.CalculateHeight()} Width:{rectangle.CalculateWidth()} Center: {rectangle.CenterPoint.X}, {rectangle.CenterPoint.Y}"); + rectangle.Move(15, 10); + // Console.WriteLine($"({rectangle.Points[50].X}, {rectangle.Points[50].Y}), ({rectangle.Points[100].X}, {rectangle.Points[50].Y}), ({rectangle.Points[100].X}, {rectangle.Points[100].Y}), ({rectangle.Points[50].X}, {rectangle.Points[100].Y}) Height:{rectangle.CalculateHeight()} Width:{rectangle.CalculateWidth()} Center: {rectangle.CenterPoint.X}, {rectangle.CenterPoint.Y}"); - - - var triangle = new Triangle(new Point(3,3), new Point(7,3), new Point(7,6)); - Console.WriteLine($"\n \n ({triangle.Points[0].X}, {triangle.Points[0].Y}) ({triangle.Points[1].X}, {triangle.Points[1].Y}) ({triangle.Points[2].X}, {triangle.Points[2].Y}) Center: ({triangle.CenterPoint.X}, {triangle.CenterPoint.Y}) Area = {triangle.ComputeArea()} Height: {triangle.Height}"); + + var triangle = new Triangle(new Point(3, 3), new Point(7, 3), new Point(7, 6)); + Console.WriteLine( + $"\n \n ({triangle.Points[0].X}, {triangle.Points[0].Y}) ({triangle.Points[1].X}, {triangle.Points[1].Y}) ({triangle.Points[2].X}, {triangle.Points[2].Y}) Center: ({triangle.CenterPoint.X}, {triangle.CenterPoint.Y}) Area = {triangle.ComputeArea()} Height: {triangle.Height}"); triangle.Rotate(30); - Console.WriteLine($"({triangle.Points[0].X}, {triangle.Points[0].Y}) ({triangle.Points[1].X}, {triangle.Points[1].Y}) ({triangle.Points[2].X}, {triangle.Points[2].Y}) Center: ({triangle.CenterPoint.X}, {triangle.CenterPoint.Y}) Area = {triangle.ComputeArea()} Height: {triangle.Height}"); - - + Console.WriteLine( + $"({triangle.Points[0].X}, {triangle.Points[0].Y}) ({triangle.Points[1].X}, {triangle.Points[1].Y}) ({triangle.Points[2].X}, {triangle.Points[2].Y}) Center: ({triangle.CenterPoint.X}, {triangle.CenterPoint.Y}) Area = {triangle.ComputeArea()} Height: {triangle.Height}"); + + var composite = new CompositeShape(); - composite.Add(new Rectangle(new Point(500, 500), new Size(100, 100) )); - // composite.Add(triangle); + composite.Add(new Rectangle(new Point(500, 500), new Size(100, 100))); + // composite.Add(triangle); var composite2 = new CompositeShape(); composite2.Add(composite); - FileStream save = new FileStream(@"/Users/bradybodily/Documents/testing/testing.jpg", FileMode.Create); + var save = new FileStream(@"/Users/bradybodily/Documents/testing/testing.jpg", FileMode.Create); composite.Draw(save); - + composite2.RemoveShape(composite); composite.RemoveAllShapes(); - - FileStream wf = new FileStream(@"/Users/bradybodily/Documents/testing/test.txt", FileMode.Create); + + var wf = new FileStream(@"/Users/bradybodily/Documents/testing/test.txt", FileMode.Create); triangle.Save(wf); - - FileStream fs = new FileStream(@"/Users/bradybodily/Documents/testing/test.txt", FileMode.Open); + + var fs = new FileStream(@"/Users/bradybodily/Documents/testing/test.txt", FileMode.Open); var triangle2 = new ShapeFactory().GetShapeFromFile(fs); - Console.WriteLine($"({triangle2.Points[0].X}, {triangle2.Points[0].Y}) ({triangle2.Points[1].X}, {triangle2.Points[1].Y}) ({triangle2.Points[2].X}, {triangle2.Points[2].Y}) Center: ({triangle2.CenterPoint.X}, {triangle2.CenterPoint.Y}) Area = {triangle2.ComputeArea()} Height: {triangle2.Height}"); + Console.WriteLine( + $"({triangle2.Points[0].X}, {triangle2.Points[0].Y}) ({triangle2.Points[1].X}, {triangle2.Points[1].Y}) ({triangle2.Points[2].X}, {triangle2.Points[2].Y}) Center: ({triangle2.CenterPoint.X}, {triangle2.CenterPoint.Y}) Area = {triangle2.ComputeArea()} Height: {triangle2.Height}"); - FileStream imf = new FileStream(@"/Users/bradybodily/Documents/testing/visual-reverse-image-search-v2_intro.jpg", FileMode.Create); + var imf = new FileStream(@"/Users/bradybodily/Documents/testing/visual-reverse-image-search-v2_intro.jpg", + FileMode.Create); - Image i = new Image(new Point(20,20), new Size(20, 20), imf ); + var i = new Image(new Point(20, 20), new Size(20, 20), imf); } } } \ No newline at end of file diff --git a/UnitTests/UnitTest1.cs b/UnitTests/CircleTests.cs similarity index 87% rename from UnitTests/UnitTest1.cs rename to UnitTests/CircleTests.cs index 794c408..82f3a2b 100644 --- a/UnitTests/UnitTest1.cs +++ b/UnitTests/CircleTests.cs @@ -2,7 +2,7 @@ using NUnit.Framework; namespace Tests { - public class Tests + public class CircleTests { [SetUp] public void Setup() diff --git a/UnitTests/RectangleTests.cs b/UnitTests/RectangleTests.cs new file mode 100644 index 0000000..86be801 --- /dev/null +++ b/UnitTests/RectangleTests.cs @@ -0,0 +1,34 @@ +using NUnit.Framework; +using Moq; +using Shapes; + +namespace Tests +{ + public class RectangleTests + { + private Mock point1; + [SetUp] + public void Setup() + { + point1 = new Mock(); + + } + + [Test] + public void RectangleWithPoints() + { + var rectangle = new Rectangle( + new Point(20,20), + new Point(30,20), + new Point(30,30), + new Point(20,30)); + Assert.IsTrue(rectangle.GetType() == typeof(Rectangle)); + Assert.IsTrue(rectangle.Height == 10 && rectangle.Width == 10 ); + Assert.IsTrue(rectangle.Lines.Count == 4); + foreach (var line in rectangle.Lines) + { + Assert.IsTrue(line.ComputeLength() == 10); + } + } + } +} \ No newline at end of file diff --git a/UnitTests/ShapeFactoryTests.cs b/UnitTests/ShapeFactoryTests.cs new file mode 100644 index 0000000..1451c22 --- /dev/null +++ b/UnitTests/ShapeFactoryTests.cs @@ -0,0 +1,18 @@ +using NUnit.Framework; + +namespace Tests +{ + public class ShapeFactoryTests + { + [SetUp] + public void Setup() + { + } + + [Test] + public void Test1() + { + Assert.Pass(); + } + } +} \ No newline at end of file diff --git a/UnitTests/TriangleTests.cs b/UnitTests/TriangleTests.cs new file mode 100644 index 0000000..b204d2d --- /dev/null +++ b/UnitTests/TriangleTests.cs @@ -0,0 +1,18 @@ +using NUnit.Framework; + +namespace Tests +{ + public class TriangleTests + { + [SetUp] + public void Setup() + { + } + + [Test] + public void Test1() + { + Assert.Pass(); + } + } +} \ No newline at end of file diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 5005a03..9d2aafc 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -7,6 +7,7 @@ +