Smolgui
Immediate gui library based on SFML
Loading...
Searching...
No Matches
Polygon.tpp
Go to the documentation of this file.
1namespace sgui
2{
3/////////////////////////////////////////////////
4template <size_t N>
5Polygon<N> makeRegularPolygon (const sf::Vector2f& center, const float polySize)
6{
7 auto poly = Polygon<N> ();
8 constexpr auto deltaAngle = 360.f / N;
9 for (uint32_t i = 0; i < N; i++) {
10 float angle = PiRad * (deltaAngle * i);
11 poly.points[i] = center + polySize * sf::Vector2f (std::cos (angle), std::sin (angle));
12 }
13 return poly;
14}
15
16/////////////////////////////////////////////////
17template <size_t N>
18constexpr sf::Vector2f centroid (const Polygon<N>& polygon)
19{
20 auto points = std::vector <sf::Vector2f> ();
21 for (const auto& point : polygon.points) {
22 points.push_back (point);
23 }
24 return centroid (points);
25}
26
27/////////////////////////////////////////////////
28template <size_t N>
29constexpr sf::FloatRect boundingBox (const Polygon<N>& polygon)
30{
31 auto minPos = polygon.points [0];
32 auto maxPos = minPos;
33 // get extremal position of polygon edges
34 for (const auto& point : polygon.points ()) {
35 minPos.x = std::min (point.x, minPos.x);
36 minPos.y = std::min (point.y, minPos.y);
37 maxPos.x = std::max (point.x, maxPos.x);
38 maxPos.y = std::max (point.y, maxPos.y);
39 }
40 // compute box size and return bounding box
41 const auto size = maxPos - minPos;
42 return sf::FloatRect (minPos, size);
43}
44
45} // namespace sgui