first unit test

This commit is contained in:
2019-10-25 03:16:17 -06:00
parent 6901925c69
commit 93b22ef2ec
21 changed files with 275 additions and 254 deletions

View File

@@ -26,7 +26,6 @@
</e> </e>
<e p="Point.cs" t="Include" /> <e p="Point.cs" t="Include" />
<e p="Rectangle.cs" t="Include" /> <e p="Rectangle.cs" t="Include" />
<e p="rose-blue-flower-rose-blooms-67636.jpg" t="Include" />
<e p="Shape.cs" t="Include" /> <e p="Shape.cs" t="Include" />
<e p="ShapeException.cs" t="Include" /> <e p="ShapeException.cs" t="Include" />
<e p="ShapeFactory.cs" t="Include" /> <e p="ShapeFactory.cs" t="Include" />
@@ -49,6 +48,7 @@
</e> </e>
<e p="UnitTests" t="IncludeRecursive"> <e p="UnitTests" t="IncludeRecursive">
<e p="bin" t="ExcludeRecursive" /> <e p="bin" t="ExcludeRecursive" />
<e p="CircleTests.cs" t="Include" />
<e p="obj" t="ExcludeRecursive"> <e p="obj" t="ExcludeRecursive">
<e p="Debug" t="Include"> <e p="Debug" t="Include">
<e p="netcoreapp2.2" t="Include"> <e p="netcoreapp2.2" t="Include">
@@ -57,7 +57,9 @@
</e> </e>
</e> </e>
</e> </e>
<e p="UnitTest1.cs" t="Include" /> <e p="RectangleTests.cs" t="Include" />
<e p="ShapeFactoryTests.cs" t="Include" />
<e p="TriangleTests.cs" t="Include" />
<e p="UnitTests.csproj" t="IncludeRecursive" /> <e p="UnitTests.csproj" t="IncludeRecursive" />
</e> </e>
</e> </e>

View File

