Archived
1
0
This repository has been archived on 2023-12-01. You can view files and clone it, but cannot push or open issues or pull requests.
Mindbox/MindBox.Lib/Triangle.cs

45 lines
1004 B
C#
Raw Normal View History

2023-11-13 22:40:23 +03:00
namespace MindBox.Lib;
public class Triangle : FlatShape
{
private readonly double[] _sides;
public Triangle(double a, double b, double c)
{
_sides = new[] { a, b, c };
if (_sides.Any(side => side <= 0))
{
throw new ArgumentException($"Side(s) cannot be non-positive: {string.Join(" ;", _sides.Where(side => side <= 0))}");
}
if (a >= b + c || b >= c + a || c >= a + b)
{
throw new ArgumentException($"Sides lengths are not valid: {a}, {b}, {c}");
}
}
public override double GetArea()
{
if (_area != null)
{
return _area.Value;
}
else
{
double semiPerimeter = _sides.Sum() / 2;
_area = Math.Sqrt(semiPerimeter *
(semiPerimeter - _sides[0]) *
(semiPerimeter - _sides[1]) *
(semiPerimeter - _sides[2]));
return _area.Value;
}
}
public bool IsRight()
{
var sorted = _sides.OrderByDescending(x => x);
double hypotenuse = sorted.First();
var catheti = sorted.Skip(1);
return hypotenuse * hypotenuse == catheti.Sum(x => x * x);
}
}