Debian Codegraph @

utils/axodraw2/axodraw2-src/axohelp.c

raw
1 /*
2
3 Need to do:
4 1. Fix reporting of number of object to in-file number
5 2. Or Don't require object 0 for ID line
6
7 #[ License :
8
9 (C) 2016-2019 by authors:
10 John Collins (jcc8 at psu dot edu)
11 Jos Vermaseren (t68 at nikhef dot nl)
12
13 axohelp is free software: you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free
15 Software Foundation, either version 3 of the License, or (at your option)
16 any later version.
17
18 axohelp is distributed in the hope that it will be useful, but WITHOUT ANY
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 details.
22
23 For the GNU General Public License see <http://www.gnu.org/licenses/>.
24
25 #] License :
26 #[ Commentary + Modifications :
27
28 This file contains the source code of the axohelp program that is used
29 together with axopdf.sty and pdflatex. It is a conversion of the postscript
30 code of axodraw.sty.
31
32 input file contains objects of the type
33 [number] axohelp input
34 the output contains objects:
35 \axo@setObject{label}%
36 {input data}%
37 {output}
38 There may be blank lines and commentary.
39
40 #[ About folds : (this line starts with one blank and two tabs)
41
42 The internals of the file have been organized in folds.
43 These are defined as a range of lines if which the first and last
44 lines have a special format. Each starts with any three characters
45 (may include tabs), then #[ for the start line and #] for the closing
46 line, then both lines need identical name fields, closed by a colon.
47 After the colon can be anything. When a fold is closed one should see
48 only the first line but with the #[ replaced by ## as in
49 ## About folds : (this line starts with one blank and two tabs)
50 Folds can be nested.
51 This fold concept comes originally from the occam compiler for the
52 transputer in the second half of the 1980's although there it was
53 implemented differently. It was taken over by the STedi editor in its
54 current form. The sources of this editor are available from the form
55 home site: http://www.nikhef.nl/~form
56 Some people have managed to emulate these folds in editors like emacs
57 and vim.
58
59 #] About folds :
60 #] Commentary + Modifications :
61 #[ Includes :
62 */
63
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <math.h>
67 #include <string.h>
68 #include <ctype.h>
69
70 /*
71 #] Includes :
72 #[ Defines :
73 */
74
75 #define NAME "axohelp"
76 #define VERSIONDATE "2021 Mar 5"
77 #define VERSION 1
78 #define SUBVERSION 4
79
80 #define COMMENTCHAR '%'
81 #define TERMCHAR ';'
82
83 #define STROKING 0
84 #define NONSTROKING 1
85
86 char **inputs;
87 long *inputsizes;
88 double **inargs;
89 long numinputs = 0;
90 long inputallocations = 0;
91 char *axohelp;
92 FILE *outfile;
93 char *inname, *outname;
94 int VerboseFlag = 0;
95 char outputbuffer[1000000];
96 char *outpos;
97 long numobject;
98 char *nameobject;
99 int witharrow = 0;
100 int identification = 0;
101
102 static int lastlinefeed = 1;
103 static double axolinewidth = 0.5;
104 static struct aRRow {
105 double stroke;
106 double width;
107 double length;
108 double inset;
109 double where;
110 double scale;
111 double aspect;
112 int type; /* 0: old style arrow; 1: Jaxodraw style arrow */
113 int adjust; /* whether the line length should be adjusted */
114 } arrow;
115 double linesep = 0;
116 int flip = 0;
117 int clockwise = 0;
118
119 void CleanupOutput(char *str);
120 void send( char* str );
121 void sendClean( char* str );
122
123 void ArrowHead();
124 void GetArrow(double *);
125 void BezierArrow(double *);
126 void ArcSegment(double,double,double);
127 double *ReadArray(char *,int *,int *);
128 double *ReadTail(char *,int *);
129 double LengthBezier(double,double,double,double,double,double,double);
130
131 double M_pi;
132 double torad;
133 #define COS(x) cos((x)*torad)
134 #define SIN(x) sin((x)*torad)
135 #define TAN(x) tan((x)*torad)
136
137 typedef void (*TFUN)(double *);
138
139 typedef struct {
140 char *name;
141 TFUN func;
142 int numargs;
143 int colortype;
144 } KEYWORD;
145
146 double mod(double,int);
147
148 void GluonHelp(double *,double);
149 void DoubleGluonHelp(double *,double,double,double,double);
150 void GluonCircHelp(double *);
151 void GluonArcHelp(double *,double,double);
152 void PhotonHelp(double *,double);
153 void PhotonArcHelp(double *,double,int);
154 void ZigZagHelp(double *,double);
155 void ZigZagArcHelp(double *);
156 double ComputeDash(double *,double,double);
157 double ComputeDashCirc(double *,double);
158
159 void ArrowArc(double *);
160 void ArrowArcn(double *);
161 void ArrowDoubleArc(double *);
162 void ArrowLine(double *);
163 void ArrowDoubleLine(double *);
164 void AxoArc(double *);
165 void AxoBezier(double *);
166 void AxoGluon(double *);
167 void AxoGluonArc(double *);
168 void AxoGluonCirc(double *);
169 void AxoLine(double *);
170 void AxoPhoton(double *);
171 void AxoPhotonArc(double *);
172 void AxoZigZag(double *);
173 void AxoZigZagArc(double *);
174 void BezierCurve(double *);
175 void Boxc(double *);
176 void CArc(double *);
177 void DashArrowArc(double *);
178 void DashArrowArcn(double *);
179 void DashArrowLine(double *);
180 void DashArrowDoubleArc(double *);
181 void DashArrowDoubleLine(double *);
182 void DashBezier(double *);
183 void DashCArc(double *);
184 void DashDoubleArc(double *);
185 void DashDoubleBezier(double *);
186 void DashDoubleLine(double *);
187 void DashDoublePhoton(double *);
188 void DashDoublePhotonArc(double *);
189 void DashDoubleZigZag(double *);
190 void DashDoubleZigZagArc(double *);
191 void DashGluon(double *);
192 void DashGluonArc(double *);
193 void DashGluonCirc(double *);
194 void DashPhoton(double *);
195 void DashPhotonArc(double *);
196 void DashZigZag(double *);
197 void DashZigZagArc(double *);
198 void DashLine(double *);
199 void DoubleArc(double *);
200 void DoubleBezier(double *);
201 void DoubleLine(double *);
202 void DoublePhoton(double *);
203 void DoublePhotonArc(double *);
204 void DoubleZigZag(double *);
205 void DoubleZigZagArc(double *);
206 void EBox(double *);
207 void EBoxc(double *);
208 void ECirc(double *);
209 void ETri(double *);
210 void FBox(double *);
211 void FBoxc(double *);
212 void FOval(double *);
213 void FTri(double *);
214 void GluonArc(double *);
215 void GluonCirc(double *);
216 void Gluon(double *);
217 void Grid(double *);
218 void LinAxis(double *);
219 void Line(double *);
220 void LogAxis(double *);
221 void Oval(double *);
222 void Photon(double *);
223 void PhotonArc(double *);
224 void Rotate(double *);
225 void Vertex(double *);
226 void ZigZag(double *);
227 void ZigZagArc(double *);
228
229 void Curve(double *,int);
230 void DashCurve(double *,int);
231 void Polygon(double *,int,int);
232
233 void Inivars(void);
234
235 KEYWORD commands[] = {
236 { "Line", Line, 4, STROKING }
237 ,{ "ArrowArc", ArrowArc, 7, STROKING }
238 ,{ "ArrowArcn", ArrowArcn, 7, STROKING }
239 ,{ "ArrowDoubleArc", ArrowDoubleArc, 8, STROKING }
240 ,{ "ArrowLine", ArrowLine, 6, STROKING }
241 ,{ "ArrowDoubleLine", ArrowDoubleLine, 7, STROKING }
242 ,{ "AxoArc", AxoArc, 17, STROKING }
243 ,{ "AxoBezier", AxoBezier, 19, STROKING }
244 ,{ "AxoGluon", AxoGluon, 8, STROKING }
245 ,{ "AxoGluonArc", AxoGluonArc, 10, STROKING }
246 ,{ "AxoGluonCirc", AxoGluonCirc, 8, STROKING }
247 ,{ "AxoLine", AxoLine, 15, STROKING }
248 ,{ "AxoPhoton", AxoPhoton, 8, STROKING }
249 ,{ "AxoPhotonArc", AxoPhotonArc, 10, STROKING }
250 ,{ "AxoZigZag", AxoZigZag, 8, STROKING }
251 ,{ "AxoZigZagArc", AxoZigZagArc, 10, STROKING }
252 ,{ "Bezier", BezierCurve, 8, STROKING }
253 ,{ "Boxc", Boxc, 4, STROKING }
254 ,{ "CArc", CArc, 5, STROKING }
255 ,{ "DashArrowArc", DashArrowArc, 8, STROKING }
256 ,{ "DashArrowArcn", DashArrowArcn, 8, STROKING }
257 ,{ "DashArrowDoubleArc", DashArrowDoubleArc, 9, STROKING }
258 ,{ "DashArrowDoubleLine",DashArrowDoubleLine,8, STROKING }
259 ,{ "DashArrowLine", DashArrowLine, 7, STROKING }
260 ,{ "DashBezier", DashBezier, 9, STROKING }
261 ,{ "DashCArc", DashCArc, 6, STROKING }
262 ,{ "DashDoubleArc", DashDoubleArc, 7, STROKING }
263 ,{ "DashDoubleBezier", DashDoubleBezier, 10, STROKING }
264 ,{ "DashDoubleLine", DashDoubleLine, 6, STROKING }
265 ,{ "DashDoublePhoton", DashDoublePhoton, 8, STROKING }
266 ,{ "DashDoublePhotonArc",DashDoublePhotonArc,10, STROKING }
267 ,{ "DashDoubleZigZag", DashDoubleZigZag, 8, STROKING }
268 ,{ "DashDoubleZigZagArc",DashDoubleZigZagArc,10, STROKING }
269 ,{ "DashGluon", DashGluon, 7, STROKING }
270 ,{ "DashGluonArc", DashGluonArc, 10, STROKING }
271 ,{ "DashGluonCirc", DashGluonCirc, 7, STROKING }
272 ,{ "DashLine", DashLine, 5, STROKING }
273 ,{ "DashPhoton", DashPhoton, 7, STROKING }
274 ,{ "DashPhotonArc", DashPhotonArc, 10, STROKING }
275 ,{ "DashZigZag", DashZigZag, 7, STROKING }
276 ,{ "DashZigZagArc", DashZigZagArc, 10, STROKING }
277 ,{ "DoubleArc", DoubleArc, 6, STROKING }
278 ,{ "DoubleBezier", DoubleBezier, 9, STROKING }
279 ,{ "DoubleLine", DoubleLine, 5, STROKING }
280 ,{ "DoublePhoton", DoublePhoton, 7, STROKING }
281 ,{ "DoublePhotonArc", DoublePhotonArc, 8, STROKING }
282 ,{ "DoubleZigZag", DoubleZigZag, 7, STROKING }
283 ,{ "DoubleZigZagArc", DoubleZigZagArc, 8, STROKING }
284 ,{ "EBox", EBox, 4, STROKING }
285 ,{ "FBox", FBox, 4, STROKING }
286 ,{ "FBoxc", FBoxc, 4, STROKING }
287 ,{ "ECirc", ECirc, 3, STROKING }
288 ,{ "ETri", ETri, 6, STROKING }
289 ,{ "FOval", FOval, 5, NONSTROKING }
290 ,{ "FTri", FTri, 6, NONSTROKING }
291 ,{ "GluonArc", GluonArc, 7, STROKING }
292 ,{ "GluonCirc", GluonCirc, 6, STROKING }
293 ,{ "Gluon", Gluon, 6, STROKING }
294 ,{ "Grid", Grid, 6, STROKING }
295 ,{ "LinAxis", LinAxis, 8, STROKING }
296 ,{ "LogAxis", LogAxis, 7, STROKING }
297 ,{ "Oval", Oval, 5, STROKING }
298 ,{ "Photon", Photon, 6, STROKING }
299 ,{ "PhotonArc", PhotonArc, 7, STROKING }
300 ,{ "Rotate", Rotate, 7, NONSTROKING }
301 ,{ "Vertex", Vertex, 3, NONSTROKING }
302 ,{ "ZigZag", ZigZag, 6, STROKING }
303 ,{ "ZigZagArc", ZigZagArc, 7, STROKING }
304 };
305
306 /*
307 #] Defines :
308 #[ SetDefaults :
309 */
310
311 void SetDefaults()
312 {
313 lastlinefeed = 1;
314 axolinewidth = 0.5;
315 linesep = 0;
316 flip = 0;
317 clockwise = 0;
318 witharrow = 0;
319 }
320
321 /*
322 #] SetDefaults :
323 #[ mod :
324 */
325
326 double mod( double x, int n ) {
327 /* Return x mod n, with positive returned value in range 0<=result<n.
328 (up to rounding error).
329 */
330 x = fmod(x, n);
331 if (x >= n) { x -= n; }
332 if (x < 0) { x += n; }
333 return x;
334 }
335
336 /*
337 #] mod :
338 #[ PDF utilities :
339
340 These routines are included to make the program more readable and easier
341 to write. It also allows the easy use of the OutputString?? OLD name routine that
342 compactifies the output.
343 */
344
345 #define Stroke send(" S")
346 #define CloseAndStroke send(" h S")
347 #define Fill send(" f")
348 #define CloseAndFill send(" h f")
349 #define SaveGraphicsState send(" q")
350 #define RestoreGraphicsState send(" Q")
351
352 void Bezier(double x1,double y1,double x2,double y2,double x3,double y3) {
353 sprintf(outputbuffer,"\n %12.3f %12.3f %12.3f %12.3f %12.3f %12.3f c",x1,y1,x2,y2,x3,y3);
354 sendClean(outputbuffer);
355 }
356
357 void LineTo(double x1,double y1) {
358 sprintf(outputbuffer,"\n %12.3f %12.3f l",x1,y1);
359 sendClean(outputbuffer);
360 }
361
362 void MoveTo(double x1,double y1) {
363 sprintf(outputbuffer,"\n %12.3f %12.3f m",x1,y1);
364 sendClean(outputbuffer);
365 }
366
367 void SetLineWidth(double w) {
368 sprintf(outputbuffer," %12.3f w",w);
369 sendClean(outputbuffer);
370 }
371
372 void SetDashSize(double dashsize,double phase) {
373 if ( dashsize ) {
374 sprintf(outputbuffer," [%12.3f] %12.3f d",dashsize,phase);
375 sendClean(outputbuffer);
376 }
377 else send(" [] 0 d");
378 }
379
380 void SetTransferMatrix(double x11,double x12,double x21,double x22,double x,double y)
381 {
382 if ( ( fabs(x11-1.) > 0.001 ) || ( fabs(x22-1.) > 0.001 )
383 || ( fabs(x12) > 0.001 ) || ( fabs(x21) > 0.001 )
384 || ( fabs(x) > 0.001 ) || ( fabs(y) > 0.001 ) ) {
385 sprintf(outputbuffer,"%12.3f %12.3f %12.3f %12.3f %12.3f %12.3f cm\n",x11,x12,x21,x22,x,y);
386 sendClean(outputbuffer);
387 }
388 }
389
390 static double BzK;
391
392 void BezierCircle(double r,char *action)
393 {
394 char *outpos = outputbuffer;
395 outpos +=
396 sprintf(outpos," %12.3f 0 m %12.3f %12.3f %12.3f %12.3f 0 %12.3f c\n",-r,-r,r*BzK,-r*BzK,r,r);
397 outpos +=
398 sprintf(outpos," %12.3f %12.3f %12.3f %12.3f %12.3f 0 c\n",r*BzK,r,r,r*BzK,r);
399 outpos +=
400 sprintf(outpos," %12.3f %12.3f %12.3f %12.3f 0 %12.3f c\n",r,-r*BzK,r*BzK,-r,-r);
401 outpos +=
402 sprintf(outpos," %12.3f %12.3f %12.3f %12.3f %12.3f 0 c %s\n",-r*BzK,-r,-r,-r*BzK,-r,action);
403 sendClean(outputbuffer);
404 }
405
406 void BezierOval(double w, double h, char *action)
407 {
408 char *outpos = outputbuffer;
409 outpos +=
410 sprintf(outpos," %12.3f 0 m %12.3f %12.3f %12.3f %12.3f 0 %12.3f c\n",-w,-w,h*BzK,-w*BzK,h,h);
411 outpos +=
412 sprintf(outpos," %12.3f %12.3f %12.3f %12.3f %12.3f 0 c\n",w*BzK,h,w,h*BzK,w);
413 outpos +=
414 sprintf(outpos," %12.3f %12.3f %12.3f %12.3f 0 %12.3f c\n",w,-h*BzK,w*BzK,-h,-h);
415 outpos +=
416 sprintf(outpos," %12.3f %12.3f %12.3f %12.3f %12.3f 0 c %s\n",-w*BzK,-h,-w,-h*BzK,-w,action);
417 sendClean(outputbuffer);
418 }
419
420 void SetGray(double grayscale,int par)
421 {
422 if ( par == STROKING ) {
423 sprintf(outputbuffer," %12.3f G",grayscale);
424 }
425 else {
426 sprintf(outputbuffer," %12.3f g",grayscale);
427 }
428 sendClean(outputbuffer);
429 }
430
431 void SetColor(double c, double m, double y, double k,int par)
432 {
433 if ( par == STROKING ) {
434 sprintf(outputbuffer," %12.3f %12.3f %12.3f %12.3f K",c,m,y,k);
435 }
436 else {
437 sprintf(outputbuffer," %12.3f %12.3f %12.3f %12.3f k",c,m,y,k);
438 }
439 sendClean(outputbuffer);
440 }
441
442 void SetBackgroundColor(int par)
443 {
444 if ( par == STROKING ) { send(" 0 0 0 0 K"); }
445 else { send(" 0 0 0 0 k"); }
446 }
447
448 void Rectangle(double x,double y,double w,double h) {
449 sprintf(outputbuffer,"\n %12.3f %12.3f %12.3f %12.3f re",x,y,w,h);
450 sendClean(outputbuffer);
451 }
452
453 void Triangle(double x1,double y1,double x2,double y2,double x3,double y3) {
454 sprintf(outputbuffer,"\n %12.3f %12.3f m %12.3f %12.3f l %12.3f %12.3f l h",x1,y1,x2,y2,x3,y3);
455 sendClean(outputbuffer);
456 }
457
458 /*
459 #] PDF utilities :
460 #[ Service routines :
461 #[ GetArrow :
462 */
463
464 void GetArrow(double *args)
465 {
466 witharrow = args[7];
467 arrow.stroke = args[0];
468 arrow.width = args[1];
469 arrow.length = args[2];
470 arrow.inset = args[3];
471 arrow.scale = args[4];
472 arrow.aspect = args[5];
473 arrow.where = args[6];
474 arrow.type = 1;
475 arrow.adjust = 0;
476 if ( args[8] ) flip = 1;
477 if ( witharrow ) {
478 if ( arrow.length == 0 && arrow.width == 0 ) {
479 arrow.width = (axolinewidth + 0.7*linesep + 1 ) * 1.2;
480 if (arrow.width < 2.5) arrow.width = 2.5;
481 arrow.length = 2*arrow.width*arrow.aspect;
482 }
483 else if ( arrow.width == 0 ) {
484 arrow.width = arrow.length/(2*arrow.aspect);
485 }
486 else if ( arrow.length == 0 ) {
487 arrow.length = 2*arrow.width*arrow.aspect;
488 }
489 arrow.width *= arrow.scale;
490 arrow.length *= arrow.scale;
491 if ( arrow.where > 1 ) { arrow.where = 1; arrow.adjust = 1; }
492 if ( arrow.where < 0 ) { arrow.where = 0; arrow.adjust = 1; }
493 }
494 }
495
496 /*
497 #] GetArrow :
498 #[ ArrowHead :
499
500 Places an arrowhead of a given size at 0 in the +x direction
501 The size: Full width is 2*size and full length is also 2*size.
502 */
503
504 void ArrowHead()
505 /*
506 Jaxodraw style arrows
507 */
508 {
509 int k;
510 double length;
511 SaveGraphicsState;
512 if ( flip ) length = -arrow.length;
513 else length = arrow.length;
514 SetDashSize(0,0);
515 if ( arrow.stroke ) {
516 SetLineWidth(arrow.stroke);
517 for (k = 1; k <= 2; k++ ) {
518 SaveGraphicsState;
519 MoveTo(length*0.5,0);
520 LineTo(-length*0.5,arrow.width);
521 LineTo(-length*0.5+length*arrow.inset,0);
522 LineTo(-length*0.5,-arrow.width);
523 if (k == 1) {
524 SetBackgroundColor(NONSTROKING);
525 send(" h f");
526 }
527 else {
528 send(" s");
529 }
530 RestoreGraphicsState;
531 }
532 }
533 else {
534 MoveTo(length*0.5,0);
535 LineTo(-length*0.5,arrow.width);
536 LineTo(-length*0.5+length*arrow.inset,0);
537 LineTo(-length*0.5,-arrow.width);
538 send(" h f");
539 }
540 RestoreGraphicsState;
541 }
542
543 /*
544 #] ArrowHead :
545 #[ BezierArrow :
546
547 We compute the length of the curve.
548 Then we try to find the t value for which holds:
549 Length(t)/Length(1) = arrow.where
550 */
551
552 void BezierArrow(double *args)
553 {
554 double t,u,x,y,dx,dy,dr,len,tlen,tmin=0,tmax=1.0;
555 double x1=args[2]-args[0],x2=args[4]-args[0],x3=args[6]-args[0];
556 double y1=args[3]-args[1],y2=args[5]-args[1],y3=args[7]-args[1];
557 len = LengthBezier(x1,y1,x2,y2,x3,y3,1.0);
558 t = arrow.where;
559 tlen = LengthBezier(x1,y1,x2,y2,x3,y3,t);
560 while ( fabs(tlen/len-arrow.where) > 0.0001 ) {
561 if ( tlen/len > arrow.where ) {
562 tmax = t;
563 t = 0.5*(tmin+t);
564 }
565 else {
566 tmin = t;
567 t = 0.5*(tmax+t);
568 }
569 tlen = LengthBezier(x1,y1,x2,y2,x3,y3,t);
570 }
571 u = 1-t;
572 x = args[0]*u*u*u+(3*args[2]*u*u+(3*args[4]*u+args[6]*t)*t)*t;
573 y = args[1]*u*u*u+(3*args[3]*u*u+(3*args[5]*u+args[7]*t)*t)*t;
574 dx = 3*(-args[0]*u*u+args[2]*u*(1-3*t)+args[4]*t*(2-3*t)+args[6]*t*t);
575 dy = 3*(-args[1]*u*u+args[3]*u*(1-3*t)+args[5]*t*(2-3*t)+args[7]*t*t);
576 dr = sqrt(dx*dx+dy*dy);
577 SetTransferMatrix(1,0,0,1,x,y);
578 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
579 ArrowHead();
580 }
581
582 /*
583 #] BezierArrow :
584 #[ ArcSegment :
585 */
586
587 void ArcSegment(double r, double phi1, double dphi)
588 {
589 double xphi, d, x, y, phia, phib, x1,y1,x2,y2,x3,y3;
590 int num, i;
591
592 num = dphi/90.0001+1.; /* number of segments we should use. */
593 xphi = dphi/num; /* arc size of each segment */
594 d = 4.*TAN(xphi/4.)/3.; /* the magic distance for the control points */
595
596 x = r*COS(phi1); y = r*SIN(phi1);
597 MoveTo(x,y);
598 for ( i = 0; i < num; i++ ) {
599 phia = phi1+i*xphi; /* Start of segment */
600 phib = phia+xphi; /* End of segment */
601 x3 = r*COS(phib); y3 = r*SIN(phib);
602 x1 = x - y*d; y1 = y + x*d;
603 x2 = x3+y3*d; y2 = y3-x3*d;
604
605 Bezier(x1,y1,x2,y2,x3,y3);
606
607 x = x3; y = y3;
608 }
609 }
610
611 /*
612 #] ArcSegment :
613 #[ ReadNumber :
614 */
615
616 char *ReadNumber(char *s,double *num)
617 {
618 double x3,minus = 1;
619 int x1,x2;
620 while ( *s == '+' || *s == '-' ) {
621 if ( *s == '-' ) minus = -minus;
622 s++;
623 }
624 x1 = x2 = 0; x3 = 1;
625 while ( *s <= '9' && *s >= '0' ) { x1 = 10*x1 + *s++ - '0'; }
626 if ( *s == 0 ) { *num = x1*minus; return(s); }
627 if ( *s == '.' ) {
628 s++;
629 while ( *s >= '0' && *s <= '9' ) { x2 = 10*x2 + *s++ - '0'; x3 *= 10; }
630 }
631 *num = minus*((double)x1 + x2/x3);
632 return(s);
633 }
634
635 /*
636 #] ReadNumber :
637 #[ ReadArray :
638
639 Reads a tail of floats of the type (x1,y1)(x2,y2),...,(xn,yn) or
640 (x1,y1)(x2,y2),...,(xn,yn) f1 ... fm
641 The floats may be written as integers.
642 Normally they are in the format ####.###
643 In num1 we return the number of coordinates encountered.
644 In num2 we return the number of extra floats encountered.
645 The array of the return value should be freed after it has been used.
646 */
647
648 double *ReadArray(char *inbuf, int *num1, int *num2)
649 {
650 int argsize = 0, newsize, num = 0, i;
651 double *args = 0, *newargs = 0, *extraargs, x, y;
652 char *s;
653 *num2 = 0;
654 s = inbuf;
655 while ( *s == ' ' || *s == '\t' || *s == '\n' ) s++;
656 if ( *s == '"' ) {
657 s++;
658 while ( *s == ' ' || *s == '\t' || *s == '\n' ) s++;
659 }
660 while ( *s == '(' ) { /* We need to read (x,y) */
661 s++;
662 while ( *s == ' ' || *s == '\n' || *s == '\t' ) s++;
663 s = ReadNumber(s,&x);
664 while ( *s == ' ' ) s++;
665 if ( *s != ',' ) {
666 fprintf(stderr,"%s: Illegal format for array of numbers in object %ld of type %s in file %s\n"
667 ,axohelp,numobject,nameobject,inname);
668 free(args);
669 return(0);
670 }
671 s++;
672 while ( *s == ' ' || *s == '\n' || *s == '\t' ) s++;
673 s = ReadNumber(s,&y);
674 while ( *s == ' ' || *s == '\n' || *s == '\t' ) s++;
675 if ( *s != ')' ) {
676 fprintf(stderr,"%s: Illegal format for array of numbers in object %ld of type %s in file %s\n"
677 ,axohelp,numobject,nameobject,inname);
678 free(args);
679 return(0);
680 }
681 s++;
682 while ( *s == ' ' ) s++;
683 num += 2;
684 if ( num >= argsize ) {
685 if ( argsize == 0 ) newsize = 20;
686 else newsize = 2*argsize;
687 newargs = (double *)malloc(sizeof(double)*newsize);
688 if (! newargs ) {
689 fprintf(stderr,"%s: Memory allocation error while reading file %s\n",axohelp,inname);
690 free(args);
691 return(0);
692 }
693 if ( args == 0 ) { args = newargs; argsize = newsize; }
694 else {
695 for ( i = 0; i < argsize; i++ ) newargs[i] = args[i];
696 free(args);
697 args = newargs; argsize = newsize;
698 }
699 }
700 args[num-2] = x; args[num-1] = y;
701 }
702 if ( *s == '"' ) s++;
703 while ( *s == ' ' || *s == '\t' || *s == '\n' ) s++;
704 *num1 = num/2;
705
706 if ( *s ) {
707 if ( ( extraargs = ReadTail(s,num2) ) == 0 ) {
708 return(0);
709 }
710 if ( num+*num2 > argsize ) {
711 newargs = (double *)malloc(sizeof(double)*(num+*num2));
712 if (! newargs ) {
713 fprintf(stderr,"%s: Memory allocation error while reading file %s\n",axohelp,inname);
714 free(args);
715 return(0);
716 }
717 for ( i = 0; i < num; i++ ) newargs[i] = args[i];
718 free(args);
719 args = newargs;
720 argsize = num+*num2;
721 }
722 for ( i = 0; i < *num2; i++ ) args[num+i] = extraargs[i];
723 free(extraargs);
724 }
725 return(args);
726 }
727
728 /*
729 #] ReadArray :
730 #[ ReadTail :
731
732 Reads a command tail that consists of floating point numbers in the
733 notation xxx.yyy or just as integers without even the decimal point.
734 The output array is allocated and should be returned in a well behaved
735 program.
736 */
737
738 double *ReadTail(char *buff,int *number)
739 {
740 char *s;
741 int num = 1, i;
742 double *outargs;
743 s = buff;
744 while ( *s ) {
745 if ( *s == ' ' || *s == '\t' || *s == '\n' ) {
746 num++; *s++ = 0;
747 while ( *s == ' ' || *s == '\t' || *s == '\n' ) *s++ = 0;
748 }
749 else s++;
750 }
751 outargs = (double *)malloc(num*sizeof(double));
752 if (! outargs ) {
753 fprintf(stderr,"%s: Memory allocation error while reading file %s\n",axohelp,inname);
754 return(0);
755 }
756 s = buff;
757 for ( i = 0; i < num; i++ ) {
758 while ( *s == 0 ) s++;
759 s = ReadNumber(s,outargs+i);
760 if ( *s == 'p' && s[1] == 't' ) s += 2;
761 if ( *s != 0 ) {
762 fprintf(stderr,"%s: Illegal format for number in command %ld (%s) in file %s.\n"
763 ,axohelp,numobject,nameobject,inname);
764 free(outargs);
765 return(0);
766 }
767 }
768 *number = num;
769 return(outargs);
770 }
771
772 /*
773 #] ReadTail :
774 #[ DoCurve :
775
776 Interpolation curve expressed as a Bezier curve.
777 */
778
779 void DoCurve(double x0, double y0, double x1, double y1,
780 double x2, double y2, double x3, double y3)
781 {
782 double xx1, yy1, xx2, yy2;
783 xx1 = (2*x1+x2)/3;
784 yy1 = ((y1-y0)/(x1-x0)*(x2-x0)+(y2-y0)/(x2-x0)*(x1-x0)+y1+2*y0)/3;
785 xx2 = (x1+2*x2)/3;
786 yy2 = ((y2-y3)/(x2-x3)*(x1-x3)+(y1-y3)/(x1-x3)*(x2-x3)+y2+2*y3)/3;
787
788 Bezier(xx1,yy1,xx2,yy2,x2,y2);
789 }
790
791 /*
792 #] DoCurve :
793 #[ LengthBezier :
794
795 Routine computes the length of a Bezier curve.
796 Method:
797 x = x0*(1-t)^3+3*x1*t*(1-t)^2+3*x2*t^2*(1-t)+x3*t^3
798 y = y0*(1-t)^3+3*y1*t*(1-t)^2+3*y2*t^2*(1-t)+y3*t^3
799 We assume that x0=y0=0. (Hence call with x1-x0 etc)
800 --> dx/dt = 3*x1*(1-t)*(1-3*t)+3*x2*t*(2-3*t)+3*x3*t^2
801 = 3*(x1+2*t*(x2-2*x1)+t^2*(x3-3*x2+3*x1))
802 --> L = int_0^1 dt * sqrt(dx^2+dy^2)
803 We use ordinary Gaussian quadratures over the domain -1,...,+1
804
805 We have here quadratures for 8, 16 or 32 points.
806 For the moment we use the 16 point quadrature. It seems to work well.
807 The numbers were taken from Abramowitz and Stegun.
808 */
809
810 typedef struct quad {
811 double x;
812 double w;
813 } QUAD;
814
815 QUAD g8[4] = {
816 { 0.183434642495650, 0.362683783378362 }
817 ,{ 0.525532409916329, 0.313706645877887 }
818 ,{ 0.796666477413627, 0.222381034453374 }
819 ,{ 0.960289856497536, 0.101228536290376 }
820 };
821 QUAD g16[8] = {
822 { 0.095012509837637440185, 0.189450610455068496285 }
823 ,{ 0.281603550779258913230, 0.182603415044923588867 }
824 ,{ 0.458016777657227386342, 0.169156519395002538189 }
825 ,{ 0.617876244402643748447, 0.149595988816576732081 }
826 ,{ 0.755404408355003033895, 0.124628971255533872052 }
827 ,{ 0.865631202387831743880, 0.095158511682492784810 }
828 ,{ 0.944575023073232576078, 0.062253523938647892863 }
829 ,{ 0.989400934991649932596, 0.027152459411754094852 }
830 };
831 QUAD g32[16] = {
832 { 0.048307665687738316235, 0.096540088514727800567 }
833 ,{ 0.144471961582796493485, 0.095638720079274859419 }
834 ,{ 0.239287362252137074545, 0.093844399080804565639 }
835 ,{ 0.331868602282127649780, 0.091173878695763884713 }
836 ,{ 0.421351276130635345364, 0.087652093004403811143 }
837 ,{ 0.506899908932229390024, 0.083311924226946755222 }
838 ,{ 0.587715757240762329041, 0.078193895787070306472 }
839 ,{ 0.663044266930215200975, 0.072345794108848506225 }
840 ,{ 0.732182118740289680387, 0.065822222776361846838 }
841 ,{ 0.794483795967942406963, 0.058684093478535547145 }
842 ,{ 0.849367613732569970134, 0.050998059262376176196 }
843 ,{ 0.896321155766052123965, 0.042835898022226680657 }
844 ,{ 0.934906075937739689171, 0.034273862913021433103 }
845 ,{ 0.964762255587506430774, 0.025392065309262059456 }
846 ,{ 0.985611511545268335400, 0.016274394730905670605 }
847 ,{ 0.997263861849481563545, 0.007018610009470096600 }
848 };
849
850 double LengthBezier(double x1,double y1,double x2,double y2,double x3,double y3,double tmax)
851 {
852 double xa = 3*x1, xb = 6*(x2-2*x1), xc = 3*(x3-3*x2+3*x1);
853 double ya = 3*y1, yb = 6*(y2-2*y1), yc = 3*(y3-3*y2+3*y1);
854 double t, sum = 0, dx, dy;
855 int j;
856
857 for ( j = 0; j < 8; j++ ) {
858 t = 0.5*(1+g16[j].x)*tmax;
859 dx = xa+t*(xb+t*xc);
860 dy = ya+t*(yb+t*yc);
861 sum += 0.5*g16[j].w*sqrt(dx*dx+dy*dy);
862 t = 0.5*(1-g16[j].x)*tmax;
863 dx = xa+t*(xb+t*xc);
864 dy = ya+t*(yb+t*yc);
865 sum += 0.5*g16[j].w*sqrt(dx*dx+dy*dy);
866 }
867 return(sum*tmax);
868 }
869
870 /*
871 #] LengthBezier :
872 #] Service routines :
873 #[ ScanForObjects :
874
875 Routine reads the input buffer and sets up pointers to the commands.
876 Basically it is responsible for checking the input syntax and making
877 sure all commands will have the proper number of arguments.
878 */
879
880 long ScanForObjects(char *buffer)
881 {
882 char *s = buffer, *t;
883 if ( *s == COMMENTCHAR ) { while ( *s && *s != '\n' ) s++; }
884 while ( *s ) { /* here we are to look for a new command */
885 if ( *s != '[' ) { /* for the case of the very first character */
886 while ( *s && ( *s != '[' || ( *s == '[' && s[-1] == '\\' ) ) ) {
887 if ( *s == ' ' || *s == '\n' ) {
888 while ( *s == ' ' || *s == '\n' ) s++; /* Skip blank lines */
889 }
890 else if ( *s == COMMENTCHAR && s[-1] != '\\' ) {
891 while ( *s && *s != '\n' ) s++;
892 }
893 else s++;
894 }
895 }
896 if ( *s == 0 ) break;
897 /*
898 if everything is correct we are now on a [
899 a: look for matching ] in the same line. This defines the label.
900 */
901 t = s+1;
902 while ( *t && *t != ']' && *t != '\n' && ( *t != COMMENTCHAR ||
903 ( *t == COMMENTCHAR && t[-1] != '\\' ) ) ) t++;
904 if ( *t == 0 ) {
905 fprintf(stderr,"%s: irregular end of file %s.\n",axohelp,inname);
906 return(-1);
907 }
908 else if ( *t == '\n' ) {
909 fprintf(stderr,"%s: a label in file %s should inside a single line.\n",axohelp,inname);
910 return(-1);
911 }
912 else if ( *t == COMMENTCHAR ) {
913 fprintf(stderr,"%s: illegal comment character inside a label in file %s.\n",axohelp,inname);
914 return(-1);
915 }
916 else if ( *t != ']' ) {
917 fprintf(stderr,"%s: internal error reading a label in file %s.\n",axohelp,inname);
918 return(-1);
919 }
920 /*
921 Store the address of this command
922 */
923 if ( numinputs >= inputallocations ) { /* we need more space */
924 long newnum, i;
925 char **newadd;
926 if ( inputallocations == 0 ) { newnum = 100; }
927 else { newnum = 2*inputallocations; }
928 if ( ( newadd = (char **)malloc(newnum*sizeof(char *)) ) == 0 ) {
929 fprintf(stderr,"%s: Memory allocation error while reading file %s\n",axohelp,inname);
930 return(-1);
931 }
932 for ( i = 0; i < inputallocations; i++ ) { newadd[i] = inputs[i]; }
933 if ( inputs != 0 ) free(inputs);
934 inputs = newadd;
935 inputallocations = newnum;
936 }
937 inputs[numinputs++] = s;
938 /*
939 Now scan for the first termination character. That is the end of the object.
940 */
941 while ( *t && ( *t != TERMCHAR || ( *t == TERMCHAR && t[-1] == '\\' ) )
942 && *t != '[' ) t++;
943 if ( *t != '[' ) {
944 while ( t[-1] == ' ' || t[-1] == '\n' ) t--;
945 *t++ = 0;
946 }
947 s = t;
948 }
949 return(numinputs);
950 }
951
952 /*
953 #] ScanForObjects :
954 #[ ReadInput :
955 */
956
957 char *ReadInput(char *filename)
958 {
959 FILE *finput;
960 long filesize, num;
961 char *buffer;
962 if ( ( finput = fopen(filename,"r") ) == 0 ) {
963 fprintf(stderr,"%s: Cannot open file %s\n",axohelp,filename);
964 exit(1);
965 }
966 if ( ( fseek(finput,0,SEEK_END) != 0 )
967 || ( ( filesize = ftell(finput) ) < 0 )
968 || ( fseek(finput,0,SEEK_SET) != 0 ) ) {
969 fprintf(stderr,"%s: File error in file %s\n",axohelp,filename);
970 exit(1);
971 }
972 if ( ( buffer = (char*)malloc((filesize+1)*sizeof(char)) ) == 0 ) {
973 fprintf(stderr,"%s: Error allocating %ld chars of memory",axohelp,filesize+1);
974 exit(1);
975 }
976 /*
977 Assume character in file is 1 byte, which is true for all cases
978 we currently encounter.
979 */
980 num = fread( buffer, 1, filesize, finput );
981 if ( ferror(finput) ) {
982 fprintf(stderr,"%s: Error reading file %s\n",axohelp,filename);
983 exit(1);
984 }
985 /*
986 By definition, fread reads ALL the items specified, or it gets to
987 end-of-file, or there is an error.
988 It returns the actual number of items successfully read, which
989 is less than the number given in the 3rd argument ONLY if a
990 read error or end-of-file is encountered.
991 We have already tested for an error.
992 But num could legitimately be less than filesize, because of
993 translation of CRLF to LF (on MSWindows with MSWindows text file).
994 */
995 buffer[num] = 0;
996 fclose(finput);
997 return(buffer);
998 }
999
1000 /*
1001 #] ReadInput :
1002 #[ CleanupOutput :
1003 */
1004
1005 void CleanupOutput(char *str)
1006 {
1007 char *s, *t;
1008 int period = 0;
1009 s = t = str;
1010 // Collapse multiple white space (' ' and '\n') to one. Remove trailing space.
1011 while ( *s ) {
1012 if ( *s == '\n' ) {*s = ' '; }
1013 if ( (*s == ' ') && ( s[1] == ' ' || s[1] == '\n' || s[1] == 0 ) ) { s++;}
1014 else { *t++ = *s++; }
1015 }
1016 *t = 0;
1017
1018 // Optimize format of numbers:
1019 s = t = str;
1020 while ( *s ) {
1021 if ( *s == '.' ) { period = 1; *t++ = *s++; }
1022 else if ( isdigit(*s) ) { *t++ = *s++; }
1023 else if ( period ) {
1024 while ( t > str && t[-1] == '0' ) { t--; }
1025 if ( t > str && t[-1] == '.' ) {
1026 t--;
1027 // Handle case that number is .000, not e.g. 9.000
1028 if (t > str && ! isdigit(t[-1]) ) {
1029 *t++ = '0';
1030 }
1031 }
1032 period = 0; *t++ = *s++;
1033 }
1034 else {
1035 period = 0; *t++ = *s++;
1036 }
1037 }
1038 *t = 0;
1039
1040 // Collapse '-0' to '0'
1041 s = t = str;
1042 while ( *s ) {
1043 if ( *s == '-' && s[1] == '0' && s[2] == ' ' ) { s++; }
1044 else *t++ = *s++;
1045 }
1046 *t = 0;
1047 }
1048
1049 /*
1050 #] CleanupOutput :
1051 #[ send :
1052 */
1053
1054 void send( char* str ) {
1055 fprintf(outfile, "%s",str);
1056 }
1057
1058 /*
1059 #] CleanupOutput :
1060 #[ sendClean :
1061 */
1062
1063 void sendClean( char* str ) {
1064 CleanupOutput(str);
1065 send(str);
1066 }
1067
1068 /*
1069 #] sendClean :
1070 #[ DoOneObject :
1071 */
1072
1073 int DoOneObject(char *cinput)
1074 {
1075 // OBSERVE: Single point for exit, to ensure proper clean up.
1076 int num, i, num1, num2, retcode;
1077 char *s, *t;
1078 double *argbuf = 0;
1079 char *curline = 0;
1080 retcode = -1;
1081 SetDefaults();
1082
1083 // Store current line here for diagnostics, since parsing is destructive
1084 // of input buffer.
1085 curline = (char *)malloc( (1+strlen(cinput)) * sizeof(char) + 1 );
1086 if (! curline) {
1087 fprintf(stderr,"%s: Memory allocation error while reading file %s\n",axohelp,inname);
1088 goto EXIT;
1089 }
1090 strcpy( curline, cinput );
1091
1092 // Locate number of object:
1093 s = cinput; while ( *s != '[' ) s++;
1094 s++; t = s; while ( *t != ']' ) t++;
1095 *t++ = 0; while ( *t == ' ' || *t == '\t' || *t == '\n' ) t++;
1096
1097 fprintf(outfile,"\\axo@setObject{%s}%%\n{%s%c}%%\n{",s,t,TERMCHAR);
1098 outpos = outputbuffer;
1099
1100 nameobject = t; while ( *t != ' ' && *t != '\t' && *t != '\n' && *t ) t++;
1101 *t++ = 0; while ( *t == ' ' || *t == '\t' || *t == '\n' ) t++;
1102 /*
1103 Now nameobject is the name of the command and t points at the first parameter.
1104 */
1105
1106 if ( *s == '0' && s[1] == 0 ) {
1107 /*
1108 The identification line.
1109 In due time we might add more options here.
1110 */
1111 if ( strcmp(nameobject,"AxodrawWantsPDF") == 0 ) {
1112 identification = 1;
1113 fprintf(outfile,"Axohelp version %d.%d. PDF output.",VERSION,SUBVERSION);
1114 goto SUCCESS;
1115 }
1116 else {
1117 fprintf(stderr,"%s: Illegal object name in identification string [0]: %s\n"
1118 ,axohelp,nameobject);
1119 goto EXIT;
1120 }
1121 }
1122 /*
1123 if ( identification == 0 ) {
1124 fprintf(stderr,"%s: No identification string. Check versions.\n",axohelp);
1125 goto EXIT;
1126 }
1127 */
1128
1129 if ( ( strcmp(nameobject,"Curve") == 0 )
1130 || ( strcmp(nameobject,"Polygon") == 0 )
1131 || ( strcmp(nameobject,"FilledPolygon") == 0 ) ) {
1132 /*
1133 #[ Curve,Polygons :
1134 */
1135 if ( ( argbuf = ReadArray(t,&num1,&num2) ) == 0 )
1136 goto EXIT;
1137 if ( num2-1 != 0 ) {
1138 fprintf(stderr,"%s: Command %s should have no extra numbers in %s.\n",
1139 axohelp,nameobject,inname);
1140 goto EXIT;
1141 }
1142 else {
1143 /*
1144 First some 'fixed' operations to set the state right
1145 1: scale 2: linewidth 3: color
1146 Then the function.
1147 Finally the trailer and cleanup.
1148 */
1149 axolinewidth = argbuf[2*num1+num2-1];
1150 SetLineWidth(axolinewidth);
1151 if ( strcmp(nameobject,"Curve") == 0 ) {
1152 Curve(argbuf,num1);
1153 }
1154 else if ( strcmp(nameobject,"Polygon") == 0 ) {
1155 Polygon(argbuf,num1,0);
1156 }
1157 else if ( strcmp(nameobject,"FilledPolygon") == 0 ) {
1158 Polygon(argbuf,num1,1);
1159 }
1160 free(argbuf);
1161 argbuf = 0;
1162 }
1163 /*
1164 #] Curve,Polygons :
1165 */
1166 }
1167 else if ( strcmp(nameobject,"DashCurve") == 0 ) {
1168 /*
1169 #[ DashCurve :
1170 */
1171 if ( ( argbuf = ReadArray(t,&num1,&num2) ) == 0 )
1172 goto EXIT;
1173 if ( num2 != 2 ) {
1174 fprintf(stderr,"%s: Command %s does not have two numbers after the coordinates\n in file %s.\n",
1175 axohelp,nameobject,inname);
1176 goto EXIT;
1177 }
1178 else {
1179 /*
1180 First some 'fixed' operations to set the state right
1181 1: scale 2: linewidth 3: color
1182 Then the function.
1183 Finally the trailer and cleanup.
1184 */
1185 axolinewidth = argbuf[2*num1+num2-1];
1186 SetLineWidth(axolinewidth);
1187 DashCurve(argbuf,num1);
1188 free(argbuf);
1189 argbuf = 0;
1190 }
1191 /*
1192 #] DashCurve :
1193 */
1194 }
1195 else {
1196 /*
1197 #[ Regular command :
1198 */
1199 if ( ( argbuf = ReadTail(t,&num) ) == 0 )
1200 goto EXIT;
1201 for ( i = 0; i < sizeof(commands)/sizeof(KEYWORD); i++ ) {
1202 if ( strcmp(nameobject,commands[i].name) == 0 ) {
1203 if ( num == commands[i].numargs+1 ) {
1204 /*
1205 First some 'fixed' operations to set the state right
1206 1: scale 2: linewidth 3: color
1207 Then the function.
1208 Finally the trailer and cleanup.
1209 */
1210 axolinewidth = argbuf[num-1];
1211 SetLineWidth(axolinewidth);
1212 (*(commands[i].func))(argbuf);
1213 free(argbuf);
1214 argbuf = 0;
1215 break;
1216 }
1217 else {
1218 fprintf(stderr,"%s: Command %s should have %d(+1) arguments in %s.\n"
1219 ,axohelp,nameobject,commands[i].numargs,inname);
1220 goto EXIT;
1221 }
1222 }
1223 }
1224 /*
1225 #] Regular command :
1226 */
1227 if ( i >= sizeof(commands)/sizeof(KEYWORD) ) {
1228 fprintf(stderr,"%s: Command %s not recognized in file %s.\n",
1229 axohelp,nameobject,inname);
1230 goto EXIT;
1231 }
1232 }
1233 SUCCESS:
1234 retcode = 0;
1235 EXIT:
1236 fprintf(outfile,"}\n");
1237 if (retcode) {
1238 fprintf( stderr, "Line in file '%s' causing error is\n '%.75s'\n",
1239 inname, curline );
1240 if ( strlen(curline) > 75 ) { fprintf( stderr, " (Truncated)\n" ); }
1241 }
1242 if (argbuf) { free(argbuf); }
1243 if (curline) { free(curline); }
1244 return(retcode);
1245 }
1246
1247 /*
1248 #] DoOneObject :
1249 #[ PrintHelp :
1250 */
1251
1252 void PrintHelp(char *name)
1253 {
1254 fprintf(stderr,"This is %s v. %d.%d of %s\n", NAME, VERSION, SUBVERSION, VERSIONDATE);
1255 fprintf(stderr,"Usage: %s [OPTION] filename\n",name);
1256 fprintf(stderr,"Input will then be from filename.ax1, output to filename.ax2.\n");
1257 fprintf(stderr,"-h, --help : prints this help information and terminates.\n");
1258 fprintf(stderr,"-v, --version : prints version information and terminates.\n");
1259 fprintf(stderr,"-V : prints information about each function treated in stdout.\n");
1260 fprintf(stderr," (NOT CURRENTLY IMPLEMENTED)\n\n");
1261 fprintf(stderr,"Please report bugs to [email protected].\n");
1262 fprintf(stderr,"axodraw2 web page: https://ctan.org/pkg/axodraw2\n");
1263 }
1264
1265 /*
1266 #] PrintHelp :
1267 #[ PrintHelpPrompt :
1268 */
1269
1270 void PrintHelpPrompt(char *name)
1271 {
1272 fprintf(stderr,"Try '%s --help' for more information.\n", name);
1273 }
1274
1275 /*
1276 #] PrintHelpPrompt :
1277 #[ Inivars :
1278 */
1279
1280 void Inivars()
1281 {
1282 M_pi = acos(-1.);
1283 torad = M_pi/180.;
1284 BzK = 4.*(sqrt(2.)-1.)/3.;
1285 }
1286
1287 /*
1288 #] Inivars :
1289 #[ main :
1290 */
1291
1292 int main(int argc,char **argv)
1293 {
1294 char *s, *inbuffer;
1295 int length, error = 0;
1296 long num,i;
1297
1298 Inivars();
1299 argc--;
1300 axohelp = *argv++;
1301 if ( argc <= 0 ) {
1302 PrintHelp(axohelp);
1303 return 1;
1304 }
1305 while ( (*argv)[0] == '-' ) { /* we have options */
1306 s = *argv;
1307 if ( s[1] == '-' ) {
1308 /* Double "--", treat as "-" */
1309 s++;
1310 }
1311 if ( ( strcmp(s,"-h") == 0) || ( strcmp(s,"-help") == 0 ) ) {
1312 PrintHelp(axohelp);
1313 return 0;
1314 }
1315 else if ( ( strcmp(s,"-v") == 0 ) || ( strcmp(s,"-version") == 0 ) ) {
1316 fprintf(stderr,
1317 "%s %d.%d\n(release date %s)\n"
1318 "Copyright 2018 John Collins and Jos Vermaseren.\n"
1319 "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n"
1320 "There is NO WARRANTY, to the extent permitted by law.\n",
1321 axohelp, VERSION, SUBVERSION, VERSIONDATE);
1322 return 0;
1323 }
1324 else if ( strcmp( s, "-V" ) == 0 ) {
1325 VerboseFlag = 1;
1326 }
1327 else {
1328 fprintf(stderr,"%s: Illegal option %s\n", axohelp, *argv);
1329 PrintHelpPrompt(axohelp);
1330 return 1;
1331 }
1332 argc--; argv++;
1333 if ( argc <= 0 ) {
1334 fprintf(stderr,"%s: Not enough arguments\n",axohelp);
1335 PrintHelpPrompt(axohelp);
1336 return 1;
1337 }
1338 }
1339 if ( argc != 1 ) {
1340 fprintf(stderr,"%s: Too many arguments\n",axohelp);
1341 PrintHelpPrompt(axohelp);
1342 return 1;
1343 }
1344 /*
1345 The filename is now in s. We copy it to a separate string and
1346 paste on the extension .ax1 (if needed), and construct the
1347 name of the output file.
1348 */
1349 s = *argv;
1350 length = strlen(s);
1351 inname = (char*)malloc((length+5)*sizeof(char));
1352 inname = strcpy(inname,s);
1353 outname = (char*)malloc((length+5)*sizeof(char));
1354 outname = strcpy(outname,s);
1355 if ( (!inname) || (!outname) ) {
1356 fprintf(stderr,"%s: Memory allocation error in startup\n",axohelp);
1357 exit(1);
1358 }
1359 if ( (length > 4) && (strcmp(s+length-4, ".ax1") == 0) ) {
1360 outname[length-1] = '2';
1361 }
1362 else {
1363 strcpy( inname+length, ".ax1" );
1364 strcpy( outname+length, ".ax2" );
1365 }
1366 if ( ( inbuffer = ReadInput(inname) ) == 0 ) return(1);
1367 if ( ( outfile = fopen(outname,"w") ) == 0 ) {
1368 fprintf(stderr,"%s: Cannot create file %s\n",axohelp,outname);
1369 exit(1);
1370 }
1371 num = ScanForObjects(inbuffer);
1372 for ( i = 0; i < num; i++ ) {
1373 numobject = i+1;
1374 if ( DoOneObject(inputs[i]) < 0 ) { error++; }
1375 }
1376 fclose(outfile);
1377 if ( error > 0 ) {
1378 fprintf(stderr,"%s: %d objects in %s were not translated correctly.\n",
1379 axohelp,error,inname);
1380 fprintf( stderr, " Erroneous data in input file normally corresponds to erroneous arguments\n for an axodraw object in the .tex file.\n" );
1381 return(1);
1382 }
1383 return(0);
1384 }
1385
1386 /*
1387 #] main :
1388 #[ routines :
1389 #[ Line routines :
1390 #[ Line : *
1391
1392 Line(x1,y1)(x2,y2)
1393 */
1394
1395 void Line(double *args)
1396 {
1397 MoveTo(args[0],args[1]);
1398 LineTo(args[2],args[3]);
1399 Stroke;
1400 }
1401
1402 /*
1403 #] Line :
1404 #[ DoubleLine : *
1405
1406 DoubleLine(x1,y1)(x2,y2){sep}
1407 */
1408
1409 void DoubleLine(double *args)
1410 {
1411 SaveGraphicsState;
1412 if ( args[4] > 0 ) {
1413 SetLineWidth(args[4]+axolinewidth);
1414 Line(args);
1415 SetLineWidth(args[4]-axolinewidth);
1416 SetBackgroundColor(STROKING);
1417 }
1418 Line(args);
1419 RestoreGraphicsState;
1420 }
1421
1422 /*
1423 #] DoubleLine :
1424 #[ DashLine : *
1425
1426 DashLine(x1,y1)(x2,y2){dashsize}
1427 */
1428
1429 void DashLine(double *args)
1430 {
1431 double dx = args[2]-args[0], dy = args[3]-args[1], dr = sqrt(dx*dx+dy*dy);
1432 double dashsize = args[4];
1433 int num;
1434 num = dr/dashsize;
1435 if ( ( num%2 ) == 1 ) num++;
1436 dashsize = dr/num;
1437 SetDashSize(dashsize,dashsize/2);
1438 Line(args);
1439 }
1440
1441 /*
1442 #] DashLine :
1443 #[ DashDoubleLine : *
1444
1445 DashDoubleLine(x1,y1)(x2,y2){sep}{dashsize}
1446 */
1447
1448 void DashDoubleLine(double *args)
1449 {
1450 double dx = args[2]-args[0], dy = args[3]-args[1], dr = sqrt(dx*dx+dy*dy);
1451 double dashsize = args[5];
1452 int num;
1453 num = dr/dashsize;
1454 if ( ( num%2 ) == 1 ) num++;
1455 dashsize = dr/num;
1456 SetDashSize(dashsize,dashsize/2);
1457 DoubleLine(args);
1458 }
1459
1460 /*
1461 #] DashDoubleLine :
1462 #[ ArrowLine : *
1463
1464 ArrowLine(x1,y1)(x2,y2){size}{where}
1465
1466 where: x of arrowhead is x1+where*(x2-x1). Same for y.
1467 serves both ArrowLine and LongArrowLine
1468 */
1469
1470 void ArrowLine(double *args)
1471 {
1472 double dx, dy, dr, where;
1473 Line(args);
1474 if ( arrow.type == 0 ) where = args[5];
1475 else where = arrow.where;
1476 dx = args[2]-args[0];
1477 dy = args[3]-args[1];
1478 dr = sqrt(dx*dx+dy*dy);
1479 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1480 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
1481 SetTransferMatrix(1,0,0,1,dr*where,0);
1482 if ( arrow.type == 0 ) arrow.width = args[4];
1483 ArrowHead();
1484 }
1485
1486 /*
1487 #] ArrowLine :
1488 #[ ArrowDoubleLine : *
1489
1490 ArrowDoubleLine(x1,y1)(x2,y2){sep}{size}{where}
1491
1492 where: x of arrowhead is x1+where*(x2-x1). Same for y.
1493 serves both ArrowLine and LongArrowLine
1494 */
1495
1496 void ArrowDoubleLine(double *args)
1497 {
1498 double dx, dy, dr, where;
1499 DoubleLine(args);
1500 if ( arrow.type == 0 ) where = args[6];
1501 else where = arrow.where;
1502 dx = args[2]-args[0];
1503 dy = args[3]-args[1];
1504 dr = sqrt(dx*dx+dy*dy);
1505 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1506 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
1507 SetTransferMatrix(1,0,0,1,dr*where,0);
1508 if ( arrow.type == 0 ) arrow.width = args[5]+args[4];
1509 ArrowHead();
1510 }
1511
1512 /*
1513 #] ArrowDoubleLine :
1514 #[ DashArrowLine : *
1515
1516 DashArrowLine(x1,y1)(x2,y2){dashsize}{amplitude}{where}
1517
1518 where: x of arrowhead is x1+where*(x2-x1). Same for y.
1519 we re-adjust the position of the arrow to place it on a dash.
1520 */
1521
1522 void DashArrowLine(double *args)
1523 {
1524 double dx = args[2]-args[0], dy = args[3]-args[1], dr = sqrt(dx*dx+dy*dy);
1525 double dashsize = args[4], where;
1526 int num, nw;
1527 if ( arrow.type == 0 ) where = args[6];
1528 else where = arrow.where;
1529 num = dr/dashsize;
1530 if ( ( num%2 ) == 1 ) num++;
1531 if ( num%4 != 0 && where > 0.499 && where < 0.501 ) num += 2;
1532 dashsize = dr/num;
1533 SetDashSize(dashsize,dashsize/2);
1534 Line(args);
1535 nw = where*(num/2)+0.5;
1536 where = (2.0*nw)/num;
1537 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1538 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
1539 SetTransferMatrix(1,0,0,1,dr*where,0);
1540 if ( arrow.type == 0 ) arrow.width = args[5];
1541 ArrowHead();
1542 }
1543
1544 /*
1545 #] DashArrowLine :
1546 #[ DashArrowDoubleLine : *
1547
1548 DashArrowDoubleLine(x1,y1)(x2,y2){sep}{dashsize}{amplitude}{where}
1549
1550 where: x of arrowhead is x1+where*(x2-x1). Same for y.
1551 we re-adjust the position of the arrow to place it on a dash.
1552 */
1553
1554 void DashArrowDoubleLine(double *args)
1555 {
1556 double dx = args[2]-args[0], dy = args[3]-args[1], dr = sqrt(dx*dx+dy*dy);
1557 double dashsize = args[5], where;
1558 int num, nw;
1559 if ( arrow.type == 0 ) where = args[7];
1560 else where = arrow.where;
1561 num = dr/dashsize;
1562 if ( ( num%2 ) == 1 ) num++;
1563 if ( num%4 != 0 && where > 0.499 && where < 0.501 ) num += 2;
1564 dashsize = dr/num;
1565 SetDashSize(dashsize,dashsize/2);
1566 DoubleLine(args);
1567 nw = where*(num/2)+0.5;
1568 where = (2.0*nw)/num;
1569 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1570 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
1571 SetTransferMatrix(1,0,0,1,dr*where,0);
1572 if ( arrow.type == 0 ) arrow.width = args[6]+args[4];
1573 ArrowHead();
1574 }
1575
1576 /*
1577 #] DashArrowDoubleLine :
1578 #] Line routines :
1579 #[ Arc routines :
1580 #[ CArc : *
1581
1582 CArc(x1,y1)(r,phi1,phi2)
1583
1584 The arc segment runs anticlockwise
1585
1586 We divide the segment into a number of equal segments, each less
1587 than 90 degrees. Then the control points are at distance
1588 4*tan(90/n)/3 from the endpoints, in which n=360/(phi2-phi1)
1589 (note that if n=4 we get tan(22.5)=sqrt(2)-1).
1590 */
1591
1592 void CArc(double *args)
1593 {
1594 double phi1 = args[3], phi2 = args[4], r = args[2];
1595 double dphi;
1596 while ( phi2 < phi1 ) phi2 += 360;
1597 dphi = phi2-phi1;
1598 if ( dphi <= 0 ) { return; }
1599 if ( dphi >= 360 ) { ECirc(args); return; }
1600 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1601 ArcSegment(r,phi1,dphi);
1602 Stroke;
1603 }
1604
1605 /*
1606 #] CArc :
1607 #[ DoubleArc : *
1608
1609 DoubleArc(x1,y1)(r,phi1,phi2){sep}
1610
1611 The arc segment runs anticlockwise
1612
1613 We divide the segment into a number of equal segments, each less
1614 than 90 degrees. Then the control points are at distance
1615 4*tan(90/n)/3 from the endpoints, in which n=360/(phi2-phi1)
1616 (note that if n=4 we get tan(22.5)=sqrt(2)-1).
1617 */
1618
1619 void DoubleArc(double *args)
1620 {
1621 double phi1 = args[3], phi2 = args[4], r = args[2];
1622 double dphi, sep = args[5];
1623 while ( phi2 < phi1 ) phi2 += 360;
1624 dphi = phi2-phi1;
1625 if ( dphi <= 0 ) { return; }
1626 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1627 ArcSegment(r+sep/2,phi1,dphi);
1628 Stroke;
1629 ArcSegment(r-sep/2,phi1,dphi);
1630 Stroke;
1631 }
1632
1633 /*
1634 #] DoubleArc :
1635 #[ DashCArc : *
1636
1637 DashCArc(x1,y1)(r,phi1,phi2){dashsize}
1638
1639 The arc segment runs anticlockwise
1640 */
1641
1642 void DashCArc(double *args)
1643 {
1644 double arcsize = args[4]-args[3];
1645 double r = args[2], dr, dashsize = args[5];
1646 int num;
1647 if ( arcsize <= 0 ) arcsize += 360;
1648 if ( arcsize > 360 ) arcsize = 360;
1649 dr = 2*M_pi*r*(arcsize/360);
1650 num = dr/dashsize;
1651 if ( (num%2) == 1 ) num++;
1652 dashsize = dr/num;
1653 SetDashSize(dashsize,dashsize/2);
1654 CArc(args);
1655 }
1656
1657 /*
1658 #] DashCArc :
1659 #[ DashDoubleArc : *
1660
1661 DashDoubleArc(x1,y1)(r,phi1,phi2){sep}{dashsize}
1662
1663 The arc segment runs anticlockwise
1664 The trouble here is to synchronize the two dash patterns.
1665 This is done by a rescaling. We assume that the rescaling is
1666 sufficiently small that the linewidth does not suffer from it.
1667 */
1668
1669 void DashDoubleArc(double *args)
1670 {
1671 double phi1 = args[3], r = args[2];
1672 double arcsize = args[4]-args[3];
1673 double dr, dashsize = args[6], sep = args[5];
1674 int num;
1675 linesep = sep;
1676 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1677 if ( arcsize <= 0 ) arcsize += 360;
1678 if ( arcsize > 360 ) arcsize = 360;
1679 dr = 2*M_pi*r*(arcsize/360);
1680 num = dr/dashsize;
1681 if ( (num%2) == 1 ) num++;
1682 dashsize = dr/num;
1683 SetDashSize(dashsize,dashsize/2);
1684 SaveGraphicsState;
1685 SetTransferMatrix(1+0.5*sep/r,0,0,1+0.5*sep/r,0,0);
1686 ArcSegment(r,phi1,arcsize);
1687 Stroke;
1688 RestoreGraphicsState;
1689 SaveGraphicsState;
1690 SetTransferMatrix(1-0.5*sep/r,0,0,1-0.5*sep/r,0,0);
1691 ArcSegment(r,phi1,arcsize);
1692 Stroke;
1693 RestoreGraphicsState;
1694 }
1695
1696 /*
1697 #] DashDoubleArc :
1698 #[ ArrowArc : *
1699
1700 ArrowArc(x1,y1)(r,phi1,phi2){amplitude}{where}
1701
1702 where: phi of arrowhead is phi1+where*(phi2-phi1)
1703 The arc segment runs anticlockwise
1704 serves both ArrowArc and LongArrowArc and ... (Jaxodraw addition)
1705 */
1706
1707 void ArrowArc(double *args)
1708 {
1709 double phi1 = args[3], phi2 = args[4], r = args[2];
1710 double dphi, x, y, phi;
1711 if ( arrow.type == 0 ) {
1712 arrow.width = args[5];
1713 arrow.where = args[6];
1714 }
1715
1716 while ( phi2 < phi1 ) phi2 += 360;
1717 dphi = phi2-phi1;
1718 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1719
1720 if ( dphi <= 0 ) { return; }
1721 ArcSegment(r,phi1,dphi);
1722 Stroke;
1723 /*
1724 Now compute the position and angle of the arrowhead
1725 */
1726 phi = phi1 + arrow.where*dphi;
1727 x = r*COS(phi); y = r*SIN(phi);
1728 SetTransferMatrix(1,0,0,1,x,y);
1729 SetTransferMatrix(COS(phi+90),SIN(phi+90),-SIN(phi+90),COS(phi+90),0,0);
1730 ArrowHead();
1731 }
1732
1733 /*
1734 #] ArrowArc :
1735 #[ ArrowDoubleArc : *
1736
1737 ArrowDoubleArc(x1,y1)(r,phi1,phi2){sep}{amplitude}{where}
1738
1739 where: phi of arrowhead is phi1+where*(phi2-phi1)
1740 The arc segment runs anticlockwise
1741 serves both ArrowArc and LongArrowArc and ... (Jaxodraw addition)
1742 */
1743
1744 void ArrowDoubleArc(double *args)
1745 {
1746 double phi1 = args[3], phi2 = args[4], r = args[2];
1747 double dphi, x, y, phi;
1748 linesep = args[5];
1749 while ( phi2 < phi1 ) phi2 += 360;
1750 dphi = phi2-phi1;
1751 if ( dphi <= 0 ) { return; }
1752 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1753 ArcSegment(r+linesep/2,phi1,dphi);
1754 Stroke;
1755 ArcSegment(r-linesep/2,phi1,dphi);
1756 Stroke;
1757 /*
1758 Now compute the position and angle of the arrowhead
1759 */
1760 if ( arrow.type == 0 ) {
1761 arrow.width = args[6];
1762 arrow.where = args[7];
1763 }
1764 phi = phi1 + arrow.where*dphi;
1765 x = r*COS(phi); y = r*SIN(phi);
1766 SetTransferMatrix(1,0,0,1,x,y);
1767 SetTransferMatrix(COS(phi+90),SIN(phi+90),-SIN(phi+90),COS(phi+90),0,0);
1768 ArrowHead();
1769 }
1770
1771 /*
1772 #] ArrowDoubleArc :
1773 #[ DashArrowArc : +
1774
1775 DashArrowArc(x1,y1)(r,phi1,phi2){dashsize}{amplitude}{where}
1776
1777 where: phi of arrowhead is phi1+where*(phi2-phi1)
1778 The arc segment runs anticlockwise
1779 */
1780
1781 void DashArrowArc(double *args)
1782 {
1783 double dphi, x, y, phi, phi1 = args[3], phi2 = args[4];
1784 double r = args[2], dr, dashsize = args[5];
1785 int num;
1786 if ( arrow.type == 0 ) {
1787 arrow.width = args[6];
1788 arrow.where = args[7];
1789 }
1790
1791 while ( phi2 < phi1 ) phi2 += 360;
1792 dphi = phi2-phi1;
1793 if ( dphi > 360 ) dphi = 360;
1794 dr = 2*M_pi*r*(dphi/360);
1795 num = dr/dashsize;
1796 if ( (num%2) == 1 ) num++;
1797 if ( num%4 != 0 && arrow.where > 0.499 && arrow.where < 0.501 ) num += 2;
1798 dashsize = dr/num;
1799 SetDashSize(dashsize,dashsize/2);
1800 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1801 if ( dphi <= 0 ) { return; }
1802 ArcSegment(r,phi1,dphi);
1803 Stroke;
1804 /*
1805 Now compute the position and angle of the arrowhead
1806 */
1807 phi = phi1 + arrow.where*dphi;
1808 x = r*COS(phi); y = r*SIN(phi);
1809 SetTransferMatrix(1,0,0,1,x,y);
1810 SetTransferMatrix(COS(phi+90),SIN(phi+90),-SIN(phi+90),COS(phi+90),0,0);
1811 ArrowHead();
1812 }
1813
1814 /*
1815 #] DashArrowArc :
1816 #[ DashArrowDoubleArc : +
1817
1818 DashArrowDoubleArc(x1,y1)(r,phi1,phi2){sep}{dashsize}{amplitude}{where}
1819
1820 where: phi of arrowhead is phi1+where*(phi2-phi1)
1821 The arc segment runs anticlockwise
1822 */
1823
1824 void DashArrowDoubleArc(double *args)
1825 {
1826 double dphi, x, y, phi, phi1 = args[3], phi2 = args[4];
1827 double r = args[2], dr, dashsize = args[6];
1828 int num;
1829 linesep = args[5];
1830 if ( arrow.type == 0 ) {
1831 arrow.width = args[7];
1832 arrow.where = args[8];
1833 }
1834
1835 while ( phi2 < phi1 ) phi2 += 360;
1836 dphi = phi2-phi1;
1837 if ( dphi > 360 ) dphi = 360;
1838 dr = 2*M_pi*r*(dphi/360);
1839 num = dr/dashsize;
1840 if ( (num%2) == 1 ) num++;
1841 if ( num%4 != 0 && arrow.where > 0.499 && arrow.where < 0.501 ) num += 2;
1842 dashsize = dr/num;
1843 SetDashSize(dashsize,dashsize/2);
1844 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1845 if ( dphi <= 0 ) { return; }
1846 SaveGraphicsState;
1847 SetTransferMatrix(1+0.5*linesep/r,0,0,1+0.5*linesep/r,0,0);
1848 ArcSegment(r,phi1,dphi);
1849 Stroke;
1850 RestoreGraphicsState;
1851 SaveGraphicsState;
1852 SetTransferMatrix(1-0.5*linesep/r,0,0,1-0.5*linesep/r,0,0);
1853 ArcSegment(r,phi1,dphi);
1854 Stroke;
1855 RestoreGraphicsState;
1856 /*
1857 Now compute the position and angle of the arrowhead
1858 */
1859 phi = phi1 + arrow.where*dphi;
1860 x = r*COS(phi); y = r*SIN(phi);
1861 SetTransferMatrix(1,0,0,1,x,y);
1862 SetTransferMatrix(COS(phi+90),SIN(phi+90),-SIN(phi+90),COS(phi+90),0,0);
1863 ArrowHead();
1864 }
1865
1866 /*
1867 #] DashArrowDoubleArc :
1868 #[ ArrowArcn : +
1869
1870 ArrowArcn(x1,y1)(r,phi1,phi2){amplitude}{where}
1871
1872 where: phi of arrowhead is phi1+where*(phi2-phi1)
1873 The arc segment runs clockwise
1874 */
1875
1876 void ArrowArcn(double *args)
1877 {
1878 double newargs[7], a;
1879 int i;
1880 for ( i = 0; i < 7; i++ ) newargs[i] = args[i];
1881 newargs[6] = 1-newargs[6];
1882 a = newargs[3]; newargs[3] = newargs[4]; newargs[4] = a;
1883 ArrowArc(newargs);
1884 }
1885
1886 /*
1887 #] ArrowArcn :
1888 #[ DashArrowArcn : +
1889
1890 DashArrowArc(x1,y1)(r,phi1,phi2){amplitude}{where}{dashsize}
1891
1892 where: phi of arrowhead is phi1+where*(phi2-phi1)
1893 The arc segment runs clockwise
1894 */
1895
1896 void DashArrowArcn(double *args)
1897 {
1898 double newargs[8], a;
1899 int i;
1900 for ( i = 0; i < 8; i++ ) newargs[i] = args[i];
1901 newargs[6] = 1-newargs[6];
1902 a = newargs[3]; newargs[3] = newargs[4]; newargs[4] = a;
1903 DashArrowArc(newargs);
1904 }
1905
1906 /*
1907 #] DashArrowArcn :
1908 #] Arc routines :
1909 #[ Circle routines :
1910 #[ ECirc : +
1911
1912 ECirc(x,y){radius}
1913 Draws a circle
1914 */
1915
1916 void ECirc(double *args)
1917 {
1918 SetTransferMatrix(1,0,0,1,args[0],args[1]);
1919 BezierCircle(args[2],"S");
1920 }
1921
1922 /*
1923 #] ECirc :
1924 #[ FOval : +
1925
1926 FOval(x1,y1)(h,w) filled oval in default color
1927
1928 */
1929
1930 void FOval(double *args)
1931 {
1932 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of oval */
1933 SetTransferMatrix(COS(args[4]),SIN(args[4]),-SIN(args[4]),COS(args[4]),0,0);
1934 BezierOval(args[3],args[2],"f");
1935 BezierOval(args[3],args[2],"S");
1936 }
1937
1938 /*
1939 #] COval :
1940 #[ Oval : +
1941
1942 Oval(x1,y1)(h,w)(phi)
1943
1944 One way would be with different scales in the x and y direction,
1945 but that messes up the linewidth.
1946 We need to describe quarter ovals with Bezier curves. The proper
1947 parameters for the curves we can obtain from the circle with scaling.
1948 */
1949
1950 void Oval(double *args)
1951 {
1952 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of oval */
1953 SetTransferMatrix(COS(args[4]),SIN(args[4]),-SIN(args[4]),COS(args[4]),0,0);
1954 BezierOval(args[3],args[2],"S");
1955 }
1956
1957 /*
1958 #] Oval :
1959 #] Circle routines :
1960 #[ Box routines :
1961 #[ EBox : *
1962
1963 \EBox(#1,#2)(#3,#4)
1964
1965 Draws a box with the left bottom at (x1,y1) and the right top
1966 at (x2,y2).
1967 Transparent interior. Current color for edge.
1968 */
1969
1970 void EBox(double *args)
1971 {
1972 Rectangle(args[0],args[1],args[2]-args[0],args[3]-args[1]);
1973 Stroke;
1974 }
1975
1976 /*
1977 #] EBox :
1978 #[ FBox : *
1979
1980 \FBox(#1,#2)(#3,#4)
1981
1982 Draws a filled box with the left bottom at (x1,y1) and
1983 the right top at (x2,y2).
1984 Current color.
1985 */
1986
1987 void FBox(double *args)
1988 {
1989 Rectangle(args[0],args[1],args[2]-args[0],args[3]-args[1]);
1990 Fill;
1991 }
1992
1993 /*
1994 #] FBox :
1995 #[ Boxc : *
1996
1997 \Boxc(#1,#2)(#3,#4)
1998
1999 Draws a transparent box with the center at (x1,y1).
2000 The width and height are (3,4). Uses current color.
2001 */
2002
2003 void Boxc(double *args)
2004 {
2005 Rectangle(args[0]-args[2]/2,args[1]-args[3]/2,args[2],args[3]);
2006 Stroke;
2007 }
2008
2009 /*
2010 #] Boxc :
2011 #[ FBoxc : *
2012
2013 \FBoxc(#1,#2)(#3,#4)
2014
2015 Draws a filled box with the center at (x1,y1).
2016 The width and height are (3,4). Uses current color.
2017 */
2018
2019 void FBoxc(double *args)
2020 {
2021 Rectangle(args[0]-args[2]/2,args[1]-args[3]/2,args[2],args[3]);
2022 Fill;
2023 }
2024
2025 /*
2026 #] FBoxc :
2027 #] Box routines :
2028 #[ Triangle routines :
2029 #[ ETri : *
2030
2031 \ETri(#1,#2)(#3,#4)(#5,#6)
2032
2033 Draws a triangle with the three corners.
2034 */
2035
2036 void ETri(double *args)
2037 {
2038 Triangle(args[0],args[1],args[2],args[3],args[4],args[5]);
2039 Stroke;
2040 }
2041
2042 /*
2043 #] ETri :
2044 #[ FTri : *
2045
2046 \FTri(#1,#2)(#3,#4)(#5,#6)
2047
2048 Draws a triangle with the three corners.
2049 */
2050
2051 void FTri(double *args)
2052 {
2053 Triangle(args[0],args[1],args[2],args[3],args[4],args[5]);
2054 Fill;
2055 }
2056
2057 /*
2058 #] FTri :
2059 #] Triangle routines :
2060 #[ Particle routines :
2061 #[ Vertex : +
2062
2063 Vertex(x,y){radius}
2064 Draws a filled circle
2065 */
2066
2067 void Vertex(double *args)
2068 {
2069 SetTransferMatrix(1,0,0,1,args[0],args[1]);
2070 BezierCircle(args[2],"f");
2071 }
2072
2073 /*
2074 #] Vertex :
2075 #[ ComputeDash :
2076 */
2077
2078 double ComputeDash(double *args,double dr,double indash)
2079 {
2080 int numwindings = args[5]+0.5, numdashes;
2081 int numhalfwindings = 2*numwindings+2.1;
2082 double onehalfwinding = dr/numhalfwindings;
2083 double amp8 = fabs(args[4])*0.9;
2084 double size = LengthBezier(-amp8,0, -amp8,2*args[4], onehalfwinding,2*args[4],1.0);
2085
2086 numdashes = size/(2*indash);
2087 if ( numdashes == 0 ) numdashes = 1;
2088 if ( fabs(size-2*indash*numdashes) > fabs(size-2*indash*(numdashes+1)) )
2089 numdashes++;
2090 return(size/(2*numdashes));
2091 }
2092
2093 /*
2094 #] ComputeDash :
2095 #[ ComputeDashCirc :
2096 */
2097
2098 double ComputeDashCirc(double *args,double indash)
2099 {
2100 int num = args[5]+0.5, numdashes;
2101 double ampi = args[4], radius = args[2];
2102 double darc;
2103 double dr,conv,inc;
2104 double amp1,amp2,amp4,amp5,amp8;
2105 double x0,x1,x2,x3,y0,y1,y2,y3,xx,size;
2106 darc = 360.;
2107 dr = darc*torad*radius;
2108 conv = 1.0/radius;
2109 inc = dr/(2*num); /* increment per half winding */
2110 amp8 = ampi*0.9;
2111 amp1 = radius+ampi;
2112 amp2 = radius-ampi;
2113 amp4 = amp1/cos((inc+amp8)*conv);
2114 amp5 = amp2/cos(amp8*conv);
2115 if ( amp8 < 0 ) amp8 = -amp8;
2116 xx = 2*inc;
2117 x0 = amp1*cos(inc*conv);
2118 y0 = amp1*sin(inc*conv);
2119 x1 = amp4*cos((xx+amp8)*conv)-x0;
2120 y1 = amp4*sin((xx+amp8)*conv)-y0;
2121 x2 = amp5*cos((xx+amp8)*conv)-x0;
2122 y2 = amp5*sin((xx+amp8)*conv)-y0;
2123 x3 = amp2*cos(xx*conv)-x0;
2124 y3 = amp2*sin(xx*conv)-y0;
2125 size = LengthBezier(x1,y1,x2,y2,x3,y3,1.0);
2126 numdashes = size/(2*indash);
2127 if ( numdashes == 0 ) numdashes = 1;
2128 if ( fabs(size-2*indash*numdashes) > fabs(size-2*indash*(numdashes+1)) )
2129 numdashes++;
2130 return(size/(2*numdashes));
2131 }
2132
2133 /*
2134 #] ComputeDashCirc :
2135 #[ ComputeDashPhotonArc :
2136 */
2137
2138 double ComputeDashPhotonArc(double *args,double darc,double dashsize,double *dashstart)
2139 {
2140 int numdashes, numd;
2141 double len1, len2, size, size2, ampli = args[5], radius = args[2];
2142 double cp = cos(darc);
2143 double sp = sin(darc);
2144 double cp2 = cos(darc/2.);
2145 double sp2 = sin(darc/2.);
2146 double beta = radius*darc/(M_pi*ampli);
2147 double tt = (sp-cp*beta)/(cp+sp*beta);
2148 double x2 = ((radius+ampli)*8*(beta*cp2-sp2)-(beta*(4+cp)
2149 +(tt*cp*3.-sp*4.))*radius)/((beta-tt)*3.);
2150 double x1 = ((radius+ampli)*8.*cp2-(1+cp)*radius)/3.-x2;
2151 double y1 = (x1-radius)*beta;
2152 double y2 = (x2-radius*cp)*tt+radius*sp;
2153 double x3 = radius*cp;
2154 double y3 = radius*sp;
2155 len1 = LengthBezier(x1-radius,y1,x2-radius,y2,x3-radius,y3,1.0);
2156 ampli = -ampli;
2157 beta = radius*darc/(M_pi*ampli);
2158 tt = (sp-cp*beta)/(cp+sp*beta);
2159 x2 = ((radius+ampli)*8*(beta*cp2-sp2)-(beta*(4+cp)
2160 +(tt*cp*3.-sp*4.))*radius)/((beta-tt)*3.);
2161 x1 = ((radius+ampli)*8.*cp2-(1+cp)*radius)/3.-x2;
2162 y1 = (x1-radius)*beta;
2163 y2 = (x2-radius*cp)*tt+radius*sp;
2164 x3 = radius*cp;
2165 y3 = radius*sp;
2166 len2 = LengthBezier(x1-radius,y1,x2-radius,y2,x3-radius,y3,1.0);
2167 size = (len1+len2)/2;
2168 size2 = len1/2;
2169 numdashes = size/(2*dashsize);
2170 if ( numdashes == 0 ) numdashes = 1;
2171 if ( fabs(size-2*dashsize*numdashes) > fabs(size-2*dashsize*(numdashes+1)) )
2172 numdashes++;
2173 dashsize = size/(2*numdashes);
2174 numd = size2/(2*dashsize);
2175 *dashstart = -(size2 - 2*numd*dashsize) + dashsize/2;
2176 if ( *dashstart < 0 ) *dashstart += 2*dashsize;
2177 return(dashsize);
2178 }
2179
2180 /*
2181 #] ComputeDashPhotonArc :
2182 #[ ComputeDashGluonArc :
2183 */
2184
2185 double ComputeDashGluonArc(double *args,double darc,double dashsize)
2186 {
2187 int numdashes, num = args[6];
2188 double radius = args[2], ampi = args[5];
2189 double dr,conv,inc, size;
2190 double amp1,amp2,amp4,amp5,amp8;
2191 double x1,x2,x3,y1,y2,y3,xx,x3p,y3p;
2192 dr = darc*torad*radius;
2193 conv = 1.0/radius;
2194 inc = dr/(2*num+2); /* increment per half winding */
2195 amp8 = ampi*0.9;
2196 amp1 = radius+ampi;
2197 amp2 = radius-ampi;
2198 amp4 = amp1/cos((inc+amp8)*conv);
2199 amp5 = amp2/cos(amp8*conv);
2200 if ( amp8 < 0 ) amp8 = -amp8;
2201
2202 xx = 2*inc;
2203 x3p = amp2*cos(xx*conv);
2204 y3p = amp2*sin(xx*conv);
2205 x1 = amp5*cos((xx-amp8)*conv)-x3p;
2206 y1 = amp5*sin((xx-amp8)*conv)-y3p;
2207 x2 = amp4*cos((xx-amp8)*conv)-x3p;
2208 y2 = amp4*sin((xx-amp8)*conv)-y3p;
2209 x3 = amp1*cos((xx+inc)*conv)-x3p;
2210 y3 = amp1*sin((xx+inc)*conv)-y3p;
2211 size = LengthBezier(x1,y1,x2,y2,x3,y3,1.0);
2212
2213 numdashes = size/(2*dashsize);
2214 if ( numdashes == 0 ) numdashes = 1;
2215 if ( fabs(size-2*dashsize*numdashes) > fabs(size-2*dashsize*(numdashes+1)) )
2216 numdashes++;
2217 dashsize = size/(2*numdashes);
2218 return(dashsize);
2219 }
2220
2221 /*
2222 #] ComputeDashGluonArc :
2223 #[ GluonHelp :
2224
2225 We draw the gluon in two strokes. This is due to the possibility
2226 of a dash pattern. We want the dashes to be nicely symmetric on
2227 the central windings. That means that either the start and end need
2228 a different size dashes, or we draw the start 'backwards'.
2229 We have chosen for the last solution.
2230 */
2231
2232 void GluonHelp(double *args,double dr)
2233 {
2234 int numwindings = args[5]+0.5;
2235 int numhalfwindings = 2*numwindings+2.1;
2236 double onehalfwinding = dr/numhalfwindings;
2237 double amp8 = fabs(args[4])*0.9;
2238 double xx = 2*onehalfwinding;
2239 int i;
2240 MoveTo(xx,-args[4]);
2241 Bezier(xx+amp8,-args[4],xx+amp8,args[4],1.4*onehalfwinding,args[4]);
2242 Bezier(0.5*onehalfwinding,args[4],
2243 0.1*onehalfwinding,args[4]*0.5,0,0);
2244 Stroke;
2245 MoveTo(xx,-args[4]);
2246 for ( i = 0; i < numwindings-1; i++ ) {
2247 Bezier(xx-amp8,-args[4], xx-amp8,args[4], xx+onehalfwinding,args[4]);
2248 xx += 2*onehalfwinding;
2249 Bezier(xx+amp8,args[4], xx+amp8,-args[4], xx,-args[4]);
2250 }
2251 Bezier(xx-amp8,-args[4], xx-amp8,args[4], xx+onehalfwinding*0.6,args[4]);
2252 Bezier(dr-onehalfwinding*0.5,args[4],
2253 dr-onehalfwinding*0.1,args[4]*0.5,
2254 dr,0);
2255 Stroke;
2256 }
2257
2258 /*
2259 #] GluonHelp :
2260 #[ GluonCircHelp :
2261 */
2262
2263 void GluonCircHelp(double *args)
2264 {
2265 int num = args[5], i;
2266 double ampi = args[4], radius = args[2];
2267 double darc;
2268 double dr,conv,inc;
2269 double amp1,amp2,amp4,amp5,amp8;
2270 double x1,x2,x3,y1,y2,y3,xx;
2271 darc = 360.;
2272 dr = darc*torad*radius;
2273 conv = 1.0/radius;
2274 inc = dr/(2*num); /* increment per half winding */
2275 amp8 = ampi*0.9;
2276 amp1 = radius+ampi;
2277 amp2 = radius-ampi;
2278 amp4 = amp1/cos((inc+amp8)*conv);
2279 amp5 = amp2/cos(amp8*conv);
2280 if ( amp8 < 0 ) amp8 = -amp8;
2281 xx = 2*inc;
2282 x3 = amp1*cos(inc*conv);
2283 y3 = amp1*sin(inc*conv);
2284 MoveTo(x3,y3);
2285 /*
2286 Now the loop
2287 */
2288 for ( i = 0; i < num; i++ ) {
2289 x1 = amp4*cos((xx+amp8)*conv);
2290 y1 = amp4*sin((xx+amp8)*conv);
2291 x2 = amp5*cos((xx+amp8)*conv);
2292 y2 = amp5*sin((xx+amp8)*conv);
2293 x3 = amp2*cos(xx*conv);
2294 y3 = amp2*sin(xx*conv);
2295 Bezier(x1,y1,x2,y2,x3,y3);
2296 x1 = amp5*cos((xx-amp8)*conv);
2297 y1 = amp5*sin((xx-amp8)*conv);
2298 x2 = amp4*cos((xx-amp8)*conv);
2299 y2 = amp4*sin((xx-amp8)*conv);
2300 x3 = amp1*cos((xx+inc)*conv);
2301 y3 = amp1*sin((xx+inc)*conv);
2302 Bezier(x1,y1,x2,y2,x3,y3);
2303 xx += 2*inc;
2304 }
2305 Stroke;
2306 }
2307
2308 /*
2309 #] GluonCircHelp :
2310 #[ GluonArcHelp :
2311 */
2312
2313 void GluonArcHelp(double *args, double darc, double ampi)
2314 {
2315 int num = args[6], i;
2316 double radius = args[2];
2317 double dr,conv,inc;
2318 double amp1,amp2,amp3,amp4,amp5,amp6,amp7,amp8;
2319 double x1,x2,x3,y1,y2,y3,xx,x1p,y1p,x2p,y2p,x3p,y3p;
2320 dr = darc*torad*radius;
2321 conv = 1.0/radius;
2322 inc = dr/(2*num+2); /* increment per half winding */
2323 amp8 = ampi*0.9;
2324 amp1 = radius+ampi;
2325 amp2 = radius-ampi;
2326 amp3 = radius+ampi/2;
2327 amp4 = amp1/cos((inc+amp8)*conv);
2328 amp5 = amp2/cos(amp8*conv);
2329 amp6 = amp1/cos((inc*0.6+amp8)*conv);
2330 amp7 = amp1/cos(inc*0.9*conv);
2331 if ( amp8 < 0 ) amp8 = -amp8;
2332 xx = 2*inc;
2333 /*
2334 First the starting part. We draw it separately because there could
2335 be a dashing pattern. This way the windings come out best.
2336 */
2337 x1 = amp3*cos(inc*0.1*conv);
2338 y1 = amp3*sin(inc*0.1*conv);
2339 x2 = amp7*cos(inc*0.5*conv);
2340 y2 = amp7*sin(inc*0.5*conv);
2341 x3 = amp1*cos(inc*1.4*conv);
2342 y3 = amp1*sin(inc*1.4*conv);
2343 x1p = amp6*cos((xx+amp8)*conv);
2344 y1p = amp6*sin((xx+amp8)*conv);
2345 x2p = amp5*cos((xx+amp8)*conv);
2346 y2p = amp5*sin((xx+amp8)*conv);
2347 x3p = amp2*cos(xx*conv);
2348 y3p = amp2*sin(xx*conv);
2349 MoveTo(x3p,y3p);
2350 Bezier(x2p,y2p,x1p,y1p,x3,y3);
2351 Bezier(x2,y2,x1,y1,radius,0);
2352 Stroke;
2353 /*
2354 Now the loop
2355 */
2356 MoveTo(x3p,y3p);
2357 for ( i = 1; i < num; i++ ) {
2358 x1 = amp5*cos((xx-amp8)*conv);
2359 y1 = amp5*sin((xx-amp8)*conv);
2360 x2 = amp4*cos((xx-amp8)*conv);
2361 y2 = amp4*sin((xx-amp8)*conv);
2362 x3 = amp1*cos((xx+inc)*conv);
2363 y3 = amp1*sin((xx+inc)*conv);
2364 Bezier(x1,y1,x2,y2,x3,y3);
2365 xx += 2*inc;
2366 x1 = amp4*cos((xx+amp8)*conv);
2367 y1 = amp4*sin((xx+amp8)*conv);
2368 x2 = amp5*cos((xx+amp8)*conv);
2369 y2 = amp5*sin((xx+amp8)*conv);
2370 x3 = amp2*cos(xx*conv);
2371 y3 = amp2*sin(xx*conv);
2372 Bezier(x1,y1,x2,y2,x3,y3);
2373 }
2374 /*
2375 And now the end point
2376 */
2377 x1 = amp5*cos((xx-amp8)*conv);
2378 y1 = amp5*sin((xx-amp8)*conv);
2379 x2 = amp6*cos((xx-amp8)*conv);
2380 y2 = amp6*sin((xx-amp8)*conv);
2381 x3 = amp1*cos((xx+inc*0.6)*conv);
2382 y3 = amp1*sin((xx+inc*0.6)*conv);
2383 Bezier(x1,y1,x2,y2,x3,y3);
2384 x1 = amp7*cos((xx+inc*1.5)*conv);
2385 y1 = amp7*sin((xx+inc*1.5)*conv);
2386 x2 = amp3*cos((dr-inc*0.1)*conv);
2387 y2 = amp3*sin((dr-inc*0.1)*conv);
2388 x3 = radius*cos(dr*conv);
2389 y3 = radius*sin(dr*conv);
2390
2391 Bezier(x1,y1,x2,y2,x3,y3);
2392
2393 Stroke;
2394 }
2395
2396 /*
2397 #] GluonArcHelp :
2398 #[ PhotonHelp :
2399 */
2400
2401 void PhotonHelp(double *args, double dr)
2402 {
2403 int numhalfwindings = args[5]*2+0.5;
2404 double onehalfwinding = dr/numhalfwindings;
2405 double y = 4.*args[4]/3;
2406 double x, xx;
2407 int i;
2408 MoveTo(0,0);
2409 /*
2410 Now loop over the half windings, alternating the sign of the y's
2411 */
2412 x = (4*onehalfwinding)/3/M_pi; xx = 0;
2413 for ( i = 0; i < numhalfwindings; i++, y = -y ) {
2414 Bezier(xx+x,y, xx+onehalfwinding-x,y, xx+onehalfwinding,0);
2415 xx += onehalfwinding;
2416 }
2417 Stroke;
2418 }
2419 /*
2420 #] PhotonHelp :
2421 #[ PhotonArcHelp :
2422 */
2423
2424 void PhotonArcHelp(double *args,double arcend,int num)
2425 {
2426 int i;
2427 double ampli = args[5], radius = args[2];
2428 double cp,sp,cp2,sp2,cpi,spi;
2429 double x1,x2,x3,y1,y2,y3,beta,tt;
2430
2431 cp = cos(arcend);
2432 sp = sin(arcend);
2433 cp2 = cos(arcend/2.);
2434 sp2 = sin(arcend/2.);
2435
2436 MoveTo(radius,0);
2437 for ( i = 0; i < num; i++, ampli = -ampli ) {
2438 cpi = cos(i*arcend);
2439 spi = sin(i*arcend);
2440 beta = radius*arcend/(M_pi*ampli);
2441 tt = (sp-cp*beta)/(cp+sp*beta);
2442 x2 = ((radius+ampli)*8*(beta*cp2-sp2)-(beta*(4+cp)
2443 +(tt*cp*3.-sp*4.))*radius)/((beta-tt)*3.);
2444 x1 = ((radius+ampli)*8.*cp2-(1+cp)*radius)/3.-x2;
2445 y1 = (x1-radius)*beta;
2446 y2 = (x2-radius*cp)*tt+radius*sp;
2447 x3 = radius*cp;
2448 y3 = radius*sp;
2449 Bezier(cpi*x1-spi*y1,cpi*y1+spi*x1,
2450 cpi*x2-spi*y2,cpi*y2+spi*x2,
2451 cpi*x3-spi*y3,cpi*y3+spi*x3);
2452 }
2453 Stroke;
2454 }
2455
2456 /*
2457 #] PhotonArcHelp :
2458 #[ ZigZagHelp :
2459 */
2460
2461 void ZigZagHelp(double *args, double dr)
2462 {
2463 int numhalfwindings = args[5]*2+0.5;
2464 double onehalfwinding = dr/numhalfwindings;
2465 double x = onehalfwinding, y = args[4];
2466 int i;
2467 MoveTo(0,0);
2468 /*
2469 Now loop over the half windings, alternating the sign of the y's
2470 */
2471 for ( i = 0; i < numhalfwindings; i++, y = -y ) {
2472 LineTo(x-onehalfwinding/2.,y); LineTo(x,0);
2473 x += onehalfwinding;
2474 }
2475 Stroke;
2476 }
2477
2478 /*
2479 #] ZigZagHelp :
2480 #[ ZigZagArcHelp :
2481 */
2482
2483 void ZigZagArcHelp(double *args)
2484 {
2485 int num = 2*args[6]-0.5, i;
2486 double amp = args[5], r = args[2];
2487 double arcstart = args[3], arcend = args[4], darc;
2488 if ( arcend < arcstart ) arcend += 360.;
2489 darc = (arcend-arcstart)/(num+1);
2490 MoveTo(r*COS(arcstart),r*SIN(arcstart));
2491 arcstart += darc/2;
2492 for ( i = 0; i <= num; i++, amp = -amp ) {
2493 LineTo((r+amp)*COS(arcstart+darc*i),(r+amp)*SIN(arcstart+darc*i));
2494 }
2495 LineTo(r*COS(arcend),r*SIN(arcend));
2496 Stroke;
2497 }
2498
2499 /*
2500 #] ZigZagArcHelp :
2501 #[ Gluon : *
2502
2503 Gluon(x1,y1)(x2,y2){amplitude}{windings}
2504
2505 Each half winding is one cubic Bezier curve.
2506 In addition the end points are different Bezier curves.
2507 */
2508
2509 void Gluon(double *args)
2510 {
2511 double dx = args[2] - args[0];
2512 double dy = args[3] - args[1];
2513 double dr = sqrt(dx*dx+dy*dy);
2514
2515 SetTransferMatrix(1,0,0,1,args[0],args[1]);
2516 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
2517 GluonHelp(args,dr);
2518 }
2519
2520 /*
2521 #] Gluon :
2522 #[ DashGluon : *
2523
2524 DashGluon(x1,y1)(x2,y2){amplitude}{windings}{dashsize}
2525
2526 Each half winding is one cubic Bezier curve.
2527 In addition the end points are different Bezier curves.
2528 */
2529
2530 void DashGluon(double *args)
2531 {
2532 double dx = args[2] - args[0];
2533 double dy = args[3] - args[1];
2534 double dr = sqrt(dx*dx+dy*dy);
2535 double dashsize;
2536
2537 SetTransferMatrix(1,0,0,1,args[0],args[1]);
2538 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
2539
2540 dashsize = ComputeDash(args,dr,args[6]);
2541 SetDashSize(dashsize,dashsize/2);
2542 GluonHelp(args,dr);
2543 }
2544
2545 /*
2546 #] DashGluon :
2547 #[ GluonCirc : *
2548
2549 GluonCirc(x1,y1)(r,phi){amplitude}{windings}
2550
2551 Draws a gluon on a circle
2552 x_center,y_center,radius,phase_angle,gluon_radius,num
2553 in which num is the number of windings of the gluon.
2554 Method: Same as GluonArc, but without special start and end
2555 */
2556
2557 void GluonCirc(double *args)
2558 {
2559 int num = args[5];
2560 double arcstart = args[3];
2561
2562 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
2563
2564 arcstart += 360./(2*num); /* extra phase to make 0 angle more accessible */
2565
2566 SetTransferMatrix(COS(arcstart),SIN(arcstart)
2567 ,-SIN(arcstart),COS(arcstart),0,0);
2568
2569 GluonCircHelp(args);
2570 }
2571
2572 /*
2573 #] GluonCirc :
2574 #[ DashGluonCirc : *
2575
2576 DashGluonCirc(x1,y1)(r,phi){amplitude}{windings}{dashsize}
2577
2578 Draws a gluon on a circle
2579 x_center,y_center,radius,phase_angle,gluon_radius,num
2580 in which num is the number of windings of the gluon.
2581 Method: Same as GluonArc, but without special start and end
2582 */
2583
2584 void DashGluonCirc(double *args)
2585 {
2586 int num = args[5];
2587 double arcstart = args[3], dashsize;
2588
2589 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
2590
2591 arcstart += 360./(2*num); /* extra phase to make 0 angle more accessible */
2592
2593 SetTransferMatrix(COS(arcstart),SIN(arcstart)
2594 ,-SIN(arcstart),COS(arcstart),0,0);
2595
2596 dashsize = ComputeDashCirc(args,args[6]);
2597
2598 SetDashSize(dashsize,dashsize/2);
2599 GluonCircHelp(args);
2600 }
2601
2602 /*
2603 #] DashGluonCirc :
2604 #[ GluonArc : *
2605
2606 GluonArc(x1,y1)(r,phi1,phi2){amplitude}{windings}
2607
2608 Draws a gluon on an arcsegment
2609 x_center,y_center,radius,stat_angle,end_angle,gluon_radius,num
2610 in which num is the number of windings of the gluon.
2611 Method:
2612 1: compute length of arc.
2613 2: generate gluon in x and y as if the arc is a straight line
2614 3: x' = (radius+y)*cos(x*const)
2615 y' = (radius+y)*sin(x*const)
2616 */
2617
2618 void GluonArc(double *args)
2619 {
2620 double darc, arcstart = args[3],arcend = args[4], ampi = args[5];
2621 /*
2622 When arcend comes before arcstart we have a problem. The solution is
2623 to flip the order and change the sign on ampi
2624 */
2625 if ( arcend < arcstart ) {
2626 darc = arcstart; arcstart = arcend; arcend = darc; ampi = -ampi;
2627 }
2628
2629 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
2630
2631 SetTransferMatrix(COS(arcstart),SIN(arcstart)
2632 ,-SIN(arcstart),COS(arcstart),0,0);
2633 darc = arcend-arcstart;
2634 GluonArcHelp(args,darc,ampi);
2635 }
2636
2637 /*
2638 #] GluonArc :
2639 #[ DashGluonArc : *
2640
2641 DashGluonArc(x1,y1)(r,phi1,phi2){amplitude}{windings}{dashsize}
2642
2643 Draws a gluon on an arcsegment
2644 x_center,y_center,radius,stat_angle,end_angle,gluon_radius,num
2645 in which num is the number of windings of the gluon.
2646 Method:
2647 1: compute length of arc.
2648 2: generate gluon in x and y as if the arc is a straight line
2649 3: x' = (radius+y)*cos(x*const)
2650 y' = (radius+y)*sin(x*const)
2651 */
2652
2653 void DashGluonArc(double *args)
2654 {
2655 double darc, arcstart = args[3],arcend = args[4], ampi = args[5];
2656 double dashsize = args[7];
2657 /*
2658 When arcend comes before arcstart we have a problem. The solution is
2659 to flip the order and change the sign on ampi
2660 */
2661 if ( arcend < arcstart ) {
2662 darc = arcstart; arcstart = arcend; arcend = darc; ampi = -ampi;
2663 }
2664
2665 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
2666
2667 SetTransferMatrix(COS(arcstart),SIN(arcstart)
2668 ,-SIN(arcstart),COS(arcstart),0,0);
2669 darc = arcend-arcstart;
2670 dashsize = ComputeDashGluonArc(args,darc,dashsize);
2671 SetDashSize(dashsize,dashsize/2);
2672 GluonArcHelp(args,darc,ampi);
2673 }
2674
2675 /*
2676 #] DashGluonArc :
2677 #[ Photon : *
2678
2679 Photon(x1,y1)(x2,y2){amplitude}{windings}
2680
2681 Each half winding is one cubic Bezier curve.
2682 */
2683
2684 void Photon(double *args)
2685 {
2686 double dx = args[2] - args[0];
2687 double dy = args[3] - args[1];
2688 double dr = sqrt(dx*dx+dy*dy);
2689
2690 SetTransferMatrix(1,0,0,1,args[0],args[1]);
2691 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
2692
2693 PhotonHelp(args,dr);
2694 }
2695
2696 /*
2697 #] Photon :
2698 #[ DoublePhoton : *
2699
2700 DoublePhoton(x1,y1)(x2,y2){amplitude}{windings}{sep}
2701
2702 Each half winding is one cubic Bezier curve.
2703 */
2704
2705 void DoublePhoton(double *args)
2706 {
2707 double dx = args[2] - args[0];
2708 double dy = args[3] - args[1];
2709 double dr = sqrt(dx*dx+dy*dy);
2710 linesep = args[6];
2711
2712 SetTransferMatrix(1,0,0,1,args[0],args[1]);
2713 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
2714
2715 SaveGraphicsState;
2716 SetLineWidth(linesep+axolinewidth);
2717 PhotonHelp(args,dr);
2718 RestoreGraphicsState;
2719
2720 SaveGraphicsState;
2721 SetLineWidth(linesep-axolinewidth);
2722 SetBackgroundColor(STROKING);
2723 PhotonHelp(args,dr);
2724 RestoreGraphicsState;
2725 }
2726
2727 /*
2728 #] DoublePhoton :
2729 #[ DashPhoton : *
2730
2731 DashPhoton(x1,y1)(x2,y2){amplitude}{windings}{dashsize}
2732
2733 Each half winding is one cubic Bezier curve.
2734 */
2735
2736 void DashPhoton(double *args)
2737 {
2738 double dx = args[2] - args[0];
2739 double dy = args[3] - args[1];
2740 double dr = sqrt(dx*dx+dy*dy);
2741 int numdashes, numhalfwindings = args[5]*2+0.5;
2742 double x, y, size;
2743 double dashsize = args[6], onehalfwinding = dr/numhalfwindings;
2744 x = (4*onehalfwinding)/3/M_pi; y = 4.*args[4]/3;
2745 size = 0.5*LengthBezier(x,y, onehalfwinding-x,y, onehalfwinding,0,1.0);
2746 numdashes = size/(2*args[6]);
2747 if ( numdashes == 0 ) numdashes = 1;
2748 if ( fabs(size-2*dashsize*numdashes) > fabs(size-2*dashsize*(numdashes+1)) )
2749 numdashes++;
2750 dashsize = size/(2*numdashes);
2751
2752 SetTransferMatrix(1,0,0,1,args[0],args[1]);
2753 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
2754
2755 SetDashSize(dashsize,dashsize/2);
2756 PhotonHelp(args,dr);
2757 }
2758
2759 /*
2760 #] DashPhoton :
2761 #[ DashDoublePhoton : *
2762
2763 DashDoublePhoton(x1,y1)(x2,y2){amplitude}{windings}{sep}{dashsize}
2764
2765 Each half winding is one cubic Bezier curve.
2766 */
2767
2768 void DashDoublePhoton(double *args)
2769 {
2770 double dx = args[2] - args[0];
2771 double dy = args[3] - args[1];
2772 double dr = sqrt(dx*dx+dy*dy);
2773 int numdashes, numhalfwindings = args[5]*2+0.5;
2774 double x, y, size;
2775 double dashsize = args[7], onehalfwinding = dr/numhalfwindings;
2776 x = (4*onehalfwinding)/3/M_pi; y = 4.*args[4]/3;
2777 size = 0.5*LengthBezier(x,y, onehalfwinding-x,y, onehalfwinding,0,1.0);
2778 numdashes = size/(2*args[6]);
2779 if ( numdashes == 0 ) numdashes = 1;
2780 if ( fabs(size-2*dashsize*numdashes) > fabs(size-2*dashsize*(numdashes+1)) )
2781 numdashes++;
2782 dashsize = size/(2*numdashes);
2783
2784 linesep = args[6];
2785
2786 SetTransferMatrix(1,0,0,1,args[0],args[1]);
2787 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
2788 SetDashSize(dashsize,dashsize/2);
2789
2790 SaveGraphicsState;
2791 SetLineWidth(linesep+axolinewidth);
2792 PhotonHelp(args,dr);
2793 RestoreGraphicsState;
2794
2795 /* SetDashSize(0,0); */
2796 SaveGraphicsState;
2797 SetLineWidth(linesep-axolinewidth);
2798 SetBackgroundColor(STROKING);
2799 PhotonHelp(args,dr);
2800 RestoreGraphicsState;
2801 }
2802
2803 /*
2804 #] DashDoublePhoton :
2805 #[ PhotonArc : *
2806
2807 PhotonArc(x1,y1)(r,phi1,phi2){amplitude}{windings}
2808
2809 This routine follows the Postscript routine closely, except for that
2810 we do not put a transfer matrix inside the loop. The corresponding
2811 moveto messes up the path. One would have to put stroking operations
2812 in there each time.
2813 */
2814
2815 void PhotonArc(double *args)
2816 {
2817 double arcstart = args[3],arcend = args[4];
2818 int num = 2*args[6]+0.5;
2819
2820 if ( arcend < arcstart ) arcend += 360.;
2821
2822 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
2823
2824 arcend = torad*(arcend-arcstart)/num;
2825
2826 SetTransferMatrix(COS(arcstart),SIN(arcstart)
2827 ,-SIN(arcstart),COS(arcstart),0,0);
2828
2829 PhotonArcHelp(args,arcend,num);
2830 }
2831
2832 /*
2833 #] PhotonArc :
2834 #[ DoublePhotonArc : *
2835
2836 DoublePhotonArc(x1,y1)(r,phi1,phi2){amplitude}{windings}{sep}
2837 */
2838
2839 void DoublePhotonArc(double *args)
2840 {
2841 double arcstart = args[3],arcend = args[4];
2842 int num = 2*args[6]+0.5;
2843 linesep = args[7];
2844
2845 if ( arcend < arcstart ) arcend += 360.;
2846
2847 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
2848
2849 arcend = torad*(arcend-arcstart)/num;
2850
2851 SetTransferMatrix(COS(arcstart),SIN(arcstart)
2852 ,-SIN(arcstart),COS(arcstart),0,0);
2853
2854 SaveGraphicsState;
2855 SetLineWidth(linesep+axolinewidth);
2856 PhotonArcHelp(args,arcend,num);
2857 RestoreGraphicsState;
2858
2859 SaveGraphicsState;
2860 SetLineWidth(linesep-axolinewidth);
2861 SetBackgroundColor(STROKING);
2862 PhotonArcHelp(args,arcend,num);
2863 RestoreGraphicsState;
2864 }
2865
2866 /*
2867 #] DoublePhotonArc :
2868 #[ DashPhotonArc : *
2869
2870 DashPhotonArc(x1,y1)(r,phi1,phi2){amplitude}{windings}{dashsize}
2871 */
2872
2873 void DashPhotonArc(double *args)
2874 {
2875 double arcstart = args[3],arcend = args[4];
2876 double dashsize = args[7], dashstart;
2877 int num = 2*args[6]+0.5;
2878
2879 if ( arcend < arcstart ) arcend += 360.;
2880
2881 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
2882
2883 arcend = torad*(arcend-arcstart)/num;
2884
2885 SetTransferMatrix(COS(arcstart),SIN(arcstart)
2886 ,-SIN(arcstart),COS(arcstart),0,0);
2887
2888 dashsize = ComputeDashPhotonArc(args,arcend,dashsize,&dashstart);
2889 SetDashSize(dashsize,dashstart);
2890 PhotonArcHelp(args,arcend,num);
2891 }
2892
2893 /*
2894 #] DashPhotonArc :
2895 #[ DashDoublePhotonArc : *
2896
2897 DashDoublePhotonArc(x1,y1)(r,phi1,phi2){amplitude}{windings}{sep}{dashsize}
2898 */
2899
2900 void DashDoublePhotonArc(double *args)
2901 {
2902 double arcstart = args[3],arcend = args[4];
2903 double dashsize = args[8], dashstart;
2904 int num = 2*args[6]+0.5;
2905 linesep = args[7];
2906
2907 if ( arcend < arcstart ) arcend += 360.;
2908
2909 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
2910
2911 arcend = torad*(arcend-arcstart)/num;
2912
2913 SetTransferMatrix(COS(arcstart),SIN(arcstart)
2914 ,-SIN(arcstart),COS(arcstart),0,0);
2915 dashsize = ComputeDashPhotonArc(args,arcend,dashsize,&dashstart);
2916
2917 SaveGraphicsState;
2918 SetDashSize(dashsize,dashstart);
2919 SetLineWidth(linesep+axolinewidth);
2920 PhotonArcHelp(args,arcend,num);
2921 RestoreGraphicsState;
2922
2923 SaveGraphicsState;
2924 SetDashSize(0,0);
2925 SetLineWidth(linesep-axolinewidth);
2926 SetBackgroundColor(STROKING);
2927 PhotonArcHelp(args,arcend,num);
2928 RestoreGraphicsState;
2929 }
2930
2931 /*
2932 #] DashDoublePhotonArc :
2933 #[ ZigZag : *
2934
2935 ZigZag(x1,y1)(x2,y2){amplitude}{windings}
2936
2937 We draw each half winding as two straight lines.
2938 This can be done better!
2939 */
2940
2941 void ZigZag(double *args)
2942 {
2943 double dx = args[2] - args[0];
2944 double dy = args[3] - args[1];
2945 double dr = sqrt(dx*dx+dy*dy);
2946
2947 SetTransferMatrix(1,0,0,1,args[0],args[1]);
2948 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
2949 ZigZagHelp(args,dr);
2950 }
2951
2952 /*
2953 #] ZigZag :
2954 #[ DoubleZigZag : *
2955
2956 DoubleZigZag(x1,y1)(x2,y2){amplitude}{windings}{sep}
2957
2958 We draw each half winding as two straight lines.
2959 This can be done better!
2960 */
2961
2962 void DoubleZigZag(double *args)
2963 {
2964 double dx = args[2] - args[0];
2965 double dy = args[3] - args[1];
2966 double dr = sqrt(dx*dx+dy*dy);
2967 linesep = args[6];
2968
2969 SetTransferMatrix(1,0,0,1,args[0],args[1]);
2970 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
2971
2972 SaveGraphicsState;
2973 SetLineWidth(linesep+axolinewidth);
2974 ZigZagHelp(args,dr);
2975 RestoreGraphicsState;
2976
2977 SaveGraphicsState;
2978 SetLineWidth(linesep-axolinewidth);
2979 SetBackgroundColor(STROKING);
2980 ZigZagHelp(args,dr);
2981 RestoreGraphicsState;
2982 }
2983
2984 /*
2985 #] DoubleZigZag :
2986 #[ DashZigZag : *
2987
2988 DashZigZag(x1,y1)(x2,y2){amplitude}{windings}{dashsize}
2989
2990 We should recalculate the size of the dashes. Otherwise the points
2991 of the teeth can become messy.
2992 */
2993
2994 void DashZigZag(double *args)
2995 {
2996 double dx = args[2] - args[0];
2997 double dy = args[3] - args[1];
2998 double dr = sqrt(dx*dx+dy*dy);
2999 double dashsize = args[6];
3000 int n = args[5]*2+0.5;
3001 double size = dr/(n*2);
3002
3003 SetTransferMatrix(1,0,0,1,args[0],args[1]);
3004 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
3005
3006 size = sqrt(size*size+args[4]*args[4]);
3007 n = size/(2*dashsize); /* number of complete dash patterns rounded down */
3008 /*
3009 Now test what is closer to dash: size/n or size/(n+1)
3010 */
3011 if ( n == 0 ) n = 1;
3012 if ( fabs(size-2*dashsize*n) > fabs(size-2*dashsize*(n+1)) ) n++;
3013 dashsize = size/(2*n);
3014
3015 SetDashSize(dashsize,dashsize/2);
3016 ZigZagHelp(args,dr);
3017 }
3018
3019 /*
3020 #] DashZigZag :
3021 #[ DashDoubleZigZag : *
3022
3023 DashDoubleZigZag(x1,y1)(x2,y2){amplitude}{windings}{sep}{dashsize}
3024
3025 We draw each half winding as two straight lines.
3026 This can be done better!
3027 */
3028
3029 void DashDoubleZigZag(double *args)
3030 {
3031 double dx = args[2] - args[0];
3032 double dy = args[3] - args[1];
3033 double dr = sqrt(dx*dx+dy*dy);
3034 double dashsize = args[7];
3035 int n = args[5]*2+0.5;
3036 double size = dr/(n*2);
3037 linesep = args[6];
3038
3039 SetTransferMatrix(1,0,0,1,args[0],args[1]);
3040 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
3041
3042 size = sqrt(size*size+args[4]*args[4]);
3043 n = size/(2*dashsize); /* number of complete dash patterns rounded down */
3044 /*
3045 Now test what is closer to dash: size/n or size/(n+1)
3046 */
3047 if ( n == 0 ) n = 1;
3048 if ( fabs(size-2*dashsize*n) > fabs(size-2*dashsize*(n+1)) ) n++;
3049 dashsize = size/(2*n);
3050
3051 SetDashSize(dashsize,dashsize/2);
3052
3053 SaveGraphicsState;
3054 SetLineWidth(linesep+axolinewidth);
3055 ZigZagHelp(args,dr);
3056 RestoreGraphicsState;
3057
3058 SetDashSize(0,0);
3059
3060 SaveGraphicsState;
3061 SetLineWidth(linesep-axolinewidth);
3062 SetBackgroundColor(STROKING);
3063 ZigZagHelp(args,dr);
3064 RestoreGraphicsState;
3065 }
3066
3067 /*
3068 #] DashDoubleZigZag :
3069 #[ ZigZagArc : *
3070
3071 ZigZagArc(x1,y1)(r,phi1,phi2){amplitude}{windings}
3072 */
3073
3074 void ZigZagArc(double *args)
3075 {
3076 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
3077 ZigZagArcHelp(args);
3078 }
3079
3080 /*
3081 #] ZigZagArc :
3082 #[ DoubleZigZagArc : *
3083
3084 DoubleZigZagArc(x1,y1)(r,phi1,phi2){amplitude}{windings}{sep}
3085 */
3086
3087 void DoubleZigZagArc(double *args)
3088 {
3089 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
3090 linesep = args[7];
3091
3092 SaveGraphicsState;
3093 SetLineWidth(linesep+axolinewidth);
3094 ZigZagArcHelp(args);
3095 RestoreGraphicsState;
3096
3097 SaveGraphicsState;
3098 SetLineWidth(linesep-axolinewidth);
3099 SetBackgroundColor(STROKING);
3100 ZigZagArcHelp(args);
3101 RestoreGraphicsState;
3102 }
3103
3104 /*
3105 #] DoubleZigZagArc :
3106 #[ DashZigZagArc : *
3107
3108 DashZigZagArc(x1,y1)(r,phi1,phi2){amplitude}{windings}{dashsize}
3109 */
3110
3111 void DashZigZagArc(double *args)
3112 {
3113 double dashsize = args[7], dashstart;
3114 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
3115 {
3116 int num = 2*args[6]-0.5, numdashes;
3117 double amp = args[5], r = args[2], size, size2;
3118 double arcstart = args[3], arcend = args[4], darc;
3119 if ( arcend < arcstart ) arcend += 360.;
3120 darc = (arcend-arcstart)/(num+1);
3121 size = sqrt(0.5*(amp*amp+r*r-(r*r-amp*amp)*COS(darc)));
3122 size2 = sqrt(amp*amp+2*(amp+r)*r*(1-COS(darc/2)));
3123 numdashes = size/(2*dashsize);
3124 if ( numdashes == 0 ) numdashes = 1;
3125 if ( fabs(size-2*dashsize*numdashes) > fabs(size-2*dashsize*(numdashes+1)) )
3126 numdashes++;
3127 dashsize = size/(2*numdashes);
3128 num = size2/(2*dashsize);
3129 dashstart = -(size2 - 2*num*dashsize) + dashsize/2;
3130 if ( dashstart < 0 ) dashstart += 2*dashsize;
3131 }
3132 SetDashSize(dashsize,dashstart);
3133 ZigZagArcHelp(args);
3134 }
3135
3136 /*
3137 #] DashZigZagArc :
3138 #[ DashDoubleZigZagArc : *
3139
3140 DashDoubleZigZagArc(x1,y1)(r,phi1,phi2){amplitude}{windings}{sep}{dashsize}
3141 */
3142
3143 void DashDoubleZigZagArc(double *args)
3144 {
3145 double dashsize = args[8], dashstart;
3146 SetTransferMatrix(1,0,0,1,args[0],args[1]); /* Move to center of circle */
3147 linesep = args[7];
3148 {
3149 int num = 2*args[6]-0.5, numdashes;
3150 double amp = args[5], r = args[2], size, size2;
3151 double arcstart = args[3], arcend = args[4], darc;
3152 if ( arcend < arcstart ) arcend += 360.;
3153 darc = (arcend-arcstart)/(num+1);
3154 size = sqrt(0.5*(amp*amp+r*r-(r*r-amp*amp)*COS(darc)));
3155 size2 = sqrt(amp*amp+2*(amp+r)*r*(1-COS(darc/2)));
3156 numdashes = size/(2*dashsize);
3157 if ( numdashes == 0 ) numdashes = 1;
3158 if ( fabs(size-2*dashsize*numdashes) > fabs(size-2*dashsize*(numdashes+1)) )
3159 numdashes++;
3160 dashsize = size/(2*numdashes);
3161 num = size2/(2*dashsize);
3162 dashstart = -(size2 - 2*num*dashsize) + dashsize/2;
3163 if ( dashstart < 0 ) dashstart += 2*dashsize;
3164 }
3165
3166 SaveGraphicsState;
3167 SetDashSize(dashsize,dashstart);
3168 SetLineWidth(linesep+axolinewidth);
3169 ZigZagArcHelp(args);
3170 RestoreGraphicsState;
3171
3172 SaveGraphicsState;
3173 SetDashSize(0,0);
3174 SetLineWidth(linesep-axolinewidth);
3175 SetBackgroundColor(STROKING);
3176 ZigZagArcHelp(args);
3177 RestoreGraphicsState;
3178 }
3179
3180 /*
3181 #] DashDoubleZigZagArc :
3182 #] Particle routines :
3183 #[ Drawing routines :
3184 #[ Polygon :
3185 */
3186
3187 void Polygon(double *args,int num,int type)
3188 {
3189 int i;
3190 MoveTo(args[0],args[1]);
3191 args += 2;
3192 for ( i = 1; i < num; i++, args += 2 ) {
3193 LineTo(args[0],args[1]);
3194 }
3195 if ( type == 0 ) { CloseAndStroke; }
3196 else if ( type == 1 ) { CloseAndFill; }
3197 }
3198
3199 /*
3200 #] Polygon :
3201 #[ Curve : +
3202
3203 Curve{(x1,y1),...,(xn,yn)}
3204
3205 num is the number of pairs in points.
3206 */
3207
3208 void Curve(double *points,int num)
3209 {
3210 int i, ss;
3211 double x0,y0,x1,y1,x2,y2,x3,y3;
3212
3213 if ( num < 2 ) return;
3214 if ( num == 2 ) { Line(points); return; }
3215
3216 ss = 2*num;
3217
3218 x1 = points[0]; y1 = points[1];
3219 x2 = points[2]; y2 = points[3];
3220 x3 = points[4]; y3 = points[5];
3221 x0 = 2*x1-x2;
3222 y0 = 2*((y3-y2)/(x3-x2)-(y2-y1)/(x2-x1))*((x2-x1)*(x2-x1)/(x3-x1))+2*y1-y2;
3223
3224 MoveTo(x1,y1);
3225 DoCurve(x0,y0,x1,y1,x2,y2,x3,y3);
3226
3227 for ( i = 0; i < ss-6; i += 2 ) {
3228 DoCurve(points[i ],points[i+1],points[i+2],points[i+3],
3229 points[i+4],points[i+5],points[i+6],points[i+7]);
3230 }
3231 if ( ss > 6 ) {
3232 x0 = points[ss-6]; y0 = points[ss-5];
3233 x1 = points[ss-4]; y1 = points[ss-3];
3234 x2 = points[ss-2]; y2 = points[ss-1];
3235 x3 = 2*x2-x1;
3236 y3 = 2*((y2-y1)/(x2-x1)-(y1-y0)/(x1-x0))*((x2-x1)*(x2-x1)/(x2-x0))+2*y2-y1;
3237
3238 DoCurve(x0,y0,x1,y1,x2,y2,x3,y3);
3239 }
3240 Stroke;
3241 }
3242
3243 /*
3244 #] Curve :
3245 #[ DashCurve : +
3246
3247 DashCurve{(x1,y1),...,(xn,yn)}{dashsize}
3248 */
3249
3250 void DashCurve(double *args,int num1)
3251 {
3252 double dashsize = args[2*num1];
3253 if ( num1 == 2 ) {
3254 DashLine(args);
3255 }
3256 else if ( num1 > 2 ) {
3257 SetDashSize(dashsize,dashsize/2);
3258 Curve(args,num1);
3259 }
3260 }
3261
3262 /*
3263 #] DashCurve :
3264 #[ LogAxis :
3265
3266 Draws a line with logarithmic hash marks along it.
3267 LogAxis(x1,y1)(x2,y2)(num_logs,hashsize,offset,width)
3268 The line is from (x1,y1) to (x2,y2) and the marks are on the left side
3269 when hashsize is positive, and right when it is negative.
3270 num_logs is the number of orders of magnitude and offset is the number
3271 at which one starts at (x1,y1) (like if offset=2 we start at 2)
3272 When offset is 0 we start at 1. Width is the linewidth.
3273 */
3274
3275 void LogAxis(double *args)
3276 {
3277 double dx = args[2]-args[0], dy = args[3]-args[1], dr = sqrt(dx*dx+dy*dy);
3278 double width = args[7], size, nlogs = args[4], hashsize = args[5];
3279 double offset = args[6], x;
3280 int i, j;
3281 SetTransferMatrix(1,0,0,1,args[0],args[1]);
3282 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
3283 MoveTo(0,0); LineTo(dr,0); Stroke;
3284 /*
3285 Now compute the hash marks.
3286 */
3287 size = dr/nlogs;
3288 if ( offset <= 0 ) { offset = 0; }
3289 else { offset = mod( log10(offset), 1 ); }
3290 /*
3291 Big hash marks
3292 */
3293 for ( i = 0; i <= nlogs+1; i++ ) {
3294 x = (i-offset)*size;
3295 if ( x >= -0.001 && x <= dr+0.001 ) {
3296 MoveTo(x,0);
3297 LineTo(x,hashsize*1.2);
3298 Stroke;
3299 }
3300 }
3301 /*
3302 Little hash marks
3303 */
3304 SetLineWidth(0.6*width);
3305 for ( i = 0; i <= nlogs; i++ ) {
3306 for ( j = 2; j < 10; j++ ) {
3307 x = (i-offset+log10(j))*size;
3308 if ( x >= -0.001 && x <= dr+0.001 ) {
3309 MoveTo(x,0); LineTo(x,hashsize*0.8); Stroke;
3310 }
3311 }
3312 }
3313 }
3314
3315 /*
3316 #] LogAxis :
3317 #[ LinAxis :
3318
3319 Draws a line with linear hash marks along it.
3320 LinAxis(x1,y1)(x2,y2)(num_decs,per_dec,hashsize,offset,width)
3321 The line is from (x1,y1) to (x2,y2) and the marks are on the left side
3322 when hashsize is positive, and right when it is negative.
3323 num_decs is the number of accented marks, per_dec the number of
3324 divisions between them and offset is the number
3325 at which one starts at (x1,y1) (like if offset=2 we start at the second
3326 small mark) Width is the linewidth.
3327 */
3328
3329 void LinAxis(double *args)
3330 {
3331 double width = args[8], hashsize = args[6], x;
3332 double dx = args[2]-args[0], dy = args[3]-args[1], dr = sqrt(dx*dx+dy*dy);
3333 double num_decs = args[4], per_dec = args[5], size, size2;
3334 double offset = args[7];
3335 int i, j, numperdec = per_dec+0.5;
3336 SetTransferMatrix(1,0,0,1,args[0],args[1]);
3337 SetTransferMatrix(dx/dr,dy/dr,-dy/dr,dx/dr,0,0);
3338 MoveTo(0,0); LineTo(dr,0); Stroke;
3339 size = dr/num_decs;
3340 if ( numperdec > 1 ) { size2 = size / numperdec; }
3341 else { size2 = size; numperdec = 1; }
3342 offset = mod( offset, numperdec );
3343 /*
3344 In geometric calculations of positions of hash marks, note that offset can
3345 be between 0 and numperdec, and that within rounding error, numperdec is
3346 allowed. Also allow for positions of hash marks that if calculated exactly
3347 are in range, but because of rounding error are slightly outside.
3348 */
3349 /*
3350 Big hash marks:
3351 */
3352 for ( i = 0; i <= num_decs+1; i++ ) {
3353 x = i*size - offset*size2;
3354 if ( ( x > -0.001) && ( x < dr+0.001 ) ) {
3355 MoveTo(x,0); LineTo(x,hashsize*1.2); Stroke;
3356 }
3357 }
3358 /*
3359 Little hash marks.
3360 */
3361 SetLineWidth(0.6*width);
3362 for ( i = 0; i <= num_decs; i++ ) {
3363 for ( j = 1; j < numperdec; j++ ) {
3364 x = i*size + (j - offset)*size2;
3365 if ( ( x > -0.001) && ( x < dr+0.001 ) ) {
3366 MoveTo(x,0); LineTo(x,hashsize*0.8); Stroke;
3367 }
3368 }
3369 }
3370 }
3371
3372 /*
3373 #] LinAxis :
3374 #[ BezierCurve :
3375
3376 Draws a Bezier curve. Starts at (x1,y1).
3377 The control points are (x2,y2),(x3,y3),(x4,y4)
3378 */
3379
3380 void BezierCurve(double *args)
3381 {
3382 MoveTo(args[0],args[1]);
3383 Bezier(args[2],args[3],args[4],args[5],args[6],args[7]);
3384 Stroke;
3385 if ( witharrow ) BezierArrow(args);
3386 }
3387
3388 /*
3389 #] BezierCurve :
3390 #[ DoubleBezier :
3391
3392 Draws a Bezier curve. Starts at (x1,y1).
3393 The control points are (x2,y2),(x3,y3),(x4,y4)
3394 */
3395
3396 void DoubleBezier(double *args)
3397 {
3398 linesep = args[8];
3399 SaveGraphicsState;
3400 SetLineWidth(linesep+axolinewidth);
3401 MoveTo(args[0],args[1]);
3402 Bezier(args[2],args[3],args[4],args[5],args[6],args[7]);
3403 Stroke;
3404 RestoreGraphicsState;
3405 SaveGraphicsState;
3406 SetLineWidth(linesep-axolinewidth);
3407 SetBackgroundColor(STROKING);
3408 MoveTo(args[0],args[1]);
3409 Bezier(args[2],args[3],args[4],args[5],args[6],args[7]);
3410 Stroke;
3411 RestoreGraphicsState;
3412 if ( witharrow ) BezierArrow(args);
3413 }
3414
3415 /*
3416 #] DoubleBezier :
3417 #[ DashBezier :
3418
3419 Draws a Bezier curve. Starts at (x1,y1).
3420 The control points are (x2,y2),(x3,y3),(x4,y4)
3421 */
3422
3423 void DashBezier(double *args)
3424 {
3425 int numdashes;
3426 double size, dashsize = args[8];
3427 size = LengthBezier(args[2]-args[0],args[3]-args[1]
3428 ,args[4]-args[0],args[5]-args[1],args[6]-args[0],args[7]-args[1],1.0);
3429
3430 numdashes = size/(2*dashsize);
3431 if ( numdashes == 0 ) numdashes = 1;
3432 if ( fabs(size-2*dashsize*numdashes) > fabs(size-2*dashsize*(numdashes+1)) )
3433 numdashes++;
3434 dashsize = (size/(2*numdashes));
3435
3436 SetDashSize(dashsize,dashsize/2);
3437 MoveTo(args[0],args[1]);
3438 Bezier(args[2],args[3],args[4],args[5],args[6],args[7]);
3439 Stroke;
3440 if ( witharrow ) BezierArrow(args);
3441 }
3442
3443 /*
3444 #] DashBezier :
3445 #[ DashDoubleBezier :
3446
3447 Draws a Bezier curve. Starts at (x1,y1).
3448 The control points are (x2,y2),(x3,y3),(x4,y4)
3449 */
3450
3451 void DashDoubleBezier(double *args)
3452 {
3453 int numdashes;
3454 double size, dashsize = args[9];
3455 size = LengthBezier(args[2]-args[0],args[3]-args[1]
3456 ,args[4]-args[0],args[5]-args[1],args[6]-args[0],args[7]-args[1],1.0);
3457
3458 numdashes = size/(2*dashsize);
3459 if ( numdashes == 0 ) numdashes = 1;
3460 if ( fabs(size-2*dashsize*numdashes) > fabs(size-2*dashsize*(numdashes+1)) )
3461 numdashes++;
3462 dashsize = (size/(2*numdashes));
3463
3464 SetDashSize(dashsize,dashsize/2);
3465 linesep = args[8];
3466 SaveGraphicsState;
3467 SetLineWidth(linesep+axolinewidth);
3468 MoveTo(args[0],args[1]);
3469 Bezier(args[2],args[3],args[4],args[5],args[6],args[7]);
3470 Stroke;
3471 RestoreGraphicsState;
3472 SaveGraphicsState;
3473 SetLineWidth(linesep-axolinewidth);
3474 SetBackgroundColor(STROKING);
3475 MoveTo(args[0],args[1]);
3476 Bezier(args[2],args[3],args[4],args[5],args[6],args[7]);
3477 Stroke;
3478 RestoreGraphicsState;
3479 if ( witharrow ) BezierArrow(args);
3480 }
3481
3482 /*
3483 #] DashDoubleBezier :
3484 #] Drawing routines :
3485 #[ Wrapper routines :
3486 #[ AxoArc :
3487
3488 Draws arc centered at (#1,#2), radius #3, starting and ending
3489 angles #4, #5.
3490 Double, dashing, arrow, flip, clockwise
3491 */
3492
3493 void AxoArc(double *args)
3494 {
3495 double dashsize = args[6];
3496 linesep = args[5];
3497 GetArrow(args+7);
3498 if ( args[16] ) { /* If clockwise: reverse the angles and the arrow */
3499 double e;
3500 clockwise = 1; /* In principle not needed */
3501 flip = 1-flip;
3502 arrow.where = 1-arrow.where;
3503 e = args[3]; args[3] = args[4]; args[4] = e;
3504 }
3505
3506 if ( witharrow ) {
3507 if ( arrow.where > 1 ) arrow.where = 1;
3508 if ( arrow.where < 0 ) arrow.where = 0;
3509 if ( dashsize > 0 ) {
3510 if ( linesep > 0 ) {
3511 DashArrowDoubleArc(args);
3512 }
3513 else {
3514 args[5] = args[6];
3515 DashArrowArc(args);
3516 }
3517 }
3518 else {
3519 if ( linesep > 0 ) {
3520 ArrowDoubleArc(args);
3521 }
3522 else {
3523 ArrowArc(args);
3524 }
3525 }
3526 }
3527 else {
3528 if ( dashsize > 0 ) {
3529 if ( linesep > 0 ) {
3530 DashDoubleArc(args);
3531 }
3532 else {
3533 args[5] = args[6];
3534 DashCArc(args);
3535 }
3536 }
3537 else {
3538 if ( linesep > 0 ) {
3539 DoubleArc(args);
3540 }
3541 else {
3542 CArc(args);
3543 }
3544 }
3545 }
3546 }
3547
3548 /*
3549 #] AxoArc :
3550 #[ AxoBezier :
3551 */
3552
3553 void AxoBezier(double *args)
3554 {
3555 linesep = args[8];
3556 GetArrow(args+10);
3557 if ( witharrow ) {
3558 if ( arrow.where > 1 ) arrow.where = 1;
3559 if ( arrow.where < 0 ) arrow.where = 0;
3560 }
3561 if ( args[9] ) { /* dashes */
3562 if ( args[8] ) { /* double */
3563 DashDoubleBezier(args);
3564 }
3565 else {
3566 args[8] = args[9];
3567 DashBezier(args);
3568 }
3569 }
3570 else {
3571 if ( args[8] ) { /* double */
3572 DoubleBezier(args);
3573 }
3574 else {
3575 BezierCurve(args); /* The name Bezier was already taken */
3576 }
3577 }
3578 }
3579
3580 /*
3581 #] AxoBezier :
3582 #[ AxoGluon :
3583 */
3584
3585 void AxoGluon(double *args)
3586 {
3587 SetLineWidth(axolinewidth + args[6]);
3588 if ( args[7] ) { /* dashes */
3589 args[6] = args[7];
3590 DashGluon(args);
3591 }
3592 else {
3593 Gluon(args);
3594 }
3595 }
3596
3597 /*
3598 #] AxoGluon :
3599 #[ AxoGluonArc :
3600 */
3601
3602 void AxoGluonArc(double *args)
3603 {
3604 SetLineWidth(axolinewidth + args[7]);
3605 if ( args[9] ) { /* Clockwise */
3606 double a = args[3]; args[3] = args[4]; args[4] = a;
3607 }
3608 if ( args[8] ) { /* Dashes */
3609 args[7] = args[8];
3610 DashGluonArc(args);
3611 }
3612 else {
3613 GluonArc(args);
3614 }
3615 }
3616
3617 /*
3618 #] AxoGluonArc :
3619 #[ AxoGluonCirc :
3620 */
3621
3622 void AxoGluonCirc(double *args)
3623 {
3624 SetLineWidth(axolinewidth + args[6]);
3625 if ( args[7] ) { /* dashes */
3626 args[6] = args[7];
3627 DashGluonCirc(args);
3628 }
3629 else {
3630 GluonCirc(args);
3631 }
3632 }
3633
3634 /*
3635 #] AxoGluonCirc :
3636 #[ AxoLine :
3637
3638 AxoLine(x1,y1)(x2,y2){sep}{dashsize}{stroke width length inset}{where}
3639
3640 Generic switchyard to the various routines for compatibility
3641 with Jaxodraw and axodraw4j
3642
3643 Note: because the specific routines can be called either in the
3644 direct way or by means of the generic routine, they have to know
3645 what arrow to use. This is regulated by arrow.type. 0=old arrows.
3646 */
3647
3648 void AxoLine(double *args)
3649 {
3650 linesep = args[4];
3651 GetArrow(args+6);
3652 if ( witharrow ) {
3653 if ( arrow.where > 1 ) arrow.where = 1;
3654 if ( arrow.where < 0 ) arrow.where = 0;
3655 if ( args[5] == 0 ) {
3656 if ( linesep == 0 ) ArrowLine(args);
3657 else { ArrowDoubleLine(args); }
3658 }
3659 else {
3660 if ( linesep == 0 ) {
3661 args[4] = args[5];
3662 DashArrowLine(args);
3663 }
3664 else { DashArrowDoubleLine(args); }
3665 }
3666 }
3667 else {
3668 if ( args[5] == 0 ) { /* No dashing */
3669 if ( linesep == 0 ) Line(args);
3670 else DoubleLine(args);
3671 }
3672 else {
3673 if ( linesep == 0 ) {
3674 args[4] = args[5]; DashLine(args);
3675 }
3676 else DashDoubleLine(args);
3677 }
3678 }
3679 }
3680
3681 /*
3682 #] AxoLine :
3683 #[ AxoPhoton :
3684 */
3685
3686 void AxoPhoton(double *args)
3687 {
3688 if ( args[7] ) { /* dashes */
3689 if ( args[6] ) { /* double */
3690 DashDoublePhoton(args);
3691 }
3692 else {
3693 args[6] = args[7];
3694 DashPhoton(args);
3695 }
3696 }
3697 else {
3698 if ( args[6] ) { /* double */
3699 DoublePhoton(args);
3700 }
3701 else {
3702 Photon(args);
3703 }
3704 }
3705 }
3706
3707 /*
3708 #] AxoPhoton :
3709 #[ AxoPhotonArc :
3710 */
3711
3712 void AxoPhotonArc(double *args)
3713 {
3714 if ( args[9] ) { /* Clockwise */
3715 int num = 2*args[6]+0.5;
3716 double a = args[3]; args[3] = args[4]; args[4] = a;
3717 if ( ( num & 1 ) == 0 ) args[5] = -args[5];
3718 }
3719 if ( args[8] ) { /* dash */
3720 if ( args[7] ) { /* double */
3721 DashDoublePhotonArc(args);
3722 }
3723 else {
3724 args[7] = args[8];
3725 DashPhotonArc(args);
3726 }
3727 }
3728 else {
3729 if ( args[7] ) { /* double */
3730 DoublePhotonArc(args);
3731 }
3732 else {
3733 PhotonArc(args);
3734 }
3735 }
3736 }
3737
3738 /*
3739 #] AxoPhotonArc :
3740 #[ AxoZigZag :
3741 */
3742
3743 void AxoZigZag(double *args)
3744 {
3745 if ( args[7] ) { /* dashes */
3746 if ( args[6] ) { /* double */
3747 DashDoubleZigZag(args);
3748 }
3749 else {
3750 args[6] = args[7];
3751 DashZigZag(args);
3752 }
3753 }
3754 else {
3755 if ( args[6] ) { /* double */
3756 DoubleZigZag(args);
3757 }
3758 else {
3759 ZigZag(args);
3760 }
3761 }
3762 }
3763
3764 /*
3765 #] AxoZigZag :
3766 #[ AxoZigZagArc :
3767 */
3768
3769 void AxoZigZagArc(double *args)
3770 {
3771 if ( args[9] ) { /* Clockwise */
3772 int num = 2*args[6]+0.5;
3773 double a = args[3]; args[3] = args[4]; args[4] = a;
3774 if ( ( num & 1 ) == 0 ) args[5] = -args[5];
3775 }
3776 if ( args[8] ) { /* dash */
3777 if ( args[7] ) { /* double */
3778 DashDoubleZigZagArc(args);
3779 }
3780 else {
3781 args[7] = args[8];
3782 DashZigZagArc(args);
3783 }
3784 }
3785 else {
3786 if ( args[7] ) { /* double */
3787 DoubleZigZagArc(args);
3788 }
3789 else {
3790 ZigZagArc(args);
3791 }
3792 }
3793 }
3794
3795 /*
3796 #] AxoZigZagArc :
3797 #] Wrapper routines :
3798 #[ Various routines :
3799 #[ Rotate : +
3800
3801 Rotate: x y angle hmode vmode textwidth textheight
3802 Note, the textwidth/textheight have been scaled already;
3803 */
3804
3805 void Rotate(double *args)
3806 {
3807 double textheight = args[6]*args[4]/2/65536.;
3808 double textwidth = args[5]*args[3]/2/65536.;
3809 SetTransferMatrix(1,0,0,1,args[0],args[1]);
3810 SetTransferMatrix(COS(args[2]),SIN(args[2]),-SIN(args[2]),COS(args[2]),0,0);
3811 SetTransferMatrix(1,0,0,1,-textwidth,textheight);
3812 }
3813
3814 /*
3815 #] Rotate :
3816 #[ Grid :
3817
3818 Makes a coordinate grid in the indicated color.
3819 (x0,y0)(incx,incy)(nx,ny){color}{linewidth}
3820 */
3821
3822 void Grid(double *args)
3823 {
3824 int i, nx = args[4]+0.01, ny = args[5]+0.01;
3825 double maxx = args[2]*args[4];
3826 double maxy = args[3]*args[5];
3827 SetTransferMatrix(1,0,0,1,args[0],args[1]);
3828 for ( i = 0; i <= nx; i++ ) {
3829 MoveTo(i*args[2],0);
3830 LineTo(i*args[2],maxy);
3831 Stroke;
3832 }
3833 for ( i = 0; i <= ny; i++ ) {
3834 MoveTo(0,i*args[3]);
3835 LineTo(maxx,i*args[3]);
3836 Stroke;
3837 }
3838 }
3839
3840 /*
3841 #] Grid :
3842 #] Various routines :
3843 #] routines :
3844 */