Did a lot of work just unit testing left

This commit is contained in:
2019-10-25 02:30:49 -06:00
parent 8d6ba5df72
commit 6901925c69
15 changed files with 508 additions and 74 deletions

View File

@@ -1,21 +1,34 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization;
namespace Shapes
{
public class Rectangle : Shape
[DataContract]
public class Rectangle : GeometricShape
{
public override List<Point> Points { get; }
[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; }
public override Point CenterPoint { get; }
public sealed override double Width { get; }
public sealed override double Height { 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<Point>();
Lines = new List<Line>();
@@ -38,6 +51,7 @@ namespace Shapes
public Rectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
{
Stroke = Color.Black;
Points = new List<Point>();
var point1 = new Point(x1, y1);
var point2 = new Point(x2, y2);
@@ -49,12 +63,13 @@ namespace Shapes
Points.Add(point4);
Height = new Line(point1, point2).ComputeLength();
Width = new Line(point1, point4).ComputeLength();
CenterPoint = new Point((point1.X + Width)/2, (point1.Y + Height)/2);
ComputeCenter();
Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {this.GetType()}");
}
public Rectangle(Point point, Size size)
{
Stroke = Color.Black;
Points = new List<Point>();
var point1 = point;
var point2 = new Point(point.X + size.Width, point.Y);
@@ -66,7 +81,7 @@ namespace Shapes
Points.Add(point4);
Height = new Line(point1, point2).ComputeLength();
Width = new Line(point1, point4).ComputeLength();
CenterPoint = new Point((point1.X + Width)/2, (point1.Y + Height)/2);
ComputeCenter();
Validator.ValidateRectangle(Points, $"Attempted to create an invalid shape {this.GetType()}");
}
@@ -77,7 +92,42 @@ namespace Shapes
public override void Scale(double scaleFactor)
{
throw new System.NotImplementedException();
foreach (var point in Points)
{
point.X *= scaleFactor;
point.Y *= scaleFactor;
}
foreach (var line in Lines)
{
line.Point1.X *= scaleFactor;
line.Point1.Y *= scaleFactor;
line.Point2.X *= scaleFactor;
line.Point2.Y *= scaleFactor;
}
}
public override void Save(Stream stream)
{
var fileWriter = new FileIO();
fileWriter.SaveShape(stream, this);
}
public override void Draw(Stream stream)
{
var tmp = new Bitmap(1000, 1000);
Pen 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,
(float) Points[i - 1].Y,
(float) Points[i].X,
(float) Points[i].Y);
}
tmp.Save(stream, ImageFormat.Jpeg);
}
public double CalculateWidth()
@@ -91,6 +141,10 @@ namespace Shapes
return new Line(Points[0], Points[1]).ComputeLength();
}
internal override void ComputeCenter()
{
CenterPoint = new Point((Points[0].X + Width)/2, (Points[0].Y + Height)/2);
}