Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
wze::polygon Class Referencefinal
Inheritance diagram for wze::polygon:
wze::component

Public Member Functions

std::vector< std::pair< float, float > > const & shape () const
 
float shape_radius () const
 
std::vector< std::pair< float, float > > const & points () const
 
float points_radius () const
 
float x () const
 
void set_x (float x) final
 
float y () const
 
void set_y (float y) final
 
float angle () const
 
void set_angle (float angle) final
 
float scale () const
 
void set_scale (float scale)
 
std::array< float, 4 > const & transformation_matrix () const
 
float x_offset () const final
 
void set_x_offset (float x_offset)
 
float y_offset () const final
 
void set_y_offset (float y_offset)
 
float angle_offset () const final
 
void set_angle_offset (float angle_offset)
 
bool attach_x () const final
 
void set_attach_x (bool attach_x)
 
bool attach_y () const final
 
void set_attach_y (bool attach_y)
 
bool attach_angle () const final
 
void set_attach_angle (bool attach_angle)
 
bool x_angle_lock () const final
 
void set_x_angle_lock (bool x_angle_lock)
 
bool y_angle_lock () const final
 
void set_y_angle_lock (bool y_angle_lock)
 
 polygon (std::vector< std::pair< float, float > > const &shape={{0, 0}, {0, 0}, {0, 0}}, float x=0, float y=0, float angle=0, float scale=1, float x_offset=0, float y_offset=0, float angle_offset=0, bool attach_x=true, bool attach_y=true, bool attach_angle=true, bool x_angle_lock=true, bool y_angle_lock=true)
 
bool inside (float x, float y) const
 
template<typename T >
std::enable_if_t< std::is_same_v< T, bool >||std::is_same_v< T, float >, T > overlap (polygon const &other) const
 
- Public Member Functions inherited from wze::component

Detailed Description

Definition at line 35 of file polygon.hpp.

Constructor & Destructor Documentation

◆ polygon()

wze::polygon::polygon ( std::vector< std::pair< float, float > > const & shape = {{0, 0}, {0, 0}, {0, 0}},
float x = 0,
float y = 0,
float angle = 0,
float scale = 1,
float x_offset = 0,
float y_offset = 0,
float angle_offset = 0,
bool attach_x = true,
bool attach_y = true,
bool attach_angle = true,
bool x_angle_lock = true,
bool y_angle_lock = true )
explicit

Definition at line 209 of file polygon.cpp.

213 {
214 _shape = shape;
215 _shape_radius = circumradius();
216 _points.resize(this->shape().size());
217 set_x(x);
218 set_y(y);
219 set_angle(angle);
220 set_scale(scale);
221 set_x_offset(x_offset);
222 set_y_offset(y_offset);
223 set_angle_offset(angle_offset);
224 set_attach_x(attach_x);
225 set_attach_y(attach_y);
226 set_attach_angle(attach_angle);
227 set_x_angle_lock(x_angle_lock);
228 set_y_angle_lock(y_angle_lock);
229}

Member Function Documentation

◆ shape()

std::vector< std::pair< float, float > > const & wze::polygon::shape ( ) const
nodiscard

Definition at line 82 of file polygon.cpp.

82 {
83 return _shape;
84}

◆ shape_radius()

float wze::polygon::shape_radius ( ) const
nodiscard

Definition at line 86 of file polygon.cpp.

86 {
87 return _shape_radius;
88}

◆ points()

std::vector< std::pair< float, float > > const & wze::polygon::points ( ) const
nodiscard

Definition at line 90 of file polygon.cpp.

90 {
91 return _points;
92}

◆ points_radius()

float wze::polygon::points_radius ( ) const
nodiscard

Definition at line 94 of file polygon.cpp.

94 {
95 return _points_radius;
96}

◆ x()

float wze::polygon::x ( ) const
nodiscard

Definition at line 98 of file polygon.cpp.

98 {
99 return _x;
100}

◆ set_x()

void wze::polygon::set_x ( float x)
finalvirtual

Implements wze::component.

