-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolygon.cpp
More file actions
202 lines (183 loc) · 5.33 KB
/
Polygon.cpp
File metadata and controls
202 lines (183 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// Created by bpiranda on 10/12/2019.
//
//
// Created by bpiranda on 19/11/2019.
//
#include <cmath>
#include <glutWindow.h>
#include "Polygon.h"
#include <stack>
#include <algorithm>
using namespace std;
bool polarComparison(const Vector2D &P1,const Vector2D &P2) {
double a1 = asin(P1.y/sqrt(P1.x*P1.x+P1.y*P1.y));
if (P1.x<0.0) a1=M_PI-a1;
double a2 = asin(P2.y/sqrt(P2.x*P2.x+P2.y*P2.y));
if (P2.x<0.0) a2=M_PI-a2;
return a1<a2;
}
MyPolygon::MyPolygon(vector<Vector2D> &points) {
assert(points.size()>3);
auto p=points.begin();
auto pymin=points.begin();
// find point with minimal y and swap with first point
while (p!=points.end()) {
if (p->y<pymin->y) {
pymin=p;
}
p++;
}
// swap
if (pymin!=points.begin()) iter_swap(points.begin(), pymin);
Vector2D origin(points.begin()->x,points.begin()->y);
// copy points in a set of points relative to points[0]
vector<Vector2D> pointsRelative;
for (auto pOrig:points) {
pointsRelative.push_back(Vector2D(pOrig.x-origin.x,pOrig.y-origin.y));
}
// sorting point with angular criteria
sort(pointsRelative.begin()+1,
pointsRelative.end(),polarComparison);
stack<Vector2D*> CHstack;
Vector2D *top_1,*top;
CHstack.push(&pointsRelative[0]);
CHstack.push(&pointsRelative[1]);
CHstack.push(&pointsRelative[2]);
vector<Vector2D>::iterator pi=pointsRelative.begin()+3;
while (pi!=pointsRelative.end()) {
top = CHstack.top();
CHstack.pop();
top_1 = CHstack.top();
CHstack.push(top);
while (!isOnTheLeft(&(*pi),top_1,top)) {
CHstack.pop();
// update values of top and top_1
top = CHstack.top();
CHstack.pop();
top_1 = CHstack.top();
CHstack.push(top);
}
CHstack.push(&(*pi));
pi++;
}
// get stack points to create current polygon
N=CHstack.size();
Nmax = N;
tabPts = new Vector2D[Nmax+1];
int i=N-1;
while (!CHstack.empty()) {
tabPts[i--]=*(CHstack.top())+origin;
CHstack.pop();
}
tabPts[N]=tabPts[0];
setColor(YELLOW);
}
void MyPolygon::triangulation() {
tabTriangles.clear();
vector<Vector2D*> tmp;
// copy the list of vertices into the tmp list
for (int i=0; i<N; i++) {
tmp.push_back(&(tabPts[i]));
}
int n=N;
// while we can add a triangle to tabTriangles
while (n>2) {
int i=0;
auto p = tmp.begin();
bool test;
// create a triangle using p,p+1 and p+2 as vertices
Triangle T(*p,*(p+1),*(p+2));
do { // search a triangle without another points inside
test=!T.isEmpty(tmp,i+3);
if (test) {
i++;
p++;
T=Triangle(*p,*(p+1),*(p+2));
}
} while (i<n-2 && test);
assert(i<n-2);
// add T to tabTriangles
tabTriangles.push_back(T);
tmp.erase(p+1); // remove point(p+1) from tmp;
n--; // or n=tmp.size();
}
}
void MyPolygon::draw() {
if (tabTriangles.empty()) {
triangulation();
}
// draw the interior
glColor4fv(&color[0]);
/*glBegin(GL_POLYGON);
for (int i=0; i<N; i++) {
glVertex2f(tabPts[i].x,tabPts[i].y);
}
glEnd();
glBegin(GL_TRIANGLES);
for (int i=0; i<N-1; i++) {
glVertex2f(tabPts[0].x,tabPts[0].y);
glVertex2f(tabPts[i].x,tabPts[i].y);
glVertex2f(tabPts[i+1].x,tabPts[i+1].y);
}
glEnd();*/
for (auto t:tabTriangles) {
glBegin(GL_TRIANGLES);
glVertex2f(t.ptr[0]->x,t.ptr[0]->y);
glVertex2f(t.ptr[1]->x,t.ptr[1]->y);
glVertex2f(t.ptr[2]->x,t.ptr[2]->y);
glEnd();
}
// draw the border
/*glColor4fv(&BLUE[0]);
glBegin(GL_LINE_LOOP);
for (auto t:tabTriangles) {
glVertex2f(t.ptr[0]->x,t.ptr[0]->y);
glVertex2f(t.ptr[1]->x,t.ptr[1]->y);
glVertex2f(t.ptr[2]->x,t.ptr[2]->y);
}
glEnd();*/
glColor3fv(&BLACK[0]);
glLineWidth(3);
glBegin(GL_LINE_LOOP);
for (int i=0; i<N; i++) {
glVertex2f(tabPts[i].x,tabPts[i].y);
}
glEnd();
glLineWidth(1);
// draw the number of points
/*for (int i=0; i<N; i++) {
glBegin(GL_LINES);
glVertex2f(tabPts[i].x-10,tabPts[i].y-10);
glVertex2f(tabPts[i].x+10,tabPts[i].y+10);
glEnd();
glBegin(GL_LINES);
glVertex2f(tabPts[i].x+10,tabPts[i].y-10);
glVertex2f(tabPts[i].x-10,tabPts[i].y+10);
glEnd();
GlutWindow::drawText(tabPts[i].x-10,tabPts[i].y,to_string(i),GlutWindow::ALIGN_RIGHT);
}*/
}
void MyPolygon::clip(int x0,int y0,int x1,int y1) {
int i=0;
while (i<N) {
// case Left/Bottom
if (tabPts[i].x==x0 && tabPts[i+1].y==y0) {
insertPoint(Vector2D(x0,y0),i+1);
i++;
} else // case Bottom/Right
if (tabPts[i].y==y0 && tabPts[i+1].x==x1) {
insertPoint(Vector2D(x1,y0),i+1);
i++;
} else // case Right/Top
if (tabPts[i].x==x1 && tabPts[i+1].y==y1) {
insertPoint(Vector2D(x1,y1),i+1);
i++;
} else // case Top/Left
if (tabPts[i].y==y1 && tabPts[i+1].x==x0) {
insertPoint(Vector2D(x0,y1),i+1);
i++;
}
i++;
}
}