This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTriangleDecorator.cpp
73 lines (59 loc) · 1.75 KB
/
CTriangleDecorator.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "CTriangleDecorator.h"
CTriangleDecorator::CTriangleDecorator(const std::shared_ptr<sf::ConvexShape> shape)
:m_shape(shape)
{
}
float CTriangleDecorator::side1() const
{
return this->GetDistance(m_shape->getPoint(0), m_shape->getPoint(1));
}
float CTriangleDecorator::side2() const
{
return this->GetDistance(m_shape->getPoint(0), m_shape->getPoint(2));
}
float CTriangleDecorator::side3() const
{
return this->GetDistance(m_shape->getPoint(1), m_shape->getPoint(2));
}
std::shared_ptr<sf::ConvexShape> CTriangleDecorator::CreateConvexShape(sf::Vector2f p1, sf::Vector2f p2, sf::Vector2f p3, sf::Color color)
{
const auto a = GetDistance(p1, p2);
const auto b = GetDistance(p1, p3);
const auto c = GetDistance(p2, p3);
if (!(a < b + c && b < a + c && c < a + b))
{
throw std::logic_error("triangle: the sum of any two sides of a triangle must be greater than the third side");
}
sf::ConvexShape shape;
shape.setPointCount(3);
shape.setPoint(0, p1);
shape.setPoint(1, p2);
shape.setPoint(2, p3);
shape.setFillColor(color);
return std::make_shared<sf::ConvexShape>(shape);
}
std::shared_ptr<sf::Shape> CTriangleDecorator::GetShapeInstance() const
{
return m_shape;
}
sf::Color CTriangleDecorator::GetFillColor() const
{
return m_shape->getFillColor();
}
void CTriangleDecorator::Accept(IShapeVisitor& visitor) const
{
visitor.Visit(*this);
}
float CTriangleDecorator::GetDistance(sf::Vector2f p1, sf::Vector2f p2)
{
return std::sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
float CTriangleDecorator::GetPerimeter() const
{
return side1() + side2() + side3();
}
float CTriangleDecorator::GetSquare() const
{
const float d = this->GetPerimeter() / 2;
return std::sqrt(d * (d - side1()) * (d - side2()) * (d - side3()));
}