Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / sources / int / graf / cirb.c
1      /* Loglan82 Compiler&Interpreter\r
2      Copyright (C) 1981-1993 Institute of Informatics, University of Warsaw\r
3      Copyright (C)  1993, 1994 LITA, Pau\r
4      \r
5      This program is free software; you can redistribute it and/or modify\r
6      it under the terms of the GNU General Public License as published by\r
7      the Free Software Foundation; either version 2 of the License, or\r
8      (at your option) any later version.\r
9      \r
10      This program is distributed in the hope that it will be useful,\r
11      but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13      GNU General Public License for more details.\r
14      \r
15 =======================================================================\r
16 */\r
17 \r
18 #include "graf.h"
19
20 #define isign(k, i)     (((i) >= 0) ? (k) : -(k))
21 #define iabs(i)         ((i) >= 0 ? (i) : -(i))
22 #define sqr(x)          ((x)*(x))
23 #define min0(x, y)      ((x) < (y) ? (x) : (y))
24 #define max0(x, y)      ((x) > (y) ? (x) : (y))
25
26 #define alf(i)          alf_[i]
27 #define ix(i)           ix_[i]
28 #define iy(i)           iy_[i]
29 #define sx(i)           sx_[i]
30 #define sy(i)           sy_[i]
31 #define d(i)            d_[i]
32 #define min(i)          min_[i]
33 #define max(i)          max_[i]
34 #define mx(i)           mx_[i]
35 #define mx1(i)          mx1_[i]
36 #define incr1(i)        incr1_[i]
37 #define incr2(i)        incr2_[i]
38 #define ip(i)           ip_[i]
39 #define sj(i)           sj_[i]
40 #define sc(i)           sc_[i]
41 #define bl(i)           bl_[i]
42 #define bxy(i)          bxy_[i]
43 #define bp(i)           bp_[i]
44 #define g(i,j)          g_[i][j]
45 #define go(i,j)         go_[i][j]
46
47 #define logical         int
48 #define FALSE           0
49 #define TRUE            1
50 #define INT(x)          ((int) (x))
51 #define FLOAT(x)        ((float) (x))
52 #define SIN(x)          sin((double) (x))
53 #define COS(x)          cos((double) (x))
54 #define SQRT(x)         sqrt((double) (x))
55
56 int pa = 3, qa = 4;
57 float asp = 0.75;
58
59 void pascal cirb(xi, yi, ri, alfa, beta, cbord, bcint, p, q)
60 int *xi, *yi, *ri;
61 float *alfa, *beta;
62 int *cbord, *bcint, *p, *q;
63 {
64     extern void pascal wyc47();
65     
66     wyc47(*xi, *yi, (double) *ri, (double) *alfa, (double) *beta,
67           *cbord, *bcint, pa*(*p), qa*(*q), (double) 1.0);
68 }
69
70 /***********************************************************************
71 void pascal rcirb(xr, yr, r, alfa, beta, cbord, bcint, p, q)
72 float *xr, *yr, *r, *alfa, *beta;
73 int *cbord, *bcint, *p, *q;
74 {
75     extern void pascal wyc47();
76     extern float mix, miy, sxx, syy;
77     extern int mii, maj;
78     
79     wyc47(mii+INT(sxx*(*xr-mix)), maj-INT(syy*(*yr-miy)), (double) *r,
80           (double) *alfa, (double) *beta,
81           *cbord, *bcint, pa*(*p), qa*(*q), (double) sxx);
82 }
83 ***********************************************************************/
84
85 void pascal wyc47(ir, jr, r, alfa, beta, cbord, bcint, p, q, cx)
86 int ir, jr;
87 double r, alfa, beta;
88 int cbord, bcint, p, q;
89 double cx;
90 {
91         extern  double sin(), cos(), sqrt();
92         extern  void pascal move(),  pascal hfill(), pascal pushxy(),
93                      pascal popxy(), pascal color(), pascal style();
94
95         float   alfj,qasp,pr,a,s,c;
96         float   alf(2+1);
97         long    p1,q1,p2,q2,p4,q4,u,v,w,d1;
98         int     i,j,j1,j2,j3,k,l,n,m,m1,m2,ri,x,y,py,px1,
99                 px2,sxi,sy1,sy2,xi,dj,dxj,dyj,inc1,
100                 pa,qa;
101         int     ix(2+1),iy(2+1),sx(2+1),sy(3+1),d(2+1),min(5+1),max(5+1),
102                 mx(2+1),mx1(2+1),incr1(2+1),incr2(2+1),ip(2+1),sj(2+1),
103                 sc(3+1),g(3+1,2+1),go(3+1,2+1);
104         logical bl(2+1),bxy(2+1),bp(3+1);
105         logical bc;
106         static int one = 1;
107
108         qasp=asp*FLOAT(q);
109         a=cx*r;
110         ri=INT(a);
111         pr=a*FLOAT(p);
112         q1=q*q;
113         q2=2*q1;
114         q4=2*q2;
115         p1=p*p;
116         p2=2*p1;
117         p4=2*p2;
118         for (i=1; i <= 3; i++)
119         {
120            bp(i)=FALSE;
121            for (j=1; j <= 2; j++)
122            {
123               go(i,j)=3;
124               g(i,j)=3;
125            }
126         }
127         sj(1)=1;
128         sj(2)=-1           ;
129         if(alfa == beta) {
130            n=2;
131            sc(1)=1;
132            sc(2)=-1;
133            bp(3)=TRUE;
134            bl(1)=TRUE;
135            bl(2)=TRUE;
136            goto L45;
137         }
138         alf(1)=alfa;
139         alf(2)=beta;
140
141         for (j=1; j <= 2; j++)
142         {
143            alfj=alf(j);
144            c=COS(alfj);
145            s=SIN(alfj);
146            a=pr/SQRT(sqr(p*c)+sqr(qasp*s));
147            m=INT(a*c);
148            sx(j)=isign(1,m);
149            ix(j)=m;
150            m=-INT(a*asp*s);
151            sy(j)=isign(1,m);
152            iy(j)=iabs(m);
153         }
154
155         sy1=sy(1);
156         sy2=sy(2);
157         sc(1)=sy1;
158         sc(2)=sy2;
159         if (sy1 >= 0) {
160            j1=1;
161            j2=2;
162         } else {
163            j1=2;
164            j2=1;
165         }
166
167         if (sy1 == sy2) {
168            m=sy1*(ix(2)-ix(1));
169            if (isign(1,m) > 0) {
170               n=1;
171               bp(2)=TRUE;
172               bp(3)=TRUE;
173               g(1,1)=j1;
174               g(1,2)=j2;
175               go(1,1)=j1;
176               go(1,2)=j2;
177            } else {
178               n=3;
179               sc(3)=-sy1;
180               g(1,2)=j2;
181               g(2,1)=j1;
182               go(1,2)=j2;
183               go(2,1)=j1;
184            }
185         } else {
186            n=2;
187            bp(3)=TRUE;
188            g(1,j1)=1;
189            g(2,j1)=2;
190            go(1,j1)=1;
191            go(2,j1)=2;
192         }
193
194         bl(1)=FALSE;
195         bl(2)=FALSE;
196
197         for (j=1; j <= 2; j++)
198         {
199            dxj=iabs(ix(j));
200            ix(j)=dxj;
201            dyj=iy(j);
202            ip(j)=0;
203            if (dyj > dxj) {
204               bxy(j)=TRUE;
205               m=2*dxj;
206               d(j)=m-dyj;
207               incr2(j)=2*(dxj-dyj);
208            } else {
209               bxy(j)=FALSE;
210               m=2*dyj;
211               d(j)=m-dxj;
212               incr2(j)=2*(dyj-dxj);
213            }
214            incr1(j)=m;
215         }
216
217
218  L45:   u=0;
219         v=ri*p4;
220         d1=q2-p1*(2*ri-1);
221         x=ri;
222         min(3)=x;
223         max(3)=x;
224         bc=FALSE;
225         y=0;
226
227
228  L300:  for (j=1; j <= 2; j++)
229         {
230            if (bl(j)) continue;
231            dj=d(j);
232            xi=ip(j);
233            min(j)=xi;
234            m=ix(j);
235            if (bxy(j)) {
236               max(j)=xi;
237               if (dj > 0) {
238                  d(j)=dj+incr2(j);
239                  if (xi >= m) bl(j)=TRUE;
240                  ip(j)=xi+1;
241               } else {
242                   d(j)=dj+incr1(j);
243               }
244            } else {
245               inc1=incr1(j);
246  L40:         if (dj > 0) {
247                  d(j)=dj+incr2(j);
248                  ip(j)=xi+1;
249                  max(j)=xi;
250               } else {
251                  dj=dj+inc1;
252                  xi=xi+1;
253                  if (xi >= m) {
254                     dj=1;
255                     bl(j)=TRUE;
256                  }
257                  goto L40;
258               }
259            }
260            if (y == iy(j)) bl(j)=TRUE;
261         }
262
263
264         for (i=1; i <= n; i++)
265         {
266            if (bp(i)) continue;
267            j1=g(i,1);
268            j2=g(i,2);
269            if (j1 != 3) {
270               j3=j1;
271               l=1;
272            } else {
273               j3=j2;
274               l=2;
275            }
276            j=j1+j2;
277            sy(3)=sc(i);
278            if ((j == 4 || j == 5) && bl(j3)) {
279               m=ix(j3);
280               if (sy(j3) == sj(j3)*sx(j3)) {
281                  bp(i)=TRUE;
282                  min(j)=m;
283                  max(j)=max0(max(3),m);
284                  if (j1 != 3) {
285                     j2=j;
286                  } else {
287                     j1=j;
288                  }
289               } else {
290                  g(i,l)=3;
291                  sc(i)=sy(j3);
292                  min(j3)=min0(min(j3),min(3));
293               }
294            } else {
295               if (j == 3) {
296                  for (l=1; l <= 2; l++)
297                  {
298                     j=g(i,l);
299                     if (bl(j)) {
300                        g(i,l)=3;
301                        sc(i)=sy(j);
302                        min(j)=min0(min(j),min(3));
303                     }
304                  }
305               }
306            }
307
308            j=j1;
309            for (l=1; l <= 2; l++)
310            {
311               m1=max(j);
312               m2=min(j);
313               if (j < 3) {
314                  sxi=sx(j);
315               } else {
316                  sxi=-sj(l);
317                  if (j == 3) {
318                     k=go(i,l);
319                     if (k < 3) {
320                        m1=min0(m1,max(k));
321                        m2=min0(m1,m2);
322                     }
323                  }
324               }
325               if (sxi > 0) {
326                  mx(l)=ir+m2;
327                  mx1(l)=ir+m1;
328               } else {
329                  mx(l)=ir-m1;
330                  mx1(l)=ir-m2;
331               }
332               j=j2;
333            }
334
335            py=jr+sy(j3)*y;
336            if (bcint != 0) {
337               px1=mx1(1)+1;
338               px2=mx(2)-1;
339               if (px1 <= px2) {
340                  move(&px1,&py);
341                  hfill(&px2);
342               }
343            }
344            pushxy();
345            color(&cbord);
346            style(&one);
347            move(&mx(1),&py);
348            hfill(&mx1(1));
349            move(&mx(2),&py);
350            hfill(&mx1(2)) ;
351            popxy();
352         }
353
354         if (x == 0) return;
355
356
357         if (bp(1) && bp(2) && bp(3)) return;
358         if (bc) goto L240;
359
360         u=u+q4;
361         if (d1 < 0) {
362            d1=d1+u+q2;
363         } else {
364            v=v-p4;
365            w=u-v;
366            if (w > 0) {
367               bc=TRUE;
368               w=w/2-v;
369            }
370            d1=d1+w+q2;
371            x=x-1;
372         }
373         y=y+1;
374         min(3)=x;
375         max(3)=x;
376         if (bc) goto L250;
377         goto L300;
378
379  L240:  max(3)=x;
380         y=y+1;
381  L250:  v=v-p4;
382         x=x-1;
383         if (d1 > 0) {
384            d1=d1-v+p2;
385         } else {
386            u=u+q4;
387            d1=d1+u-v+p2;
388            min(3)=x+1;
389            goto L300;
390         }
391         if (x > 0) goto L250;
392         min(3)=x;
393         goto L300;
394 }
395 \1a
396 \r