Recti 1.2.4
Loading...
Searching...
No Matches
to_polygon.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <vector>
8
9#include "polygon.hpp"
10#include "rpolygon.hpp"
11
12namespace recti {
13
24 template <typename T> constexpr auto to_polygon(const RPolygon<T>& rpoly) -> Polygon<T> {
25 const auto& _vecs = rpoly.vectors();
26 if (_vecs.empty()) {
27 return Polygon<T>(rpoly.origin(), {});
28 }
29
30 std::vector<Vector2<T>> new_vecs;
31 Vector2<T> current_point(0, 0);
32
33 for (const auto& next_point : _vecs) {
34 if (current_point.x() != next_point.x() && current_point.y() != next_point.y()) {
35 // Add intermediate point for non-rectilinear segment
36 new_vecs.emplace_back(next_point.x(), current_point.y());
37 }
38 new_vecs.emplace_back(next_point);
39 current_point = next_point;
40 }
41
42 // Handle closing segment
43 Vector2<T> first_point(0, 0);
44 if (current_point.x() != first_point.x() && current_point.y() != first_point.y()) {
45 new_vecs.emplace_back(first_point.x(), current_point.y());
46 }
47
48 return Polygon<T>(rpoly.origin(), std::move(new_vecs));
49 }
50} // namespace recti
Polygon.
Definition polygon.hpp:23
constexpr const std::vector< Vector2< T > > & vectors() const
Gets the displacement vectors of the polygon.
Definition polygon.hpp:124
Definition rpolygon.hpp:86
2D vector template class.
Definition vector2.hpp:28
constexpr auto x() const noexcept -> const T1 &
Returns a const reference to the x-coordinate of the vector.
Definition vector2.hpp:72
constexpr auto y() const noexcept -> const T2 &
Returns a const reference to the y-coordinate of the vector.
Definition vector2.hpp:81
Definition svg_utils.hpp:12
constexpr auto to_polygon(const RPolygon< T > &rpoly) -> Polygon< T >
Converts a rectilinear polygon to a general polygon.
Definition to_polygon.hpp:24
General polygon template class with geometric properties.
Rectilinear polygon (RPolygon) template class.