@@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
@@ -11,23 +9,6 @@ namespace Shapes
[DataContract] [DataContract]
public class Circle : GeometricShape 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 * 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. * @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 * @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; Stroke = Color.Black;
Validator.ValidatePositiveDouble(radius, "Invalid radius"); Validator.ValidatePositiveDouble(radius, "Invalid radius");
if (center == null) if (center == null)
@@ -64,7 +46,23 @@ namespace Shapes
Width = radius * 2; 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 * Scale the circle
@@ -83,19 +81,18 @@ namespace Shapes
public override void Save(Stream stream) public override void Save(Stream stream)
{ {
var fileWriter = new FileIO(); var fileWriter = new FileIO();
fileWriter.SaveShape(stream, this); fileWriter.SaveShape(stream, this);
} }
public override void Draw(Stream stream) public override void Draw(Stream stream)
{ {
var tmp = new Bitmap((int) Radius * 4, (int) Radius * 4); 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. // Draw line to screen.
using (var graphics = Graphics.FromImage(tmp)) using (var graphics = Graphics.FromImage(tmp))
{ {
graphics.DrawEllipse(blackPen, (float) CenterPoint.X, (float) CenterPoint.Y, (float) Radius * 2, graphics.DrawEllipse(blackPen, (float) CenterPoint.X, (float) CenterPoint.Y, (float) Radius * 2,
(float) Radius * 2); (float) Radius * 2);
} }
tmp.Save(stream, ImageFormat.Jpeg); tmp.Save(stream, ImageFormat.Jpeg);
@@ -108,6 +105,5 @@ namespace Shapes
{ {
return Math.PI * Math.Pow(Radius, 2); return Math.PI * Math.Pow(Radius, 2);
} }
} }
} }

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
@@ -9,22 +8,21 @@ namespace Shapes
{ {
public class CompositeShape : Shape public class CompositeShape : Shape
{ {
private List<Shape> thisShapesList = new List<Shape>(); private readonly List<Shape> thisShapesList = new List<Shape>();
public override Color Fill { get; set; } public override Color Fill { get; set; }
public override Color Stroke { get; set; } public override Color Stroke { get; set; }
public void Add(Shape shape) public void Add(Shape shape)
{ {
if(shape.CompositeShape) if (shape.CompositeShape)
throw new ShapeException($"This shape has already been added to a composite"); throw new ShapeException("This shape has already been added to a composite");
if(shape == this) if (shape == this)
throw new ShapeException($"You cant add a shape to itself"); throw new ShapeException("You cant add a shape to itself");
thisShapesList.Add(shape); thisShapesList.Add(shape);
shape.CompositeShape = true; shape.CompositeShape = true;
} }
public override double ComputeArea() public override double ComputeArea()
{ {
@@ -33,32 +31,27 @@ namespace Shapes
public override void Scale(double scaleFactor) public override void Scale(double scaleFactor)
{ {
foreach (var shape in thisShapesList) foreach (var shape in thisShapesList) shape.Scale(scaleFactor);
{
shape.Scale(scaleFactor);
}
} }
public override void Save(Stream stream) public override void Save(Stream stream)
{ {
var fileWriter = new FileIO(); var fileWriter = new FileIO();
fileWriter.SaveShape(stream, this); fileWriter.SaveShape(stream, this);
} }
public override void Draw(Stream stream) public override void Draw(Stream stream)
{ {
var tmp = new Bitmap(1000, 1000); var tmp = new Bitmap(1000, 1000);
Pen blackPen = new Pen(Stroke, 3); var blackPen = new Pen(Stroke, 3);
// Draw line to screen. // Draw line to screen.
using (var graphics = Graphics.FromImage(tmp)) using (var graphics = Graphics.FromImage(tmp))
{ {
foreach (var shape in thisShapesList) foreach (var shape in thisShapesList)
{ for (var i = 1; i < shape.Points.Count; i++)
for (int i = 1; i < shape.Points.Count; i++) graphics.DrawLine(blackPen, (float) shape.Points[i - 1].X, (float) shape.Points[i - 1].Y,
graphics.DrawLine(blackPen, (float) shape.Points[i - 1].X, (float) shape.Points[i - 1].Y, (float) shape.Points[i].X, (float) shape.Points[i].X,
(float) shape.Points[i].Y); (float) shape.Points[i].Y);
}
} }
tmp.Save(stream, ImageFormat.Jpeg); tmp.Save(stream, ImageFormat.Jpeg);
@@ -67,11 +60,10 @@ namespace Shapes
public void RemoveShape(Shape shape) 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."); throw new ShapeException($"{shape.GetType().Name} is not part of the composite shape.");
thisShapesList.Remove(shape); thisShapesList.Remove(shape);
shape.CompositeShape = false; shape.CompositeShape = false;
} }
public void RemoveAllShapes() public void RemoveAllShapes()
@@ -82,6 +74,5 @@ namespace Shapes
thisShapesList.Remove(thisShapesList[0]); thisShapesList.Remove(thisShapesList[0]);
} }
} }
} }
} }

View File