Definition at line 102 of file polygon.cpp.

102 {
103 _x = x;
104 update_x();
105}

◆ y()

float wze::polygon::y ( ) const
nodiscard

Definition at line 107 of file polygon.cpp.

107 {
108 return _y;
109}

◆ set_y()

void wze::polygon::set_y ( float y)
finalvirtual

Implements wze::component.

Definition at line 111 of file polygon.cpp.

111 {
112 _y = y;
113 update_y();
114}

◆ angle()

float wze::polygon::angle ( ) const
nodiscard

Definition at line 116 of file polygon.cpp.

116 {
117 return _angle;
118}

◆ set_angle()

void wze::polygon::set_angle ( float angle)
finalvirtual

Implements wze::component.

Definition at line 120 of file polygon.cpp.

120 {
121 _angle = angle;
122 _transformation_matrix =
123 math::transformation_matrix(this->angle(), scale());
124 update_x();
125 update_y();
126}
static std::array< float, 4 > transformation_matrix(float angle, float scale)
Creates a transformation matrix.
Definition math.cpp:57

◆ scale()

float wze::polygon::scale ( ) const
nodiscard

Definition at line 128 of file polygon.cpp.

128 {
129 return _scale;
130}

◆ set_scale()

void wze::polygon::set_scale ( float scale)

Definition at line 132 of file polygon.cpp.

132 {
133 _scale = scale;
134 _points_radius = shape_radius() * this->scale();
135 _transformation_matrix =
136 math::transformation_matrix(angle(), this->scale());
137 update_x();
138 update_y();
139}

◆ transformation_matrix()

std::array< float, 4 > const & wze::polygon::transformation_matrix ( ) const
nodiscard

Definition at line 141 of file polygon.cpp.

141 {
142 return _transformation_matrix;
143}

◆ x_offset()

float wze::polygon::x_offset ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 145 of file polygon.cpp.

145 {
146 return _x_offset;
147}

◆ set_x_offset()

void wze::polygon::set_x_offset ( float x_offset)

Definition at line 149 of file polygon.cpp.

149 {
150 _x_offset = x_offset;
151}

◆ y_offset()

float wze::polygon::y_offset ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 153 of file polygon.cpp.

153 {
154 return _y_offset;
155}

◆ set_y_offset()

void wze::polygon::set_y_offset ( float y_offset)

Definition at line 157 of file polygon.cpp.

157 {
158 _y_offset = y_offset;
159}

◆ angle_offset()

float wze::polygon::angle_offset ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 161 of file polygon.cpp.

161 {
162 return _angle_offset;
163}

◆ set_angle_offset()

void wze::polygon::set_angle_offset ( float angle_offset)

Definition at line 165 of file polygon.cpp.

165 {
166 _angle_offset = angle_offset;
167}

◆ attach_x()

bool wze::polygon::attach_x ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 169 of file polygon.cpp.

169 {
170 return _attach_x;
171}

◆ set_attach_x()

void wze::polygon::set_attach_x ( bool attach_x)

Definition at line 173 of file polygon.cpp.

173 {
174 _attach_x = attach_x;
175}

◆ attach_y()

bool wze::polygon::attach_y ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 177 of file polygon.cpp.

177 {
178 return _attach_y;
179}

◆ set_attach_y()

void wze::polygon::set_attach_y ( bool attach_y)

Definition at line 181 of file polygon.cpp.

181 {
182 _attach_y = attach_y;
183}

◆ attach_angle()

bool wze::polygon::attach_angle ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 185 of file polygon.cpp.

185 {
186 return _attach_angle;
187}

◆ set_attach_angle()

void wze::polygon::set_attach_angle ( bool attach_angle)

Definition at line 189 of file polygon.cpp.

189 {
190 _attach_angle = attach_angle;
191}

◆ x_angle_lock()

bool wze::polygon::x_angle_lock ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 193 of file polygon.cpp.

193 {
194 return _x_angle_lock;
195}

◆ set_x_angle_lock()

void wze::polygon::set_x_angle_lock ( bool x_angle_lock)

