raylib_impl 0.1.0
Vendor raylib 5.5 : IGraphic2Module / IGraphic3Module / IAudioModule
Loading...
Searching...
No Matches
RayPolygon.hpp
Go to the documentation of this file.
1
11#ifndef RAYPOLYGON_HPP_
12#define RAYPOLYGON_HPP_
13
14#include <vector>
15
16//Raylib (without conflict)
17#include "RaylibAliases.hpp"
18
19// Interface
20#include "IPolygon.hpp"
21
22// system : Triangle, Vector2f, Epsilon
23#include "Shape.hpp"
24
25class RayPolygon : public graphic::IPolygon {
26
27public:
28 RayPolygon(std::vector<Vector2f> points) {
29 _color = raylib::Color{255, 0, 0, 255};
30 _position = {0, 0};
31
32 for (auto point : points) {
33 _points.push_back(Vector2f{point.x, point.y});
34 }
35
36 triangulate();
37 }
38
39 ~RayPolygon() = default;
40
41 bool isReady() const override {
42 return true;
43 }
44
45 Vector2f getPosition() const override {
46 return {_position.x, _position.y};
47 }
48
49 void setPosition(Vector2f position) override {
50 _position = raylib::Vector2{float(position.x), float(position.y)};
51 }
52
53 Color getColor() const override {
54 return Color{_color.r, _color.g, _color.b, _color.a};
55 }
56
57 void setColor(Color color) override {
58 _color = raylib::Color{color.r, color.g, color.b, color.a};
59 }
60
61 std::vector<Vector2f> getPoints() const override {
62 return _points;
63 }
64
65 friend class RayWindow;
66
67private:
77 static bool is_convex(Vector2f A, Vector2f B, Vector2f C) {
78 const Vector2f BA = {A.x - B.x, A.y - B.y};
79 const Vector2f BC = {C.x - B.x, C.y - B.y};
80 const double cross = BA.cross(BC);
81
82 return (std::abs(cross) < epsilond) ? false : (cross < 0);
83 }
84
85 float polygon_area(const std::vector<Vector2f>& pts) {
86 float area = 0;
87 for (size_t i = 0; i < pts.size(); ++i) {
88 const Vector2f& a = pts[i];
89 const Vector2f& b = pts[(i + 1) % pts.size()];
90 area += (a.x * b.y - b.x * a.y);
91 }
92 return area * 0.5f;
93 }
94
95 void triangulate() {
96 std::vector<Vector2f> tmp = _points;
97
98 if (polygon_area(tmp) < 0)
99 std::reverse(tmp.begin(), tmp.end());
100
101 while (tmp.size() >= 3) {
102 bool earFound = false;
103
104 for (size_t i = 0; i < tmp.size(); i++) {
105 Vector2f A = tmp[i];
106 Vector2f B = tmp[(i + 1) % tmp.size()];
107 Vector2f C = tmp[(i + 2) % tmp.size()];
108 Triangle<double> t = Triangle<double>{A, B, C};
109
110 if (is_convex(A, B, C)) {
111 bool isEar = true;
112 for (size_t j = 0; j < tmp.size(); j++) {
113 if (j == i || j == (i + 1) % tmp.size() || j == (i + 2) % tmp.size()) continue;
114 if (t.isInside(tmp[j])) {
115 isEar = false;
116 break;
117 }
118 }
119
120 if (isEar) {
121 _triangles.push_back(Triangle<double>{A, B, C});
122 size_t earIndex = (i + 1) % tmp.size();
123 tmp.erase(tmp.begin() + earIndex);
124 earFound = true;
125 break;
126 }
127 }
128 }
129
130 if (!earFound)
131 break;
132 }
133 }
134
135 raylib::Color _color;
136 raylib::Vector2 _position;
137
138 std::vector<Vector2f> _points;
139 std::vector<Triangle<double>> _triangles;
140};
141
144#endif /* !RAYPOLYGON_HPP_ */
Definition RayPolygon.hpp:25
void setColor(Color color) override
Definition RayPolygon.hpp:57
void setPosition(Vector2f position) override
Definition RayPolygon.hpp:49
std::vector< Vector2f > getPoints() const override
Definition RayPolygon.hpp:61
bool isReady() const override
Definition RayPolygon.hpp:41
~RayPolygon()=default
RayPolygon(std::vector< Vector2f > points)
Definition RayPolygon.hpp:28
Color getColor() const override
Definition RayPolygon.hpp:53
Vector2f getPosition() const override
Definition RayPolygon.hpp:45
Raylib Window class - a single concrete class implementing IWindow3 fully (which already IS-A IWindow...
Definition RayWindow.hpp:37