@@ -1,9 +1,5 @@
using System;
using System.IO; using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Json; using System.Runtime.Serialization.Json;
using System.Xml.Serialization;
using static System.Runtime.Serialization.Json.JsonReaderWriterFactory;
namespace Shapes namespace Shapes
{ {
@@ -11,7 +7,7 @@ namespace Shapes
{ {
public void SaveShape(Stream stream, Shape shape) public void SaveShape(Stream stream, Shape shape)
{ {
DataContractJsonSerializer a = new DataContractJsonSerializer(typeof(Shape)); var a = new DataContractJsonSerializer(typeof(Shape));
a.WriteObject(stream, shape); a.WriteObject(stream, shape);
// DataContractJsonSerializer j = new DataContractJsonSerializer(typeof(Shape)); // DataContractJsonSerializer j = new DataContractJsonSerializer(typeof(Shape));
// j.WriteObject(stream, shape); // j.WriteObject(stream, shape);
@@ -20,8 +16,7 @@ namespace Shapes
public T GetShapeFromFile<T>(Stream stream) public T GetShapeFromFile<T>(Stream stream)
{ {
var a = new DataContractJsonSerializer(typeof(T)); var a = new DataContractJsonSerializer(typeof(T));
return (T) a.ReadObject(stream); return (T) a.ReadObject(stream);
} }
} }
} }

View File

@@ -1,7 +1,4 @@
using System; using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace Shapes namespace Shapes
@@ -12,20 +9,19 @@ namespace Shapes
[KnownType(typeof(Triangle))] [KnownType(typeof(Triangle))]
public abstract class GeometricShape : Shape public abstract class GeometricShape : Shape
{ {
[DataMember] [DataMember] public abstract double Width { get; internal set; }
public abstract double Width { get; internal set; }
[DataMember] [DataMember] public abstract double Height { get; internal set; }
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(); internal abstract void ComputeCenter();
public void Rotate(double degrees) public void Rotate(double degrees)
{ {
double radians = degrees * (Math.PI / 180); var radians = degrees * (Math.PI / 180);
double cosTheta = Math.Cos(radians); var cosTheta = Math.Cos(radians);
double sinTheta = Math.Sin(radians); var sinTheta = Math.Sin(radians);
foreach (var point in Points) foreach (var point in Points)
{ {
var oldPoint = point; var oldPoint = point;
@@ -34,15 +30,14 @@ namespace Shapes
var tempX = oldPoint.X - CenterPoint.X; var tempX = oldPoint.X - CenterPoint.X;
var tempY = oldPoint.Y - CenterPoint.X; var tempY = oldPoint.Y - CenterPoint.X;
var rotatedX = tempX*cosTheta - tempY*sinTheta; var rotatedX = tempX * cosTheta - tempY * sinTheta;
var rotatedY = tempX*sinTheta + tempY*cosTheta; var rotatedY = tempX * sinTheta + tempY * cosTheta;
point.X = rotatedX + CenterPoint.X; point.X = rotatedX + CenterPoint.X;
point.Y = rotatedY + CenterPoint.Y; point.Y = rotatedY + CenterPoint.Y;
} }
} }
public void Move(double deltaX, double deltaY) public void Move(double deltaX, double deltaY)
{ {
var tmpCenter = CenterPoint; var tmpCenter = CenterPoint;
@@ -50,7 +45,7 @@ namespace Shapes
var property = GetType().GetProperties().Equals(nameof(Points)); var property = GetType().GetProperties().Equals(nameof(Points));
Console.WriteLine(property.GetType()); Console.WriteLine(property.GetType());
if(property) if (property)
{ {
Console.WriteLine(property.GetType()); Console.WriteLine(property.GetType());
foreach (var point in Points) foreach (var point in Points)
@@ -61,6 +56,7 @@ namespace Shapes
point.Y = deltaY + tmpPoint.Y; point.Y = deltaY + tmpPoint.Y;
} }
} }
CenterPoint = new Point(deltaX, deltaY); CenterPoint = new Point(deltaX, deltaY);
} }
} }

View File

@@ -6,13 +6,14 @@ namespace Shapes
{ {
public class Image : Rectangle 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,
public Image(Point point1, Point point2, Point point3, Point point4, Stream stream) : base(point1, point2, point3, point4) point3, point4)
{ {
Picture = GetImage(stream); 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); Picture = GetImage(stream);
} }
@@ -21,5 +22,7 @@ namespace Shapes
{ {
Picture = GetImage(stream); Picture = GetImage(stream);
} }
public Bitmap Picture { get; }
} }
} }

View File

