|
|
1.1 root 1: #include "map.h"
2:
3: #define NSYMBOL 20
4:
5: enum flag { POINT,ENDSEG,ENDSYM };
6: struct symb {
7: double x, y;
8: char name[10+1];
9: enum flag flag;
10: } *symbol[NSYMBOL];
11:
12: static int nsymbol;
13: static double halfrange = 1;
14: extern int halfwidth;
15:
16: static int getrange(FILE *);
17: static int getsymbol(FILE *, int);
18: static void setrot(struct place *, double, int);
19: static void dorot(struct symb *, double *, double *);
20:
21:
22: void
23: getsyms(char *file)
24: {
25: FILE *sf = fopen(file,"r");
26: if(sf==0)
27: filerror("cannot open", file);
28: while(nsymbol<NSYMBOL-1 && getsymbol(sf,nsymbol))
29: nsymbol++;
30: fclose(sf);
31: }
32:
33: static int
34: getsymbol(FILE *sf, int n)
35: {
36: double x,y;
37: char s[2];
38: int i;
39: struct symb *sp;
40: for(;;) {
41: if(fscanf(sf,"%1s",s)==EOF)
42: return 0;
43: switch(s[0]) {
44: case ':':
45: break;
46: case 'o':
47: case 'c': /* cl */
48: fscanf(sf,"%*[^\n]");
49: continue;
50: case 'r':
51: if(getrange(sf))
52: continue;
53: default:
54: error("-y file syntax error");
55: }
56: break;
57: }
58: sp = (struct symb*)malloc(sizeof(struct symb));
59: symbol[n] = sp;
60: if(fscanf(sf,"%10s",sp->name)!=1)
61: return 0;
62: i = 0;
63: while(fscanf(sf,"%1s",s)!=EOF) {
64: switch(s[0]) {
65: case 'r':
66: if(!getrange(sf))
67: break;
68: continue;
69: case 'm':
70: if(i>0)
71: symbol[n][i-1].flag = ENDSEG;
72: continue;
73: case ':':
74: ungetc(s[0],sf);
75: break;
76: default:
77: ungetc(s[0],sf);
78: case 'v':
79: if(fscanf(sf,"%f %f",&x,&y)!=2)
80: break;
81: sp->x = x*halfwidth/halfrange;
82: sp->y = y*halfwidth/halfrange;
83: sp->flag = POINT;
84: i++;
85: symbol[n] = (struct symb*)realloc(symbol[n],
86: (i+1)*sizeof(struct symb));
87: sp++;
88: continue;
89: }
90: break;
91: }
92: if(i>0)
93: symbol[n][i-1].flag = ENDSYM;
94: else
95: symbol[n] = 0;
96: return 1;
97: }
98:
99: static int
100: getrange(FILE *sf)
101: {
102: double x,y,xmin,ymin;
103: if(fscanf(sf,"%*s %f %f %f %f",
104: &xmin,&ymin,&x,&y)!=4)
105: return 0;
106: x -= xmin;
107: y -= ymin;
108: halfrange = (x>y? x: y)/2;
109: if(halfrange<=0)
110: error("bad ra command in -y file");
111: return 1;
112: }
113:
114: /* r=0 upright;=1 normal;=-1 reverse*/
115: int
116: putsym(struct place *p, char *name, double s, int r)
117: {
118: int x,y,n;
119: struct symb *sp;
120: double dx,dy;
121: int conn = 0;
122: for(n=0; symbol[n]; n++)
123: if(strcmp(name,symbol[n]->name)==0)
124: break;
125: sp = symbol[n];
126: if(sp==0)
127: return 0;
128: if(!doproj(p,&x,&y))
129: return 1;
130: setrot(p,s,r);
131: for(;;) {
132: dorot(sp,&dx,&dy);
133: conn = cpoint(x+(int)dx,y+(int)dy,conn);
134: switch(sp->flag) {
135: case ENDSEG:
136: conn = 0;
137: case POINT:
138: sp++;
139: continue;
140: case ENDSYM:
141: break;
142: }
143: break;
144: }
145: return 1;
146: }
147:
148: static double rot[2][2];
149:
150: static void
151: setrot(struct place *p, double s, int r)
152: {
153: double x0,y0,x1,y1;
154: struct place up;
155: up = *p;
156: up.nlat.l += .5*RAD;
157: sincos(&up.nlat);
158: if(r&&(*projection)(p,&x0,&y0)) {
159: if((*projection)(&up,&x1,&y1)<=0) {
160: up.nlat.l -= RAD;
161: sincos(&up.nlat);
162: if((*projection)(&up,&x1,&y1)<=0)
163: goto unit;
164: x1 = x0 - x1;
165: y1 = y0 - y1;
166: } else {
167: x1 -= x0;
168: y1 -= y0;
169: }
170: x1 = r*x1;
171: s /= hypot(x1,y1);
172: rot[0][0] = y1*s;
173: rot[0][1] = x1*s;
174: rot[1][0] = -x1*s;
175: rot[1][1] = y1*s;
176: } else {
177: unit:
178: rot[0][0] = rot[1][1] = s;
179: rot[0][1] = rot[1][0] = 0;
180: }
181: }
182:
183: static void
184: dorot(struct symb *sp, double *px, double *py)
185: {
186: *px = rot[0][0]*sp->x + rot[0][1]*sp->y;
187: *py = rot[1][0]*sp->x + rot[1][1]*sp->y;
188: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.