-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal1.c~
400 lines (327 loc) · 11.6 KB
/
final1.c~
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* mobile_s.c --- Display the moving object for which search was used */
/* compile: cc final1.c -g -O2 -Wall -I/usr/include/freetype2 -lftgl -lglfw -lGLU -lGL -lX11 -lXrandr -lm -o final1 */
/* CC=cc
CFLAGS=-g -O2 -Wall
LDFLAGS= -I/usr/include/freetype2
LDLIBS= -lftgl -lglfw -lGLU -lGL -lX11 -lXrandr -lm
final1: final1.c
$(CC) [email protected] $(CFLAGS) $(LDFLAGS $(LDLIBS) -o $@
~$ make
*/
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glfw.h>
#include <FTGL/ftgl.h>
#define CrossingNumber 100 /* The number of crossings = 100 */
#define MaxName 50 /* The maximum number of characters = 50 (half-width) */
#define Radius_Marker 0.2 /* The radius of a marker */
#define INDA 100
#define SHORTEST 100
/* Definition of coordinate conversion macro */
#define ORIGIN_X -2.0
#define ORIGIN_Y 3.0
#define REAL_SIZE_X 15.0
#define REAL_SIZE_Y 15.0
/* Definition of data structure */
typedef struct
{
double x, y; /* Position x, y */
} Position; /* A structure that represents positions */
typedef struct
{
int id; /* Crossing number */
int destinationid, startid;
Position pos; /* A structure that represents positions */
double wait; /* Average waiting time */
char jname[MaxName]; /* Crossing name (Japanese) */
char ename[MaxName]; /* Crossing name (Alphabetical name) */
int points; /* The number of crossing roads */
int next[5]; /* Crossing numbers of neighboring crossings */
double distance;
} Crossing;
typedef struct{
int nextstepid;
int stepnumber;
int SHORTEST;
} Shortest;
typedef struct{
char start[200];
char destination[200];
int startid;
int destinationid;
int INDA;
} Inputdata;
/* Definition of variables in which data will be stored */
Crossing cross[CrossingNumber];
Shortest shor[SHORTEST];
Inputdata input[INDA];
void search_cross(int Crossing_number, int INDA) /* Search for exactly-matched data */
{
int i;
int f= -1;
char inputstart[200],inputdestination[200],answer;
printf("\n\nplease input your start point and destination point\nFor example: if you want to start from place A go to place B, please input as A to B");
scanf("%s to %s",inputstart, inputdestination);
for (i=0;cross[i].ename!=NULL;i++)
{
if(strcmp(cross[i].ename,inputstart)==0)
{
strcpy(start,inputstart);
f=1;
inputid=cross[i].id;
return Crossing_number;
break;
}
}
for(i=0;cross[i].ename!=NULL;i++)
{
if(strcmp(cross[i].ename,inputdestination)==0)
{
strcpy(destination,inputdestination);
f=1;
destinationid=cross[i].id;
return Crossing_number;
break;
}
}
if(f<0)
{
printf("\nyour input is not been found, please check your destination again");
return f;
}
printf("\nyour journey is from %s to %s, is it correct?(yes or no)\n",start,destination);
scanf("%s",answer);
if(strcmp(answer,"yes")!=0)
{
printf("program terminted");
exit(EXIT_FAILURE);
}
return INDA;
}
void dist(int Crossing_number, int INDA)
{
int i;
double distance,x,y;
for (i=0;i<crossing_number;i++)
{
x=abs(cross[destination].pos.x-cross[i].pos.x);
y=abs(cross[destination].pos.y-cross[i].pos.y);
cross[i].distance=hypot(x,y);
}
return Crossing_number;
}
void search_shortest_next_crossing(int Crossing_number, int SHORTEST)
{
int i,j,shortestid,nextid,nextid1,currentid;
currentid=cross[start].id;
for(i=0;shortestid!=cross[destination].id;i++)
{
shortestid=cross[currentid].next[0];
for(j=0;j<cross[start].pints-1;j++)
{
nextid1=cross[currentid].next[j+1];
if(cross[nextid1].distance<cross[shortestid].distance)
{
shortestid=nextid1;
}
}
shor[i].nextstepid=shortestid;
nextid=shortestid;
return nextid;
currentid=shortestid;
return currentid;
}
if(i=0)
{
printf("\nyour destination and your start point is the same");
exit(EXIT_FAILURE);
}
return SHORTEST;
}
/* Draw a circle */
void circle(double x, double y, double r)
{
int const N = 12; /* Divide the circumference by 12 and draw in line segments */
int i;
glBegin(GL_LINE_LOOP);
for (i = 0; i < N; i++)
glVertex2d(x + cos(2 * M_PI * i / N) * r, y + sin(2 * M_PI * i / N) * r);
glEnd();
}
#ifndef FONT_FILENAME
/* The file name of the fonts that are available on the training server */
#define FONT_FILENAME "/usr/share/fonts/truetype/fonts-japanese-gothic.ttf"
#endif
FTGLfont *font; /* A pointer that points to the read fonts */
/* Draw strings */
void outtextxy(double x, double y, char const *text)
{
double const scale = 0.01;
glPushMatrix();
glTranslated(x, y, 0.0);
glScaled(scale, scale, scale);
ftglRenderFont(font, text, FTGL_RENDER_ALL);
glPopMatrix();
}
/* Read the file */
int map_read(char *filename)
{
FILE *fp;
int i, j;
int crossing_number; /* The number of crossings */
fp = fopen(filename, "r");
if (fp == NULL)
{
perror(filename);
return -1;
}
/* First, read the number of crossings */
fscanf(fp, "%d", &crossing_number);
for (i = 0; i < crossing_number; i++)
{
fscanf(fp, "%d,%lf,%lf,%lf,%[^,],%[^,],%d,%d,%d,",&(cross[i].id), &(cross[i].pos.x), &(cross[i].pos.y), &(cross[i].wait), cross[i].jname,cross[i].ename, &(cross[i].points));
for(j=0;cross[i].next[j]!=NULL;j++)
{
fscanf(fp,"%d",&cross[i].next[j]);
}
}
fclose(fp);
/* Return the number of crossings that has been read from the file */
return crossing_number;
}
/* Display road networks */
void map_show(int crossing_number)
{
int i, j;
for (i = 0; i < crossing_number; i++)
{ /* A loop for each crossing */
double x0 = cross[i].pos.x;
double y0 = cross[i].pos.y;
/* Draw a circle that represents a crossing */
glColor3d(1.0, 0.5, 0.5);
circle(x0, y0, 0.1);
/* Draw the name of the crossing */
glColor3d(1.0, 1.0, 0.0);
outtextxy(x0, y0, cross[i].ename);
/* Draw roads that start from the crossing */
glColor3d(1.0, 1.0, 1.0);
glBegin(GL_LINES);
for(j = 0; j < cross[i].points; j++)
{
double x1 = cross[ cross[i].next[j] ].pos.x;
double y1 = cross[ cross[i].next[j] ].pos.y;
glVertex2d(x0, y0);
glVertex2d(x1, y1);
}
glEnd();
}
}
int main(void)
{
int Crossing_number; /* The number of crossings */
int destination; /* The final destination point of the moving object */
int start;
int vehicle_edgeFrom; /* The position of the moving object on the path
(it represents that this is the n-th road) */
int vehicle_edgeTo; /* The position of the moving object on the path
(it represents that this is the n-th road) */
int vehicle_stepOnEdge; /* The position of the moving object on the road
(it represents that this is the n-th step) */
double vehicle_x = 0.0, vehicle_y = 0.0; /* The coordinate of the moving object */
int INDA;
int SHORTEST;
int currentid;
int nextid;
double distance;
vehicle_edgeFrom=start;
vehicle_edgeTo=destination;
Crossing_number = map_read("map2.dat");
if (Crossing_number < 0)
{
fprintf(stderr, "couldn't read map file\n");
exit (1);
}
search_cross(Crossing_number,INDA);
dist(Crossing_number,SHORTEST);
search_shortest_next_crossing(Crossing_number,SHORTEST);
/* Initialize the graphic environment and open a window */
glfwInit();
glfwOpenWindow(1000, 800, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
/* Based on (ORIGIN_X, ORIGIN_Y), project the space within the range of REAL_SIZE_X * REAL_SIZE_Y to the viewport */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(ORIGIN_X + REAL_SIZE_X * -0.5, ORIGIN_X + REAL_SIZE_X * 0.5, ORIGIN_Y + REAL_SIZE_Y * -0.5, ORIGIN_Y + REAL_SIZE_Y * 0.5, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); /* Do not perform other coordinate conversion */
/* Read and configure the font used for drawing strings */
font = ftglCreateExtrudeFont(FONT_FILENAME);
if (font == NULL)
{
perror(FONT_FILENAME);
fprintf(stderr, "could not load font\n");
exit (1);
}
ftglSetFontFaceSize(font, 24, 24);
ftglSetFontDepth(font, 0.01);
ftglSetFontOutset(font, 0, 0.1);
ftglSetFontCharMap(font, ft_encoding_unicode);
/* Initialize the state of the moving object */
#if 1
start = 5; /* From Izumi Ohashi */
destination = 19; /* To the Road Museum */
#else
vehicle_edgeFrom = 5; /* From Izumi Ohashi */
vehicle_goal = 71; /* To Yagiyama Zoological Park */
#endif
vehicle_edgeTo = vehicle_edgeFrom;
vehicle_stepOnEdge = 0;
for (;;) {
int width, height;
/* End when Esc is pressed or the window is closed */
if (glfwGetKey(GLFW_KEY_ESC) || !glfwGetWindowParam(GLFW_OPENED))
break;
glfwGetWindowSize(&width, &height); /* Get the size of the current window */
glViewport(0, 0, width, height); /* Set the whole window as a viewport */
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT); /* Fill the back buffer with black */
map_show(Crossing_number); /* Display road networks */
/* Move the moving object forward and calculate the coordinate */
if (start != destination)
{
/* Since the goal has not been reached, move forward the position of the moving object */
/* Create based on the previous exercise.
The number of variables and their names have been changed, but the basic flow is the
same.
Complete the program.
*/
nextid =search_shortest_next_crossing(Crossing_number, SHORTEST);
currentid=search_shortest_next_crossing(Crossing_number, SHORTEST);
/* Set (x0, y0), (x1, y1) for the current and next crossings */
double x0 = cross[currentid].pos.x; /* The last crossing passed, x */
double y0 = cross[currentid].pos.y; /* The last crossing passed, y */
double x1 = cross[nextid].pos.x; /* The next crossing to pass, x */
double y1 = cross[nextid].pos.y; /* The next crossing to pass, y */
double distance = hypot(x1 - x0, y1 - y0); /* Direct distance */
int steps = (int)(distance / 0.1);
/* Move forward on the road and update the coordinate */
vehicle_stepOnEdge++;
vehicle_x = x0 + (x1 - x0) / steps * vehicle_stepOnEdge;
vehicle_y = y0 + (y1 - y0) / steps * vehicle_stepOnEdge;
if (vehicle_stepOnEdge >= steps) { /* After reaching the crossing, enter the next road */
vehicle_edgeFrom = vehicle_edgeTo; /* Increment the index that represents the current crossing on the path */
vehicle_stepOnEdge = 0; /* Reset the index that represents that it is the n-th step on the edge */
}
}
/* Display the goal and the moving object */
glColor3d(1.0, 1.0, 1.0);
circle(cross[nextid].pos.x, cross[nextid].pos.y, Radius_Marker);
circle(vehicle_x, vehicle_y, Radius_Marker);
glfwSwapBuffers(); /* Swap the front buffer with the back buffer */
usleep(50 * 1000); /* Wait for about 50 millisecond */
}
glfwTerminate();
return 0;
}