@@ -6,7 +6,8 @@ namespace Shapes
{ {
public static class ImageManager public static class ImageManager
{ {
private static Hashtable _images; private static readonly Hashtable _images;
static ImageManager() static ImageManager()
{ {
_images = new Hashtable(); _images = new Hashtable();
@@ -18,16 +19,10 @@ namespace Shapes
{ {
return (Bitmap) _images[stream]; 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;
} }
} }
} }

View File

@@ -1,5 +1,4 @@
using System; using System;
using System.Drawing;
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace Shapes namespace Shapes
@@ -7,11 +6,6 @@ namespace Shapes
[DataContract(Name = "Line", Namespace = "Shapes")] [DataContract(Name = "Line", Namespace = "Shapes")]
public class Line public class Line
{ {
[DataMember]
public Point Point1 { get; private set; }
[DataMember]
public Point Point2 { get; private set; }
/** /**
* Constructor based on x-y Locations * Constructor based on x-y Locations
* @param x1 The x-location of first point -- must be a valid double. * @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) public Line(Point point1, Point point2)
{ {
if (point1==null || point2==null) if (point1 == null || point2 == null)
throw new ShapeException("Invalid point"); throw new ShapeException("Invalid point");
Point1 = point1; Point1 = point1;
Point2 = point2; Point2 = point2;
} }
[DataMember] public Point Point1 { get; private set; }
[DataMember] public Point Point2 { get; private set; }
/** /**
* Move a line * Move a line
* *
@@ -69,6 +67,5 @@ namespace Shapes
{ {
return (Point2.Y - Point1.Y) / (Point2.X - Point1.X); return (Point2.Y - Point1.Y) / (Point2.X - Point1.X);
} }
} }
} }

View File

@@ -6,29 +6,29 @@ namespace Shapes
[DataContract(Name = "Points", Namespace = "Shapes")] [DataContract(Name = "Points", Namespace = "Shapes")]
public class Point 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) public Point(double x, double y)
{ {
Validator.ValidateDouble(x, "Invalid x-location point"); Validator.ValidateDouble(x, "Invalid x-location point");
Validator.ValidateDouble(y, "Invalid y-location point"); Validator.ValidateDouble(y, "Invalid y-location point");
X = x; X = x;
Y = y; Y = y;
Color = Color.Black; 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 * Move the point in the x direction
* *
* @param deltaX The delta amount to move the point -- must be a valid double * @param deltaX The delta amount to move the point -- must be a valid double
* @throws ShapeException Exception thrown if the parameter is invalid * @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"); Validator.ValidateDouble(deltaX, "Invalid delta-x value");
X += deltaX; 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 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 * @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 * @throws ShapeException Exception throw if any parameter is invalid
*/ */
public void Move(double deltaX, double deltaY) public void Move(double deltaX, double deltaY)
{ {
MoveX(deltaX); MoveX(deltaX);
@@ -74,20 +74,19 @@ namespace Shapes
var y = point1.Y - point2.Y; var y = point1.Y - point2.Y;
return new Point(x, y); return new Point(x, y);
} }
public static Point operator +(Point point1, Point point2) public static Point operator +(Point point1, Point point2)
{ {
var x = point1.X + point2.X; var x = point1.X + point2.X;
var y = point1.Y + point2.Y; var y = point1.Y + point2.Y;
return new Point(x, y); return new Point(x, y);
} }
public static Point operator /(Point point1, double divisor) public static Point operator /(Point point1, double divisor)
{ {
var x = point1.X / divisor; var x = point1.X / divisor;
var y = point1.Y / divisor; var y = point1.Y / divisor;
return new Point(x, y); return new Point(x, y);
} }
} }
} }

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
@@ -10,33 +9,17 @@ namespace Shapes
[DataContract] [DataContract]
public class Rectangle : GeometricShape public class Rectangle : GeometricShape
{ {
[DataMember]
public override List<Point> Points { get; internal set; }
[DataMember]
public override Color Fill { get; set; }
[DataMember]
public override Color Stroke { get; set; }
[DataMember]
public List<Line> 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) public Rectangle(Point point1, Point point2, Point point3, Point point4)
{ {
Stroke = Color.Black; Stroke = Color.Black;
Points = new List<Point>(); Points = new List<Point>();
Lines = new List<Line>(); Lines = new List<Line>();
Points.Add(point1); Points.Add(point1);
Points.Add(point2); Points.Add(point2);
Points.Add(point3); Points.Add(point3);
Points.Add(point4); Points.Add(point4);
Lines.Add(new Line(point1, point2)); Lines.Add(new Line(point1, point2));
Lines.Add(new Line(point2, point3)); Lines.Add(new Line(point2, point3));
Lines.Add(new Line(point3, point4)); Lines.Add(new Line(point3, point4));
@@ -45,8 +28,8 @@ namespace Shapes
Height = new Line(point1, point4).ComputeLength(); Height = new Line(point1, point4).ComputeLength();
Width = new Line(point1, point2).ComputeLength(); Width = new Line(point1, point2).ComputeLength();
CenterPoint = new Point(point1.X + (Width/2), point1.Y + (Height/2)); CenterPoint = new Point(point1.X + Width / 2, point1.Y + Height / 2);
Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {this.GetType()}"); 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) 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(); Height = new Line(point1, point2).ComputeLength();
Width = new Line(point1, point4).ComputeLength(); Width = new Line(point1, point4).ComputeLength();
ComputeCenter(); ComputeCenter();
Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {this.GetType()}"); Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {GetType()}");
} }
/// <inheritdoc />
public Rectangle(Point point, Size size) public Rectangle(Point point, Size size)
{ {
Stroke = Color.Black; Stroke = Color.Black;
@@ -82,9 +66,23 @@ namespace Shapes
Height = new Line(point1, point2).ComputeLength(); Height = new Line(point1, point2).ComputeLength();
Width = new Line(point1, point4).ComputeLength(); Width = new Line(point1, point4).ComputeLength();
ComputeCenter(); 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<Point> Points { get; internal set; }
[DataMember] public override Color Fill { get; set; }
[DataMember] public override Color Stroke { get; set; }
[DataMember] public List<Line> 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() public override double ComputeArea()
{ {
return Height * Width; return Height * Width;
@@ -97,6 +95,7 @@ namespace Shapes
point.X *= scaleFactor; point.X *= scaleFactor;
point.Y *= scaleFactor; point.Y *= scaleFactor;
} }
foreach (var line in Lines) foreach (var line in Lines)
{ {
line.Point1.X *= scaleFactor; line.Point1.X *= scaleFactor;
@@ -114,14 +113,14 @@ namespace Shapes
public override void Draw(Stream stream) public override void Draw(Stream stream)
{ {
var tmp = new Bitmap(1000, 1000); var tmp = new Bitmap((int) Width * 2, (int) Height * 2);
Pen blackPen = new Pen(Stroke, 3); var blackPen = new Pen(Stroke, 3);
// Draw line to screen. // Draw line to screen.
using (var graphics = Graphics.FromImage(tmp)) using (var graphics = Graphics.FromImage(tmp))
{ {
for (int i = 1; i < Points.Count; i++) for (var i = 1; i < Points.Count; i++)
graphics.DrawLine(blackPen, graphics.DrawLine(blackPen,
(float) Points[i - 1].X, (float) Points[i - 1].X,
(float) Points[i - 1].Y, (float) Points[i - 1].Y,
(float) Points[i].X, (float) Points[i].X,
(float) Points[i].Y); (float) Points[i].Y);
@@ -133,7 +132,6 @@ namespace Shapes
public double CalculateWidth() public double CalculateWidth()
{ {
return new Line(Points[0], Points[3]).ComputeLength(); return new Line(Points[0], Points[3]).ComputeLength();
} }
public double CalculateHeight() public double CalculateHeight()
@@ -143,10 +141,7 @@ namespace Shapes
internal override void ComputeCenter() 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);
} }
} }
} }

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace Shapes namespace Shapes
@@ -11,21 +9,19 @@ namespace Shapes
[KnownType(typeof(CompositeShape))] [KnownType(typeof(CompositeShape))]
public abstract class Shape public abstract class Shape
{ {
[DataMember]
public virtual List<Point> 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() public Shape()
{ {
CompositeShape = false; CompositeShape = false;
} }
[DataMember] public virtual List<Point> 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(); public abstract double ComputeArea();
@@ -33,6 +29,5 @@ namespace Shapes
public abstract void Save(Stream stream); public abstract void Save(Stream stream);
public abstract void Draw(Stream stream); public abstract void Draw(Stream stream);
} }
} }

