45 lines
1004 B
C#
45 lines
1004 B
C#
|
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);
|
||
|
}
|
||
|
}
|