Smolgui
Immediate gui library based on SFML
Loading...
Searching...
No Matches
Shapes.tpp
Go to the documentation of this file.
1namespace sgui
2{
3
4/// Line
5/////////////////////////////////////////////////
6template <typename Type>
7Line<Type>::Line (const sf::Vector2<Type>& begin, const sf::Vector2<Type>& end)
8 : tail (begin), head (end)
9{}
10
11/////////////////////////////////////////////////
12template <typename Type>
13sf::Vector2<Type> Line<Type>::direction () const
14{
15 return head - tail;
16}
17
18/// Circle
19/////////////////////////////////////////////////
20template <typename Type>
21Circle<Type>::Circle (const sf::Vector2<Type>& c, Type r)
22 : radius (r), center (c)
23{}
24
25/// Utility
26/////////////////////////////////////////////////
27template<typename Type>
28constexpr sf::Vector2f centroid (const std::vector<sf::Vector2 <Type>>& vertices)
29{
30 sf::Vector2f centroid;
31 float signedArea = 0.f;
32 // compute centroid
33 for (uint32_t i = 0; i < vertices.size (); i++) {
34 auto current = sf::Vector2f (vertices[i]);
35 auto next = sf::Vector2f (vertices[(i + 1) % vertices.size()]);
36 auto a = (current.x * next.y) - (current.y * next.x);
37 signedArea += a;
38 centroid += (current + next) * a;
39 }
40 // add the prefactor
41 centroid /= (3.f * signedArea);
42 return centroid;
43}
44
45} // namespace sgui