View File

@@ -1,11 +1,11 @@
using System;
namespace Shapes namespace Shapes
{ {
public class ShapeException : Exception
public class ShapeException : System.Exception
{ {
public ShapeException(string message) : base(message) public ShapeException(string message) : base(message)
{ {
} }
} }
} }

View File

@@ -1,4 +1,3 @@
using System.ComponentModel.DataAnnotations;
using System.IO; using System.IO;
namespace Shapes namespace Shapes
@@ -7,7 +6,7 @@ namespace Shapes
{ {
public T GetShapeFromFile<T>(FileStream fileStream) public T GetShapeFromFile<T>(FileStream fileStream)
{ {
FileIO fi = new FileIO(); var fi = new FileIO();
return fi.GetShapeFromFile<T>(fileStream); return fi.GetShapeFromFile<T>(fileStream);
} }
} }

View File

@@ -1,10 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace Shapes namespace Shapes
@@ -12,46 +10,29 @@ namespace Shapes
[DataContract(Name = "Triangle", Namespace = "Shapes")] [DataContract(Name = "Triangle", Namespace = "Shapes")]
public class Triangle : GeometricShape public class Triangle : GeometricShape
{ {
[DataMember]
public sealed override List<Point> 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<Line> Lines { get; internal set; }
public Triangle(Point point1, Point point2, Point point3) public Triangle(Point point1, Point point2, Point point3)
{ {
Stroke = Color.Black; Stroke = Color.Black;
Points = new List<Point>(); Points = new List<Point>();
Lines = new List<Line>(); Lines = new List<Line>();
Points.Add(point1); Points.Add(point1);
Points.Add(point2); Points.Add(point2);
Points.Add(point3); Points.Add(point3);
Lines.Add(new Line(point1, point2)); Lines.Add(new Line(point1, point2));
Lines.Add(new Line(point2, point3)); Lines.Add(new Line(point2, point3));
Lines.Add(new Line(point3, point1)); Lines.Add(new Line(point3, point1));
Point centerOfLine3 = ((point1 - point3) / 2) + point1; var centerOfLine3 = (point1 - point3) / 2 + point1;
var centerTriangle = ((point1 - centerOfLine3) / 2) + point1; var centerTriangle = (point1 - centerOfLine3) / 2 + point1;
CenterPoint = centerTriangle; CenterPoint = centerTriangle;
Height = new Line(point1, centerOfLine3).ComputeLength(); Height = new Line(point1, centerOfLine3).ComputeLength();
Width = new Line(point1, point2).ComputeLength(); Width = new Line(point1, point2).ComputeLength();
} }
public Triangle(double x1, double y1, double x2, double y2, double x3, double y3) 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 point2 = new Point(x2, y2);
var point3 = new Point(x3, y3); var point3 = new Point(x3, y3);
Points = new List<Point>(); Points = new List<Point>();
Lines = new List<Line>(); Lines = new List<Line>();
Points.Add(point1); Points.Add(point1);
Points.Add(point2); Points.Add(point2);
Points.Add(point3); Points.Add(point3);
Lines.Add(new Line(point1, point2)); Lines.Add(new Line(point1, point2));
Lines.Add(new Line(point2, point3)); Lines.Add(new Line(point2, point3));
Lines.Add(new Line(point3, point1)); Lines.Add(new Line(point3, point1));
Point centerOfLine3 = ((point1 - point3) / 2) + point1; var centerOfLine3 = (point1 - point3) / 2 + point1;
var centerTriangle = ((point1 - centerOfLine3) / 2) + point1; var centerTriangle = (point1 - centerOfLine3) / 2 + point1;
CenterPoint = centerTriangle; CenterPoint = centerTriangle;
} }
[DataMember] public sealed override List<Point> 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<Line> Lines { get; internal set; }
public override double ComputeArea() public override double ComputeArea()
{ {
var a = Lines[0].ComputeLength(); var a = Lines[0].ComputeLength();
var b = Lines[1].ComputeLength(); var b = Lines[1].ComputeLength();
var c = Lines[2].ComputeLength(); var c = Lines[2].ComputeLength();
@@ -99,6 +93,7 @@ namespace Shapes
point.X *= scaleFactor; point.X *= scaleFactor;
point.Y *= scaleFactor; point.Y *= scaleFactor;
} }
foreach (var line in Lines) foreach (var line in Lines)
{ {
line.Point1.X *= scaleFactor; line.Point1.X *= scaleFactor;
@@ -117,25 +112,26 @@ namespace Shapes
public override void Draw(Stream stream) public override void Draw(Stream stream)
{ {
var tmp = new Bitmap(1000, 1000); var tmp = new Bitmap((int) Width * 2, (int) Height * 2);
Pen blackPen = new Pen(Stroke, 3); var blackPen = new Pen(Stroke, 3);
// Draw line to screen. // Draw line to screen.
using (var graphics = Graphics.FromImage(tmp)) using (var graphics = Graphics.FromImage(tmp))
{ {
for (int i = 1; i < Points.Count; i++) for (var i = 1; i < Points.Count; i++)
graphics.DrawLine(blackPen, graphics.DrawLine(blackPen,
(float) Points[i - 1].X, (float) Points[i - 1].X,
(float) Points[i - 1].Y, (float) Points[i - 1].Y,
(float) Points[i].X, (float) Points[i].X,
(float) Points[i].Y); (float) Points[i].Y);
} }
tmp.Save(stream, ImageFormat.Jpeg); } tmp.Save(stream, ImageFormat.Jpeg);
}
internal override void ComputeCenter() internal override void ComputeCenter()
{ {
Point centerOfLine3 = ((Points[0] - Points[2]) / 2) + Points[0]; var centerOfLine3 = (Points[0] - Points[2]) / 2 + Points[0];
var centerTriangle = ((Points[0] - centerOfLine3) / 2) + Points[0]; var centerTriangle = (Points[0] - centerOfLine3) / 2 + Points[0];
CenterPoint = centerTriangle; CenterPoint = centerTriangle;
} }
} }

