From 8d6ba5df72cc2c364f0a3cec575fd76c0754d3e8 Mon Sep 17 00:00:00 2001 From: bbod Date: Wed, 23 Oct 2019 19:06:40 -0600 Subject: [PATCH] more stuff --- .idea/.idea.Shapes/.idea/.name | 1 + .idea/.idea.Shapes/.idea/contentModel.xml | 2 +- Shapes/Circle.cs | 17 ++---- Shapes/Line.cs | 4 +- Shapes/Point.cs | 22 ++++++++ Shapes/Rectangle.cs | 39 +++++--------- Shapes/Shape.cs | 36 +++++++++++-- Shapes/Triangle.cs | 66 +++++++++++++++++++++-- Temp/Program.cs | 7 +++ 9 files changed, 143 insertions(+), 51 deletions(-) create mode 100644 .idea/.idea.Shapes/.idea/.name diff --git a/.idea/.idea.Shapes/.idea/.name b/.idea/.idea.Shapes/.idea/.name new file mode 100644 index 0000000..6fd8e14 --- /dev/null +++ b/.idea/.idea.Shapes/.idea/.name @@ -0,0 +1 @@ +Shapes \ No newline at end of file diff --git a/.idea/.idea.Shapes/.idea/contentModel.xml b/.idea/.idea.Shapes/.idea/contentModel.xml index c48181e..325f510 100644 --- a/.idea/.idea.Shapes/.idea/contentModel.xml +++ b/.idea/.idea.Shapes/.idea/contentModel.xml @@ -5,7 +5,7 @@ - + diff --git a/Shapes/Circle.cs b/Shapes/Circle.cs index ffa6c0d..48bf992 100644 --- a/Shapes/Circle.cs +++ b/Shapes/Circle.cs @@ -5,11 +5,9 @@ namespace Shapes { public class Circle : Shape { - public Point Center { get; private set; } + public override Point CenterPoint { get; } public double Radius { get; private set; } - - public override Color Fill { get; set; } - public override Color Color { get; set; } + public override double Height { get; } public override double Width { get; } @@ -24,10 +22,8 @@ namespace Shapes public Circle(double x, double y, double radius) { Validator.ValidatePositiveDouble(radius, "Invalid radius"); - Center = new Point(x, y); + CenterPoint = new Point(x, y); Radius = radius; - Color = Color.Black; - Fill = Color.White; Height = radius * 2; Width = radius * 2; } @@ -44,10 +40,8 @@ namespace Shapes Validator.ValidatePositiveDouble(radius, "Invalid radius"); if (center == null) throw new ShapeException("Invalid center point"); - Center = center; + CenterPoint = center; Radius = radius; - Color = Color.Black; - Fill = Color.White; Height = radius * 2; Width = radius * 2; } @@ -60,7 +54,7 @@ namespace Shapes */ public void Move(double deltaX, double deltaY) { - Center.Move(deltaX, deltaY); + CenterPoint.Move(deltaX, deltaY); } /** @@ -84,6 +78,5 @@ namespace Shapes { return Math.PI * Math.Pow(Radius, 2); } - } } \ No newline at end of file diff --git a/Shapes/Line.cs b/Shapes/Line.cs index 3ccd7d8..f0ad7d0 100644 --- a/Shapes/Line.cs +++ b/Shapes/Line.cs @@ -7,7 +7,6 @@ namespace Shapes { public Point Point1 { get; private set; } public Point Point2 { get; private set; } - public Color Color { get; set; } /** * Constructor based on x-y Locations @@ -19,7 +18,6 @@ namespace Shapes */ public Line(double x1, double y1, double x2, double y2) { - Color = Color.Black; Point1 = new Point(x1, y1); Point2 = new Point(x2, y2); } @@ -34,7 +32,6 @@ namespace Shapes { if (point1==null || point2==null) throw new ShapeException("Invalid point"); - Color = Color.Black; Point1 = point1; Point2 = point2; } @@ -68,5 +65,6 @@ 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 99d2b21..177c15c 100644 --- a/Shapes/Point.cs +++ b/Shapes/Point.cs @@ -62,5 +62,27 @@ namespace Shapes { 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, 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 5b1e3bf..c910d3b 100644 --- a/Shapes/Rectangle.cs +++ b/Shapes/Rectangle.cs @@ -6,10 +6,10 @@ namespace Shapes { public class Rectangle : Shape { - public override Color Fill { get; set; } - public override Color Color { get; set; } - public List Points { get; } - public Point CenterPoint { get; } + public override List Points { get; } + + public List Lines { get; } + public override Point CenterPoint { get; } public sealed override double Width { get; } public sealed override double Height { get; } @@ -17,10 +17,19 @@ namespace Shapes public Rectangle(Point point1, Point point2, Point point3, Point point4) { 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)); + Lines.Add(new Line(point4, point1)); + + Height = new Line(point1, point4).ComputeLength(); Width = new Line(point1, point2).ComputeLength(); CenterPoint = new Point(point1.X + (Width/2), point1.Y + (Height/2)); @@ -82,27 +91,7 @@ namespace Shapes return new Line(Points[0], Points[1]).ComputeLength(); } - public void Rotate(double degrees) - { - double radians = degrees * (Math.PI / 180); - double cosTheta = Math.Cos(radians); - double sinTheta = Math.Sin(radians); - foreach (var point in Points) - { - var oldPoint = point; - - - var tempX = oldPoint.X - CenterPoint.X; - var tempY = oldPoint.Y - CenterPoint.X; - - var rotatedX = tempX*cosTheta - tempY*sinTheta; - var rotatedY = tempX*sinTheta + tempY*cosTheta; - - point.X = rotatedX + CenterPoint.X; - point.Y = rotatedY + CenterPoint.Y; - - } - } + } diff --git a/Shapes/Shape.cs b/Shapes/Shape.cs index aa5de68..f181079 100644 --- a/Shapes/Shape.cs +++ b/Shapes/Shape.cs @@ -1,19 +1,45 @@ -using System.ComponentModel; -using System.Diagnostics.Tracing; +using System; +using System.Collections.Generic; using System.Drawing; -using System.Reflection.Metadata.Ecma335; namespace Shapes { public abstract class Shape { - public abstract Color Fill { get; set; } - public abstract Color Color { get; set; } + public abstract double Width { get; } public abstract double Height { get; } public abstract double ComputeArea(); + + public abstract Point CenterPoint { get; } + + public virtual List Points { get; } + public abstract void Scale(double scaleFactor); + + + public void Rotate(double degrees) + { + double radians = degrees * (Math.PI / 180); + double cosTheta = Math.Cos(radians); + double sinTheta = Math.Sin(radians); + foreach (var point in Points) + { + var oldPoint = point; + + + var tempX = oldPoint.X - CenterPoint.X; + var tempY = oldPoint.Y - CenterPoint.X; + + var rotatedX = tempX*cosTheta - tempY*sinTheta; + var rotatedY = tempX*sinTheta + tempY*cosTheta; + + point.X = rotatedX + CenterPoint.X; + point.Y = rotatedY + CenterPoint.Y; + + } + } } diff --git a/Shapes/Triangle.cs b/Shapes/Triangle.cs index 98e1086..254a56a 100644 --- a/Shapes/Triangle.cs +++ b/Shapes/Triangle.cs @@ -1,34 +1,90 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Drawing; +using System.Linq; namespace Shapes { public class Triangle : Shape { - public override Color Fill { get; set; } - public override Color Color { get; set; } - public override double Width { get; } + public sealed override List Points { get; } + public override double Height { get; } + public override double Width { get; } + + public sealed override Point CenterPoint { get; } + public List Lines { get; } + public Triangle(Point point1, Point point2, Point point3) { + 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; + + + 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) { + var point1 = new Point(x1, y1); + 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; + + CenterPoint = centerTriangle; } public override double ComputeArea() { - throw new NotImplementedException(); + + var a = Lines[0].ComputeLength(); + var b = Lines[1].ComputeLength(); + var c = Lines[2].ComputeLength(); + + var p = (a + b + c) / 2; + + return Math.Sqrt(p * (p - a) * (p - b) * (p - c)); } public override void Scale(double scaleFactor) { - throw new NotImplementedException(); + foreach (var point in Points) + { + point.X *= scaleFactor; + point.Y *= scaleFactor; + } } } } \ No newline at end of file diff --git a/Temp/Program.cs b/Temp/Program.cs index 29f17be..ed726bf 100644 --- a/Temp/Program.cs +++ b/Temp/Program.cs @@ -23,6 +23,13 @@ namespace Temp 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}"); + + + + var triangle = new Triangle(new Point(3,3), new Point(7,3), new Point(7,6)); + 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}"); + 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}"); } }