blob: 54e8a8524b95bca17d65621b6207bbea791ebdc3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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);
}
}
|