View File

@@ -1,56 +1,45 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
namespace Shapes namespace Shapes
{ {
public class Validator public class Validator
{ {
public static void ValidateDouble(double value, string errorMessage) public static void ValidateDouble(double value, string errorMessage)
{ {
if (double.IsInfinity(value) || double.IsNaN(value)) if (double.IsInfinity(value) || double.IsNaN(value))
throw new ShapeException(errorMessage); throw new ShapeException(errorMessage);
} }
public static void ValidatePositiveDouble(double value, String errorMessage) public static void ValidatePositiveDouble(double value, string errorMessage)
{ {
ValidateDouble(value, errorMessage); ValidateDouble(value, errorMessage);
if (value < 0) if (value < 0)
throw new ShapeException(errorMessage); throw new ShapeException(errorMessage);
} }
public static void ValidateRectangle(List<Point> points, String errorMessage) public static void ValidateRectangle(List<Point> points, string errorMessage)
{ {
List<Point> pointList = new List<Point>(points); var pointList = new List<Point>(points);
var TOLERANCE = Double.Epsilon + Double.Epsilon; var TOLERANCE = double.Epsilon + double.Epsilon;
var plumLine1 = new Line(points[0], points[2]); var plumLine1 = new Line(points[0], points[2]);
var plumLine2 = new Line(points[1], points[3]); var plumLine2 = new Line(points[1], points[3]);
var heightLine1 = new Line(points[0], 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 lengthLine1 = new Line(points[0], points[1]);
var lengthLine2 = new Line(points[3], points[2]); var lengthLine2 = new Line(points[3], points[2]);
if (Math.Abs(plumLine1.ComputeLength() - plumLine2.ComputeLength()) > TOLERANCE if (Math.Abs(plumLine1.ComputeLength() - plumLine2.ComputeLength()) > TOLERANCE
|| Math.Abs(heightLine1.ComputeLength() - heightLine2.ComputeLength()) > TOLERANCE || Math.Abs(heightLine1.ComputeLength() - heightLine2.ComputeLength()) > TOLERANCE
|| Math.Abs(lengthLine1.ComputeLength() - lengthLine2.ComputeLength()) > TOLERANCE ) || Math.Abs(lengthLine1.ComputeLength() - lengthLine2.ComputeLength()) > TOLERANCE)
{
throw new ShapeException(errorMessage); throw new ShapeException(errorMessage);
}
while(pointList.Count > 0) while (pointList.Count > 0)
{ {
var tmp = pointList[0]; var tmp = pointList[0];
pointList.Remove(tmp); pointList.Remove(tmp);
if (pointList.Contains(tmp)) if (pointList.Contains(tmp)) throw new ShapeException(errorMessage);
{
throw new ShapeException(errorMessage);
}
} }
} }
} }
} }

View File

@@ -1,7 +1,6 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.IO.Compression;
using Shapes; using Shapes;
using Image = Shapes.Image; using Image = Shapes.Image;
using Point = Shapes.Point; using Point = Shapes.Point;
@@ -9,60 +8,63 @@ using Rectangle = Shapes.Rectangle;
namespace Temp 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); var circle = new Circle(new Point(20, 20), 4);
Console.WriteLine($"({circle.CenterPoint.X}, {circle.CenterPoint.Y})"); Console.WriteLine($"({circle.CenterPoint.X}, {circle.CenterPoint.Y})");
circle.Move(5,5); circle.Move(5, 5);
Console.WriteLine($"({circle.CenterPoint.X}, {circle.CenterPoint.Y})"); Console.WriteLine($"({circle.CenterPoint.X}, {circle.CenterPoint.Y})");
var line = new Line(3, 4, 4, 5); var line = new Line(3, 4, 4, 5);
var tmp = line.ComputeLength(); var tmp = line.ComputeLength();
var point = new Point(3, 3); 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)); //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); // 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}"); // 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); // 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}"); // 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); 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}"); // 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));
var triangle = new Triangle(new Point(3,3), new Point(7,3), new Point(7,6)); Console.WriteLine(
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}"); $"\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); 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(); var composite = new CompositeShape();
composite.Add(new Rectangle(new Point(500, 500), new Size(100, 100) )); composite.Add(new Rectangle(new Point(500, 500), new Size(100, 100)));
// composite.Add(triangle); // composite.Add(triangle);
var composite2 = new CompositeShape(); var composite2 = new CompositeShape();
composite2.Add(composite); 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); composite.Draw(save);
composite2.RemoveShape(composite); composite2.RemoveShape(composite);
composite.RemoveAllShapes(); 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); 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<Triangle>(fs); var triangle2 = new ShapeFactory().GetShapeFromFile<Triangle>(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);
} }
} }
} }

View File

@@ -2,7 +2,7 @@ using NUnit.Framework;
namespace Tests namespace Tests
{ {
public class Tests public class CircleTests
{ {
[SetUp] [SetUp]
public void Setup() public void Setup()

View File

@@ -0,0 +1,34 @@
using NUnit.Framework;
using Moq;
using Shapes;
namespace Tests
{
public class RectangleTests
{
private Mock<Point> point1;
[SetUp]
public void Setup()
{
point1 = new Mock<Point>();
}
[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);
}
}
}
}

View File

@@ -0,0 +1,18 @@
using NUnit.Framework;
namespace Tests
{
public class ShapeFactoryTests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
Assert.Pass();
}
}
}

View File

@@ -0,0 +1,18 @@
using NUnit.Framework;
namespace Tests
{
public class TriangleTests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
Assert.Pass();
}
}
}

View File

@@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="nunit" Version="3.11.0" /> <PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" /> <PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />