-
Notifications
You must be signed in to change notification settings - Fork 0
/
retangulos.c
445 lines (419 loc) · 17.7 KB
/
retangulos.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* TRABALHO 4 - MAPEAMENTO DA CIDADE DE BITNÓPOLIS
* LUCCAS DE FREITAS FERREIRA - LEONARDO FIRMINO DE SOUZA
* IMPLEMENTAÇÃO DE TABELA HASH USANDO LISTA ENCADEADA E DUPLOHASHING
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tree.h"
#include "hash.h"
#include "retangulos.h"
#define MAX 500
//VARIAVEIS PARA INVOCACAO POR PARAMETRO
char fileName[MAX];
char dirSaida[MAX];
char *solicingaux;
//FGETS MODIFICADA PARA CONCATENAR COM \0
char *fgets2(char* str, int size, FILE* stream){
char *result = fgets(str, size, stream);
if(result){
while (isspace(str[strlen(str) - 1])){
str[strlen(str) - 1] = '\0';
}
}
return result;
}
//RECURSAO PARA PERCORRER ARVORE E GERAR SVG
void getInfo(Tree *root, FILE *file){
if(root == NULL)
return;
if(*root != NULL){
//Retangulo (*root)->info = (*root)->info;
getInfo(&((*root)->esq), file);
if((*root)->info.type == 0){ //RETANGULO COMUM
fprintf(file,"\n<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" style=\"fill:%s;\"/>",
(*root)->info.x, (*root)->info.y, (*root)->info.h, (*root)->info.l, (*root)->info.cor);
}
else if((*root)->info.type == 1){ //QUADRA
fprintf(file,"\n<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" style=\"fill:#cc5200;\"/>",
(*root)->info.x, (*root)->info.y, (*root)->info.h, (*root)->info.l);
}
//else if((*root)->info.type == 2) //CASA
//algo pra verificar cep
//else if((*root)->info.type == 3) //COMERCIO
//algo pra verificar cep
else if((*root)->info.type == 4){ //HIDRANTE
fprintf(file,"\n<circle cx=\"%f\" cy=\"%f\" r=\"8\" fill=\"red\"/>", (*root)->info.x, (*root)->info.y);
fprintf(file, "\n<text x=\"%f\" y=\"%f\" fill=\"white\"> H </text>", (*root)->info.x, (*root)->info.y);
}
else if((*root)->info.type == 5){ //SEMAFORO
fprintf(file,"\n<circle cx=\"%f\" cy=\"%f\" r=\"8\" fill=\"green\"/>", (*root)->info.x, (*root)->info.y);
fprintf(file, "\n<text x=\"%f\" y=\"%f\" fill=\"red\"> S </text>", (*root)->info.x, (*root)->info.y);
}
else if((*root)->info.type == 6){ //RADIO-BASE
fprintf(file,"\n<circle cx=\"%f\" cy=\"%f\" r=\"8\" fill=\"yellow\"/>", (*root)->info.x, (*root)->info.y);
fprintf(file, "\n<text x=\"%f\" y=\"%f\" fill=\"black\"> (:c:) </text>", (*root)->info.x, (*root)->info.y);
}
getInfo(&((*root)->dir), file);
}
}
//FUNÇAO DE PROCESSAMENTO DO NOME DO ARQUIVO
void processaNome(){
char *caminho, *auxcaminho; // tratamento para captar o nome do arquivo de entrada, que será usado para criação dos arquivos de saida
caminho = strtok(fileName, "/");
while(caminho != NULL){
auxcaminho = caminho;
caminho = strtok(NULL, "/");
}
solicingaux = strtok(auxcaminho,".");
}
//GERAR ARQUIVO DE SAIDA SVG
void makeSVG(Tree* root, char *sufix){
char saida1[MAX]; // pega o nome tratado, e adciona .svg para criação das imagens
if(sufix != NULL){ //caso haja sufixo
int i=0, aux = strlen(solicingaux), j = 0;
while(j != strlen(solicingaux)){ //procura o segundo "-", caso haja. (conflito com os nomes dos arquivos.ops)
if(solicingaux[j] == '-'){
aux = j;
i++;
}
j++;
}
if(i == 2)
solicingaux[aux] = '\0';
strtok(solicingaux,".svg");
strcat(solicingaux, "-");
strcat(solicingaux, sufix);
}
strcat(solicingaux,".svg");// concatena a extenção ao arquivo
strcpy(saida1,dirSaida);
strcat(saida1,"\\");
strcat(saida1,solicingaux);
FILE* file = fopen(saida1, "w+"); // abre o arquivo para escrita do svg
Retangulo retangulos;
if(file){
fprintf(file,"<svg height=\"3000\" width=\"3000\">");
if(root == NULL)
return;
if(*root != NULL){
getInfo(root, file);
}
fprintf(file,"\n</svg>");
}else
printf("\nErro ao abrir o arquivo.\n");
fclose(file);
printf("\nArquivo SVG gerado.\n");
}
//AREA DE BUSCA PARA REMOÇÃO
int removeRetangulo(Tree *root, Retangulo retangulo, FILE *file){
if(root == NULL)
return 0;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(aux.x >= retangulo.x && aux.y >= retangulo.y){
if(aux.x + aux.h <= retangulo.h && aux.y + aux.l <= retangulo.l){
fprintf(file, "Objeto removido: (%f, %f) ", (*root)->info.x, (*root)->info.y);
fprintf(file, "\r\n");
return removeNode(root, aux);
}
}
removeRetangulo(&((*root)->esq), retangulo, file);
removeRetangulo(&((*root)->dir), retangulo, file);
}
}
//AREA DE BUSCA PARA ALTERAR COR
int changeColor(Tree *root, char *cor1, char *cor2, Retangulo retangulo, int num, FILE *file){
if(root == NULL)
return 0;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(aux.x >= retangulo.x && aux.y >= retangulo.y){
if(aux.x + aux.h <= retangulo.h && aux.y + aux.l <= retangulo.l){
if(strcmp(aux.cor, cor1) == 0){
fprintf(file, "(%f, %f)\n", (*root)->info.x, (*root)->info.y);
fprintf(file, "\r\n");
strcpy((*root)->info.cor, cor2);
num++;
}
}
}
changeColor(&((*root)->esq), cor1, cor2, retangulo, num, file);
changeColor(&((*root)->dir), cor1, cor2, retangulo, num, file);
}else
return num+=num;
}
//ALTERAR COR DE TODOS OS RETANGULOS
int changeAll(Tree *root, char *cor1, char *cor2, int num){
if(root == NULL)
return 0;
if(*root != NULL){
if(strcmp((*root)->info.cor, cor1) == 0){
strcpy((*root)->info.cor, cor2);
num++;
}
changeAll(&((*root)->esq), cor1, cor2, num);
changeAll(&((*root)->dir), cor1, cor2, num);
}else
return num+=num;
}
//FUNÇÕES ADICIONAIS PARA O TRABALHO 4 - (HASH)
//MQ?
void procuraMoradores(Hash* hash, char* chave, FILE *file){
int i, pos, newPos;
if(hash == NULL)
return;
int nome = valueString(chave);
pos = hashFunction1(nome, hash->size);
for(i=0; i < hash->size; i++){
newPos = duploHash(pos, nome, i, hash->size);
if(hash->itens[newPos] == NULL)
return;
else if(valueString(hash->itens[newPos]->cep) == nome && hash->itens[newPos]->type == 2){
fprintf(file, "CEP: %s\n", hash->itens[newPos]->cep);fprintf(file, "\r\n");
fprintf(file, "FACE: %s\n", hash->itens[newPos]->face);fprintf(file, "\r\n");
fprintf(file, "NUM: %s\n", hash->itens[newPos]->num);fprintf(file, "\r\n");
fprintf(file, "NOME: %s\n", hash->itens[newPos]->nome);fprintf(file, "\r\n");
fprintf(file, "TEL: %s\n", hash->itens[newPos]->tel);fprintf(file, "\r\n");
fprintf(file, "\n###########################\n");fprintf(file, "\r\n");
fprintf(file, "\r\n");
}
}
}
//MR?
void moradoresRegiao(Tree *root, Hash* hash, Retangulo retangulo, FILE *file){
if(root == NULL)
return;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(aux.x >= retangulo.x && aux.y >= retangulo.y){
if(aux.x + aux.h <= retangulo.h && aux.y + aux.l <= retangulo.l){
if(aux.type == 1){
char *cep = malloc(sizeof(char) * 50);
strcpy(cep, aux.cep);
procuraMoradores(hash, cep, file);
}
}
}
moradoresRegiao(&((*root)->esq), hash, retangulo, file);
moradoresRegiao(&((*root)->dir), hash, retangulo, file);
}
}
//ET?
void procuraQuadra(Hash* hash, char* chave, Retangulo retangulo, FILE *file){
int i, pos, newPos;
if(hash == NULL)
return;
int nome = valueString(chave);
pos = hashFunction1(nome, hash->size);
for(i=0; i < hash->size; i++){
newPos = duploHash(pos, nome, i, hash->size);
if(hash->itens[newPos] == NULL)
return;
else if(valueString(hash->itens[newPos]->cep) == nome && hash->itens[newPos]->type == 1){
if(hash->itens[newPos]->x >= retangulo.x && hash->itens[newPos]->y >= retangulo.y){
if(hash->itens[newPos]->x + hash->itens[newPos]->h <= retangulo.h && hash->itens[newPos]->y + hash->itens[newPos]->l <= retangulo.l){
fprintf(file, "Coordenada: (%f, %f)\n", hash->itens[newPos]->x, hash->itens[newPos]->y);
fprintf(file, "\r\n");
fprintf(file, "Dimensoes: (%f, %f)\n", hash->itens[newPos]->h, hash->itens[newPos]->l);
fprintf(file, "\r\n");
fprintf(file, "CEP: %s\n", hash->itens[newPos]->cep);fprintf(file, "\r\n");
fprintf(file, "\n###########################\n");fprintf(file, "\r\n");
}
}
}
}
}
//ET?
void comercioQuadra(Tree *root, Hash* hash, Retangulo retangulo, char *tp, FILE *file){
if(root == NULL)
return;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(!strcmp(aux.tp, tp)){
char *cep = malloc(sizeof(char) * 50);
strcpy(cep, aux.cep);
procuraQuadra(hash, cep, retangulo, file);
}
comercioQuadra(&((*root)->esq), hash, retangulo, tp, file);
comercioQuadra(&((*root)->dir), hash, retangulo, tp, file);
}
}
//ED?
void procuraComercio(Hash* hash, char* chave, char *tp, FILE *file){
int i, pos, newPos;
if(hash == NULL)
return;
int nome = valueString(chave);
pos = hashFunction1(nome, hash->size);
for(i=0; i < hash->size; i++){
newPos = duploHash(pos, nome, i, hash->size);
if(hash->itens[newPos] == NULL)
return;
else if(valueString(hash->itens[newPos]->cep) == nome && hash->itens[newPos]->type == 3){
if(!strcmp(tp, hash->itens[newPos]->tp)){ //SE FOR O TIPO PASSADO
fprintf(file, "CEP: %s\n", hash->itens[newPos]->cep);fprintf(file, "\r\n");
fprintf(file, "FACE: %s\n", hash->itens[newPos]->face);fprintf(file, "\r\n");
fprintf(file, "NUM: %s\n", hash->itens[newPos]->num);fprintf(file, "\r\n");
fprintf(file, "TIPO: %s\n", hash->itens[newPos]->tp);fprintf(file, "\r\n");
fprintf(file, "NOME: %s\n", hash->itens[newPos]->nome);fprintf(file, "\r\n");
fprintf(file, "TEL: %s\n", hash->itens[newPos]->tel);fprintf(file, "\r\n");
fprintf(file, "\n###########################\n");fprintf(file, "\r\n");
fprintf(file, "\r\n");
}
else if(!strcmp(tp,"*")){ //SE FOR * PRINTA TODOS
fprintf(file, "CEP: %s\n", hash->itens[newPos]->cep);fprintf(file, "\r\n");
fprintf(file, "FACE: %s\n", hash->itens[newPos]->face);fprintf(file, "\r\n");
fprintf(file, "NUM: %s\n", hash->itens[newPos]->num);fprintf(file, "\r\n");
fprintf(file, "TIPO: %s\n", hash->itens[newPos]->tp);fprintf(file, "\r\n");
fprintf(file, "NOME: %s\n", hash->itens[newPos]->nome);fprintf(file, "\r\n");
fprintf(file, "TEL: %s\n", hash->itens[newPos]->tel);fprintf(file, "\r\n");
fprintf(file, "\n###########################\n");fprintf(file, "\r\n");
fprintf(file, "\r\n");
}
}
}
}
//ED?
void comercioRegiao(Tree *root, Hash* hash, Retangulo retangulo, char *tp, FILE *file){
if(root == NULL)
return;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(aux.x >= retangulo.x && aux.y >= retangulo.y){
if(aux.x + aux.h <= retangulo.h && aux.y + aux.l <= retangulo.l){
if(aux.type == 1){
char *cep = malloc(sizeof(char) * 50);
strcpy(cep, aux.cep);
procuraComercio(hash, cep, tp, file);
}
}
}
comercioRegiao(&((*root)->esq), hash, retangulo, tp, file);
comercioRegiao(&((*root)->dir), hash, retangulo, tp, file);
}
}
//ED?
void comerciosTotal(Tree *root, char *tp, FILE *file){
if(root == NULL)
return;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(aux.type == 3){
if(!strcmp(tp, aux.tp)){ //SE FOR O TIPO PASSADO
fprintf(file, "CEP: %s\n", aux.cep);fprintf(file, "\r\n");
fprintf(file, "FACE: %s\n", aux.face);fprintf(file, "\r\n");
fprintf(file, "NUM: %s\n", aux.num);fprintf(file, "\r\n");
fprintf(file, "TIPO: %s\n", aux.tp);fprintf(file, "\r\n");
fprintf(file, "NOME: %s\n", aux.nome);fprintf(file, "\r\n");
fprintf(file, "TEL: %s\n", aux.tel);fprintf(file, "\r\n");
fprintf(file, "\n###########################\n");fprintf(file, "\r\n");
fprintf(file, "\r\n");
}
else if(!strcmp(tp,"*")){ //SE FOR * PRINTA TODOS
fprintf(file, "CEP: %s\n", aux.cep);fprintf(file, "\r\n");
fprintf(file, "FACE: %s\n", aux.face);fprintf(file, "\r\n");
fprintf(file, "NUM: %s\n", aux.num);fprintf(file, "\r\n");
fprintf(file, "TIPO: %s\n", aux.tp);fprintf(file, "\r\n");
fprintf(file, "NOME: %s\n", aux.nome);fprintf(file, "\r\n");
fprintf(file, "TEL: %s\n", aux.tel);fprintf(file, "\r\n");
fprintf(file, "\n###########################\n");fprintf(file, "\r\n");
fprintf(file, "\r\n");
}
}
comerciosTotal(&((*root)->esq), tp, file);
comerciosTotal(&((*root)->dir), tp, file);
}
}
//HQ?
void hidranteQuadra(Tree *root, Retangulo retangulo, FILE *file){
if(root == NULL)
return;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(!strcmp(retangulo.cep,aux.nome)){
if(aux.x + aux.h <= retangulo.l + aux.x && aux.y + aux.l <= retangulo.l + aux.y){
if(aux.type == 4){
fprintf(file, "ID: %s\n", aux.nome);fprintf(file, "\r\n");
fprintf(file, "Coordenada-(%f, %f)-\n", aux.x, aux.y);fprintf(file, "\r\n");
fprintf(file, "\r\n");
}
}
}
hidranteQuadra(&((*root)->esq), retangulo, file);
hidranteQuadra(&((*root)->dir), retangulo, file);
}
}
//QT?
void buscaTorreArea(Tree *root, Retangulo retangulo, FILE *file){
if(root == NULL)
return;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(aux.type == 6){
if(aux.x >= retangulo.x && aux.y >= retangulo.y){
if(aux.x <= retangulo.x + retangulo.h && aux.y <= retangulo.y + retangulo.l){
fprintf(file, "CEP: %s\n", retangulo.cep);fprintf(file, "\r\n");
fprintf(file, "Ancoragem: (%f, %f)\n", retangulo.x, retangulo.y);fprintf(file, "\r\n");
fprintf(file, "Dimensoes: (H %f, L %f)\n", retangulo.h, retangulo.l);fprintf(file, "\r\n");
fprintf(file, "\n###########################\n");fprintf(file, "\r\n");
fprintf(file, "\r\n");
}
}
}
buscaTorreArea(&((*root)->esq), retangulo, file);
buscaTorreArea(&((*root)->dir), retangulo, file);
}
}
//QT?
void quadraTorre(Tree *root, FILE *file){//quadras com torres de celular
Tree *rootAux = root;
if(root == NULL)
return;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(aux.type == 1)
buscaTorreArea(rootAux, aux, file);
quadraTorre(&((*root)->esq), file);
quadraTorre(&((*root)->dir), file);
}
}
//H?
void hidrante(Tree *root, Retangulo retangulo, FILE *file){
if(root == NULL)
return;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(aux.x >= retangulo.x && aux.y >= retangulo.y){
if(aux.x + aux.h <= retangulo.h && aux.y + aux.l <= retangulo.l){
if(aux.type == 4){
fprintf(file, "ID: %s\n", aux.nome);fprintf(file, "\r\n");
fprintf(file, "(%f, %f)\n", aux.x, aux.y);fprintf(file, "\r\n");
fprintf(file, "\r\n");
}
}
}
hidrante(&((*root)->esq), retangulo, file);
hidrante(&((*root)->dir), retangulo, file);
}
}
//S?
void semaforo(Tree *root, Retangulo retangulo, FILE *file){
if(root == NULL)
return;
if(*root != NULL){
Retangulo aux = (*root)->info;
if(aux.x >= retangulo.x && aux.y >= retangulo.y){
if(aux.x + aux.h <= retangulo.h && aux.y + aux.l <= retangulo.l){
if(aux.type == 5){
fprintf(file, "ID: %s\n", aux.nome);fprintf(file, "\r\n");
fprintf(file, "(%f, %f)\n", aux.x, aux.y);fprintf(file, "\r\n");
fprintf(file, "\r\n");
}
}
}
semaforo(&((*root)->esq), retangulo, file);
semaforo(&((*root)->dir), retangulo, file);
}
}