5 #define pi 3.1415926535897932384626433832795
9 struct TriangleHelper {
17 auto calculateInner = [] (QPoint& point, QPoint& prev, QPoint& post) {
18 QPoint AP(point.x() - prev.x(), point.y() - prev.y());
19 QPoint BP(point.x() - post.x(), point.y() - post.y());
21 float topSclar = AP.x() * BP.x() + AP.y() * BP.y();
22 float absolute =
static_cast<float>(sqrt(pow(AP.x(),2.) + pow(AP.y(),2.)) * sqrt(pow(BP.x(),2.) + pow(BP.y(),2.)));
23 return acos(topSclar / absolute);
27 auto getTip = [] (
const std::vector<TriangleHelper>& vec) {
29 for(
size_t i = 0; i<vec.size(); i++) {
30 if(vec[i].interiorAngle<vec[min].interiorAngle) {
38 auto getPrev = [] (
int idx,
int length) {
39 return (idx - 1)>=0 ? (idx - 1) : (length - 1);
43 auto getPost = [] (
int idx,
int length) {
44 return (idx + 1) % length;
48 auto isTip = [] (
float angle) {
49 return static_cast<double>(angle)<(
pi / 2.);
52 std::vector<TriangleHelper> Vertices;
53 std::vector<Triangle> Triangles;
56 for(
int i = 0; i<static_cast<int>(polyPoints.size()); i++) {
57 TriangleHelper helper;
58 int prev = getPrev(i,
static_cast<int>(polyPoints.size()));
59 int post = getPost(i,
static_cast<int>(polyPoints.size()));
61 helper.vertex = polyPoints[
static_cast<size_t>(i)];
64 helper.interiorAngle = calculateInner(polyPoints[
static_cast<size_t>(i)],
65 polyPoints[
static_cast<size_t>(prev)],
66 polyPoints[
static_cast<size_t>(post)]);
67 helper.isTip = isTip(helper.interiorAngle);
68 Vertices.push_back(helper);
72 while(Triangles.size() != polyPoints.size() - 2) {
74 TriangleHelper smallest = getTip(Vertices);
75 int prev = getPrev(smallest.idx,
static_cast<int>(Vertices.size()));
76 int post = getPost(smallest.idx,
static_cast<int>(Vertices.size()));
79 tri.
A = Vertices[
static_cast<size_t>(prev)].vertex;
80 tri.
B = Vertices[
static_cast<size_t>(smallest.idx)].vertex;
81 tri.
C = Vertices[
static_cast<size_t>(post)].vertex;
82 Triangles.push_back(tri);
85 Vertices.erase(Vertices.begin() + smallest.idx);
86 for(
size_t i =
static_cast<size_t>(smallest.idx); i<Vertices.size(); i++) {
91 post = getPrev(post, Vertices.size());
92 prev = prev<smallest.idx ? prev : (prev - 1);
95 int prevOfPrev = getPrev(prev,
static_cast<int>(Vertices.size()));
96 int postOfPrev = getPost(prev,
static_cast<int>(Vertices.size()));
98 int prevOfPost = getPrev(post,
static_cast<int>(Vertices.size()));
99 int postOfPost = getPost(post,
static_cast<int>(Vertices.size()));
103 Vertices[
static_cast<size_t>(prev)].interiorAngle = calculateInner(Vertices[
static_cast<size_t>(prev)].vertex,
104 Vertices[
static_cast<size_t>(prevOfPrev)].vertex,
105 Vertices[
static_cast<size_t>(postOfPrev)].vertex);
106 Vertices[
static_cast<size_t>(prev)].isTip = isTip(Vertices[
static_cast<size_t>(prev)].interiorAngle);
108 Vertices[
static_cast<size_t>(post)].interiorAngle = calculateInner(Vertices[
static_cast<size_t>(post)].vertex,
109 Vertices[
static_cast<size_t>(prevOfPost)].vertex,
110 Vertices[
static_cast<size_t>(postOfPost)].vertex);
111 Vertices[
static_cast<size_t>(post)].isTip = isTip(Vertices[
static_cast<size_t>(post)].interiorAngle);
117 for(
auto triangle : triangles) {