11#ifndef SDLPOLYGON_HPP_
12#define SDLPOLYGON_HPP_
38 SdlPolygon(std::vector<Vector2f> points) : _points(points) {
45 bool isReady()
const override {
return !_vertices.empty(); }
54 for (SDL_Vertex &vertex : _vertices)
55 vertex.color = toFColor(color);
58 std::vector<Vector2f>
getPoints()
const override {
return _points; }
72 const double cross = BA.
cross(BC);
74 return (std::abs(cross) <
epsilond) ? false : (cross < 0);
77 static float polygon_area(
const std::vector<Vector2f> &pts) {
80 for (
size_t i = 0; i < pts.size(); ++i) {
82 const Vector2f &b = pts[(i + 1) % pts.size()];
84 area += (a.
x * b.
y - b.
x * a.
y);
90 std::vector<Vector2f> tmp = _points;
92 if (polygon_area(tmp) < 0)
93 std::reverse(tmp.begin(), tmp.end());
95 while (tmp.size() >= 3) {
96 bool earFound =
false;
98 for (
size_t i = 0; i < tmp.size(); i++) {
100 Vector2f B = tmp[(i + 1) % tmp.size()];
101 Vector2f C = tmp[(i + 2) % tmp.size()];
102 Triangle<double> t = Triangle<double>{A, B, C};
107 for (
size_t j = 0; j < tmp.size(); j++) {
108 if (j == i || j == (i + 1) % tmp.size() || j == (i + 2) % tmp.size())
continue;
116 _triangles.push_back(Triangle<double>{A, B, C});
117 tmp.erase(tmp.begin() + (i + 1) % tmp.size());
139 static SDL_FColor toFColor(Color color) {
140 return SDL_FColor{color.
r / 255.f, color.
g / 255.f, color.
b / 255.f, color.
a / 255.f};
143 void buildVertices() {
144 const SDL_FColor color = toFColor(_color);
146 _vertices.reserve(_triangles.size() * 3);
147 for (
const Triangle<double> &t : _triangles)
149 _vertices.push_back(SDL_Vertex{
150 SDL_FPoint{static_cast<float>(point.x), static_cast<float>(point.y)},
152 SDL_FPoint{0.f, 0.f}});
153 _screen.resize(_vertices.size());
156 Color _color{255, 0, 0, 255};
159 std::vector<Vector2f> _points;
160 std::vector<Triangle<double>> _triangles;
162 std::vector<SDL_Vertex> _vertices;
163 std::vector<SDL_Vertex> _screen;
Un contour quelconque, decoupe en triangles une fois pour toutes.
Definition SdlPolygon.hpp:35
bool isReady() const override
Get the Position object.
Definition SdlPolygon.hpp:45
Color getColor() const override
Get the Color object.
Definition SdlPolygon.hpp:50
SdlPolygon(std::vector< Vector2f > points)
Definition SdlPolygon.hpp:38
void setColor(Color color) override
Set the Color object.
Definition SdlPolygon.hpp:52
void setPosition(Vector2f position) override
Set the Position object.
Definition SdlPolygon.hpp:48
std::vector< Vector2f > getPoints() const override
Get the Points object.
Definition SdlPolygon.hpp:58
Vector2f getPosition() const override
Get the Position object.
Definition SdlPolygon.hpp:47
Une fenetre SDL et son renderer, implemente IWindow2.
Definition SdlWindow.hpp:50
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
Vector2< T > p3
Definition Shape.hpp:84
bool isInside(Vector2< T > P)
Definition Shape.hpp:107
Vector2< T > p2
Definition Shape.hpp:84
Vector2< T > p1
Definition Shape.hpp:84
T y
Definition Vector2.hpp:21
T x
Definition Vector2.hpp:21
T cross(const Vector2 &other) const
Definition Vector2.hpp:65