11#ifndef RAYPOLYGON_HPP_
12#define RAYPOLYGON_HPP_
29 _color = raylib::Color{255, 0, 0, 255};
32 for (
auto point : points) {
33 _points.push_back(
Vector2f{point.x, point.y});
46 return {_position.x, _position.y};
50 _position = raylib::Vector2{float(position.
x), float(position.
y)};
54 return Color{_color.r, _color.g, _color.b, _color.a};
58 _color = raylib::Color{color.
r, color.
g, color.
b, color.
a};
80 const double cross = BA.
cross(BC);
82 return (std::abs(cross) <
epsilond) ? false : (cross < 0);
85 float polygon_area(
const std::vector<Vector2f>& pts) {
87 for (
size_t i = 0; i < pts.size(); ++i) {
89 const Vector2f& b = pts[(i + 1) % pts.size()];
90 area += (a.
x * b.
y - b.
x * a.
y);
96 std::vector<Vector2f> tmp = _points;
98 if (polygon_area(tmp) < 0)
99 std::reverse(tmp.begin(), tmp.end());
101 while (tmp.size() >= 3) {
102 bool earFound =
false;
104 for (
size_t i = 0; i < tmp.size(); 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};
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;
121 _triangles.push_back(Triangle<double>{A, B, C});
122 size_t earIndex = (i + 1) % tmp.size();
123 tmp.erase(tmp.begin() + earIndex);
135 raylib::Color _color;
136 raylib::Vector2 _position;
138 std::vector<Vector2f> _points;
139 std::vector<Triangle<double>> _triangles;
void setColor(Color color) override
Set the Color object.
Definition RayPolygon.hpp:57
void setPosition(Vector2f position) override
Set the Position object.
Definition RayPolygon.hpp:49
std::vector< Vector2f > getPoints() const override
Get the Points object.
Definition RayPolygon.hpp:61
bool isReady() const override
Get the Position object.
Definition RayPolygon.hpp:41
RayPolygon(std::vector< Vector2f > points)
Definition RayPolygon.hpp:28
friend class RayWindow
Definition RayPolygon.hpp:65
Color getColor() const override
Get the Color object.
Definition RayPolygon.hpp:53
Vector2f getPosition() const override
Get the Position object.
Definition RayPolygon.hpp:45
Polygon interface.
Definition IPolygon.hpp:34
bool is_convex(Vector2f A, Vector2f B, Vector2f C, bool clockwise=true)
check if the angle is convex or concave
Definition geometry.hpp:37
constexpr double epsilond
Definition Constants.hpp:35
Vector2< double > Vector2f
Definition Vector2.hpp:112
std::uint8_t g
Definition Color.hpp:18
std::uint8_t a
Definition Color.hpp:20
std::uint8_t b
Definition Color.hpp:19
std::uint8_t r
Definition Color.hpp:17
bool isInside(Vector2< T > P)
Definition Shape.hpp:107
T y
Definition Vector2.hpp:21
T x
Definition Vector2.hpp:21
T cross(const Vector2 &other) const
Definition Vector2.hpp:65