Definition at line 197 of file polygon.cpp.

197 {
198 _x_angle_lock = x_angle_lock;
199}

◆ y_angle_lock()

bool wze::polygon::y_angle_lock ( ) const
nodiscardfinalvirtual

Implements wze::component.

Definition at line 201 of file polygon.cpp.

201 {
202 return _y_angle_lock;
203}

◆ set_y_angle_lock()

void wze::polygon::set_y_angle_lock ( bool y_angle_lock)

Definition at line 205 of file polygon.cpp.

205 {
206 _y_angle_lock = y_angle_lock;
207}

◆ inside()

bool wze::polygon::inside ( float x,
float y ) const
nodiscard

Definition at line 231 of file polygon.cpp.

231 {
232 std::vector<std::pair<float, float>>::const_iterator point1;
233 std::vector<std::pair<float, float>>::const_iterator point2;
234 float determinant;
235 float temporary;
236
237 if (points_radius() < math::length(x - this->x(), y - this->y())) {
238 return false;
239 }
240
241 determinant = 0;
242 for (point1 = points().begin(), point2 = points().begin() + 1;
243 point1 != points().end(); ++point1, ++point2) {
244 if (point2 == points().end()) {
245 point2 = points().begin();
246 }
247
248 temporary = (point2->first - point1->first) * (y - point1->second) -
249 (x - point1->first) * (point2->second - point1->second);
250
251 if ((0 < determinant && temporary < 0) ||
252 (determinant < 0 && 0 < temporary)) {
253 return false;
254 }
255
256 determinant = temporary;
257 }
258
259 return true;
260}
static float length(float x, float y)
Returns the length of a vector.
Definition math.cpp:33

◆ overlap()

template<typename T >
std::enable_if_t< std::is_same_v< T, bool >||std::is_same_v< T, float >, T > wze::polygon::overlap ( polygon const & other) const
inlinenodiscard

Definition at line 369 of file polygon.hpp.

369 {
370 polygon const* polygon1;
371 polygon const* polygon2;
372 T state;
373 size_t i;
374 std::vector<std::pair<float, float>>::const_iterator point1;
375 std::vector<std::pair<float, float>>::const_iterator point2;
376 std::pair<float, float> normal;
377 std::pair<float, float> projection1;
378 std::pair<float, float> projection2;
379
380 if (points_radius() + other.points_radius() <
381 math::length(other.x() - x(), other.y() - y())) {
382 return std::is_same_v<T, bool> ? false : 0;
383 }
384
385 polygon1 = this;
386 polygon2 = &other;
387 state =
388 std::is_same_v<T, bool> ? true : std::numeric_limits<float>::max();
389
390 for (i = 0; i != 2; ++i) {
391 for (point1 = polygon1->points().begin(),
392 point2 = polygon1->points().begin() + 1;
393 point1 != polygon1->points().end(); ++point1, ++point2) {
394 if (point2 == polygon1->points().end()) {
395 point2 = polygon1->points().begin();
396 }
397
398 normal = math::normal(point2->first - point1->first,
399 point2->second - point1->second);
400 if constexpr (std::is_same_v<T, float>) {
401 normal = std::apply(math::normalize, normal);
402 }
403 projection1 = polygon1->project(normal);
404 projection2 = polygon2->project(normal);
405
406 if (projection2.second < projection1.first ||
407 projection1.second < projection2.first) {
408 return std::is_same_v<T, bool> ? false : 0;
409 }
410
411 if constexpr (std::is_same_v<T, float>) {
412 state = std::min(
413 std::min(projection1.second, projection2.second) -
414 std::max(projection1.first, projection2.first),
415 state);
416 }
417 }
418
419 polygon1 = &other;
420 polygon2 = this;
421 }
422
423 return state;
424 }
static constexpr std::pair< float, float > normal(float x, float y)
Returns the normal vector of a vector.
Definition math.hpp:86
static std::pair< float, float > normalize(float x, float y)
Normalizes a vector.
Definition math.cpp:41

The documentation for this class was generated from the following files: