From 80b5cc51094661fc7c64905cb3140bffedfd2c31 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Mon, 18 Mar 2024 22:09:29 +0100 Subject: [PATCH] Add more shapes --- src/CuteUtils/FluentMath/Shapes/Circle.cs | 12 +++++++ src/CuteUtils/FluentMath/Shapes/Ellipse.cs | 12 +++++++ src/CuteUtils/FluentMath/Shapes/Rectangle.cs | 27 ++++++++------- src/CuteUtils/FluentMath/Shapes/Triangle.cs | 35 ++++++++++++++++++++ 4 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 src/CuteUtils/FluentMath/Shapes/Circle.cs create mode 100644 src/CuteUtils/FluentMath/Shapes/Ellipse.cs create mode 100644 src/CuteUtils/FluentMath/Shapes/Triangle.cs diff --git a/src/CuteUtils/FluentMath/Shapes/Circle.cs b/src/CuteUtils/FluentMath/Shapes/Circle.cs new file mode 100644 index 0000000..bb9d3c3 --- /dev/null +++ b/src/CuteUtils/FluentMath/Shapes/Circle.cs @@ -0,0 +1,12 @@ +namespace CuteUtils.FluentMath.Shapes; + +public class Circle(double radius) +{ + public double Radius { get; set; } = radius; + + public double Diameter => Radius * 2; + + public double Circumference => 2 * Math.PI * Radius; + + public double Area => Math.PI * Math.Pow(Radius, 2); +} \ No newline at end of file diff --git a/src/CuteUtils/FluentMath/Shapes/Ellipse.cs b/src/CuteUtils/FluentMath/Shapes/Ellipse.cs new file mode 100644 index 0000000..e4963ab --- /dev/null +++ b/src/CuteUtils/FluentMath/Shapes/Ellipse.cs @@ -0,0 +1,12 @@ +namespace CuteUtils.FluentMath.Shapes; + +public class Ellipse(double majorAxis, double minorAxis) +{ + public double MajorAxis { get; set; } = majorAxis; + public double MinorAxis { get; set; } = minorAxis; + + public double Area => Math.PI * MajorAxis * MinorAxis; + public double Circumference => Math.PI * ((3 * (MajorAxis + MinorAxis)) - Math.Sqrt(((3 * MajorAxis) + MinorAxis) * (MajorAxis + (3 * MinorAxis)))); + + public bool IsCircle => MajorAxis == MinorAxis; +} \ No newline at end of file diff --git a/src/CuteUtils/FluentMath/Shapes/Rectangle.cs b/src/CuteUtils/FluentMath/Shapes/Rectangle.cs index 424424b..d67a204 100644 --- a/src/CuteUtils/FluentMath/Shapes/Rectangle.cs +++ b/src/CuteUtils/FluentMath/Shapes/Rectangle.cs @@ -3,17 +3,22 @@ /// /// Represents a rectangle /// -public class Rectangle +/// +/// Creates a new rectangle instance +/// +/// The length of the rectangle +/// The width of the rectangle +public class Rectangle(double length, double width) { /// /// The length of the /// - public double Length { get; set; } + public double Length { get; set; } = length; /// /// The width of the /// - public double Width { get; set; } + public double Width { get; set; } = width; /// /// The diagonal of the @@ -28,16 +33,10 @@ public class Rectangle /// /// The perimeter of the /// - public double Perimeter => Length * 2 + Width * 2; + public double Perimeter => (Length * 2) + (Width * 2); - /// - /// Creates a new rectangle instance - /// - /// The length of the rectangle - /// The width of the rectangle - public Rectangle(double length, double width) - { - Length = length; - Width = width; - } + public bool IsSquare => Length == Width; + public bool IsRectangle => !IsSquare; + + public bool IsGolden => Length / Width == 1.61803398875; } \ No newline at end of file diff --git a/src/CuteUtils/FluentMath/Shapes/Triangle.cs b/src/CuteUtils/FluentMath/Shapes/Triangle.cs new file mode 100644 index 0000000..4a25927 --- /dev/null +++ b/src/CuteUtils/FluentMath/Shapes/Triangle.cs @@ -0,0 +1,35 @@ +namespace CuteUtils.FluentMath.Shapes; + +public class Triangle(double sideA, double sideB, double sideC) +{ + public double SideA { get; set; } = sideA; + public double SideB { get; set; } = sideB; + public double SideC { get; set; } = sideC; + + public double Area + { + get + { + double s = (SideA + SideB + SideC) / 2; + return Math.Sqrt(s * (s - SideA) * (s - SideB) * (s - SideC)); + } + } + + public double Perimeter => SideA + SideB + SideC; + + public bool IsRightAngled + { + get + { + double[] sides = [SideA, SideB, SideC]; + Array.Sort(sides); + return Math.Pow(sides[0], 2) + Math.Pow(sides[1], 2) == Math.Pow(sides[2], 2); + } + } + + public bool IsEquilateral => SideA == SideB && SideB == SideC; + + public bool IsIsosceles => SideA == SideB || SideB == SideC || SideA == SideC; + + public bool IsScalene => !IsEquilateral && !IsIsosceles; +} \ No newline at end of file