include stdafx include list using namespace std int s_height 600 s_wid

  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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "stdafx.h"
#include <list>
using namespace std;
int s_height = 600, s_width = 800;
int F=0;
struct ver{
int x, y,f;
ver *b, *e;
};
struct vertex{
ver m[2];
int n;
};
list<ver> A,B;
list<list<ver>> R;
static void cursor_callback(GLFWwindow* window, double x, double y){
}
int index = 0;
static void mouse_callback(GLFWwindow* window, int button, int action, int mods){
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS){
}
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
double x, y;
ver t;
glfwGetCursorPos(window, &x, &y);
t.x = (int)x;
t.y = (int)(s_height - y);
t.b = NULL;
t.e = NULL;
if (!F){
t.f = index;
index++;
A.push_back(t);
}
else {
t.f = -1;
B.push_back(t);
}
}
}
void test(ver t, ver w, ver v, int *ft){
if ((w.x - t.x)*(v.y - t.y) - (v.x - t.x)*(w.y - t.y) < 0)
*ft = 1;
}
int Det(int a11, int a12, int a21, int a22){
return a11*a22-a12*a21;
}
void Saz_Hod(){
list<ver> K;
for (list<ver>::iterator v = B.begin(); v != B.end(); v++){
list<ver>::iterator w = v;
v++;
if (v == B.end())
v = B.begin();
for (list<ver>::iterator y = A.begin(); y != A.end(); y++){
list<ver>::iterator u = y;
y++;
if (y == A.end())
y = A.begin();
int fu = 0, fy = 0;
test(*y,*w,*v, &fy);
test(*u,*w,*v, &fu);
if (!fu && !fy){
y = u;
continue;
}
if (fu && fy){
K.push_back(*y);
y = u;
continue;
}
ver I;
int x1 = (*u).x, y1 = (*u).y, x2 = (*y).x, y2 = (*y).y, x3 = (*v).x, y3 = (*v).y, x4 = (*w).x, y4 = (*w).y;
int d = Det(y2-y1,x1-x2,y4-y3,x3-x4),k1=x1*y2-x2*y1,k2=x3*y4-x4*y3;
I.x = int(float(Det(k1, x1 - x2, k2, x3 - x4)) / float(d));
I.y = int(float(Det(y2 - y1, k1, y4 - y3, k2)) / float(d));
I.f = 0;
I.b = &(*w);
I.e = &(*v);
if (fu && !fy){
K.push_back(I);
y = u;
continue;
}
if (!fu && fy){
K.push_back(I);
K.push_back(*y);
}
y = u;
}
R.push_back(K);
K.erase(K.begin(), K.end());
v = w;
}
}
int do_it = 1;
void to_vertexs();
static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key == GLFW_KEY_ENTER && action == GLFW_PRESS){
F++;
if (F == 3 && do_it){
Saz_Hod();
to_vertexs();
do_it = 0;
}
}
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
if (F == 3)
F = 2;
else
F=3;
}
static void resize_callback(GLFWwindow* window, int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-(GLdouble)0, (GLdouble)width, -(GLdouble)0, (GLdouble)height, -(GLdouble)1, (GLdouble)1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
s_width = width;
s_height = height;
}
void draw(GLFWwindow* window,list<ver> D,int g){
double x, y;
if (g==0)
glColor3f(1, 0, 1);
else
glColor3f(1, 0, 0);
glLineWidth(1);
glBegin(GL_LINE_LOOP);
for (list<ver>::iterator v = D.begin(); v != D.end(); v++)
glVertex2i((*v).x, (*v).y);
if (F==g){
glfwGetCursorPos(window, &x, &y);
glVertex2i((int)x, int(s_height - y));
}
glEnd();
}
int test_in(int x1, int y1, int x2, int y2, int x0, int y0){
return (x2 - x0)*(x1 - x0) <= 0 && (y2 - y0)*(y1 - y0) <= 0;
}
list<vertex> W;
int dist(ver a, ver b){
return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}
void bsort(ver *b, int n){
for (int t = n - 1; t>0;){
int bound = t;
t = 0;
for (int i=0;i<bound;i++)
if (b[i + 1].f < b[i].f){
ver c = b[i];
b[i] = b[i + 1];
b[i + 1] = c;
t = i;
}
}
}
void to_vertexs(){
list<vertex> V;
for (list<list<ver>>::iterator L = R.begin(); L != R.end(); L++){
list<ver> T;
for (list<ver>::iterator y = (*L).begin(); y != (*L).end(); y++){
list<ver>::iterator u = y;
y++;
if (y == (*L).end())
y = (*L).begin();
ver a = *u;
ver b = *y;
y = u;
if (!a.b || !b.b){
vertex x;
x.m[0] = a;
x.m[1] = b;
if (!a.b)
x.n = a.f;
else
x.n = b.f+1;
if (x.n == -1)
x.n = index - 1;
if (!a.b && !b.b)
x.n = -1;
V.push_back(x);
}
if (!a.b)
continue;
int x1 = a.b->x, y1 = a.b->y, x2 = a.e->x, y2 = a.e->y, x3 = a.x, y3 = a.y;
if (test_in(x1, y1, x2, y2, x3, y3)){
T.push_back(a);
continue;
}
int m1 = dist(a,*a.b);
int m2 = dist(a,*a.e);
ver g;
if (m1 < m2)
g=*a.b;
else
g=*a.e;
g.b = a.b;
g.e = a.e;
T.push_back(g);
}
if (!T.size())
continue;
int n = T.size();
ver *Z = new ver[n];
int j = 0;
for (list<ver>::iterator v = T.begin(); v != T.end(); v++){
ver a = *v;
a.f = dist(a, *a.e);
Z[j] = a;
j++;
}
bsort(Z, n);
for (int i = 0; i < n - 1; i++){
if (!(i % 2)){
vertex x;
x.m[0] = Z[i];
x.m[1] = Z[i+1];
x.n = -1;
V.push_back(x);
}
}
delete[] Z;
}
W = V;
}
void draw_W(){
glColor3f(1, 0, 1);
for (list<vertex>::iterator v = W.begin(); v != W.end(); v++){
vertex u = *v;
glBegin(GL_LINE_STRIP);
glVertex2d(u.m[0].x, u.m[0].y);
glVertex2d(u.m[1].x, u.m[1].y);
glEnd();
}
}
int main(int argc, _TCHAR* argv[]){
if (!glfwInit()){
printf("glfwInit failed\n");
return -1;
}
GLFWwindow* window;
window = glfwCreateWindow(s_width, s_height, "Test app", NULL, NULL);
if (window == NULL){
printf("glfwOpenWindow failed. Can your hardware handle OpenGL 3.2?\n");
glfwTerminate();
return -2;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyboard_callback);
glfwSetFramebufferSizeCallback(window, resize_callback);
glfwSetMouseButtonCallback(window, mouse_callback);
glfwSetCursorPosCallback(window, cursor_callback);
resize_callback(window, s_width, s_height);
while (!glfwWindowShouldClose(window)){
glClear(GL_COLOR_BUFFER_BIT);
if (F>=0 && F<=2)
draw(window, A,0);
if (F>=1 && F<=2)
draw(window, B,1);
if (F > 2)
draw_W();
glfwSwapBuffers(window);
glfwWaitEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}