Debian Codegraph @

texmf-dist/doc/latex/coordsys/putfile.maple

raw
1 ##
2 ## This is file `putfile.',
3 ## generated with the docstrip utility.
4 ##
5 ## The original source files were:
6 ##
7 ## coordsys.dtx (with options: `putfile')
8 ##
9 ##
10 ## This is the coordsys and logsys packages
11 ## Copyright 2000-06 by Mogens Lemvig Hansen,
12 ## [email protected]
13 ##
14 ## This program may be distributed and/or modified under the
15 ## conditions of the LaTeX Project Public License, either version 1.3
16 ## of this license or (at your option) any later version. The latest
17 ## version of this license is in
18 ## http://www.latex-project.org/lppl.txt
19 ## and version 1.2 or later is part of all distributions of LaTeX
20 ## version 2003/12/01 or later.
21 ##
22 ## This program consists of the files coordsys.dtx and coordsys.ins
23 ##
24 putfile := proc(fn::{string, list(string), set(string)},
25 P::specfunc(anything, PLOT) )
26 option `Copyright 2000-06 by Mogens Lemvig Hansen`;
27 description "version 1.1";
28 local L, fns, i, V, scl, FH, ttl, pt, path, ext;
29 global StringTools;
30 L := [args[3..-1]];
31 if hasoption(L, 'scale', scl, 'L') then
32 scl := evalf(scl);
33 if not type(scl, ['numeric', 'numeric']) then
34 error "the scale option must be [numeric, numeric] but got %1",
35 scl;
36 fi;
37 else
38 scl := [1,1];
39 fi;
40 if L <> [] then
41 error "unknown options: %1", L;
42 fi;
43 L := map( `putfile/grabcurves`, [op(P)] );
44 if type(fn, 'string') then
45 path := map(StringTools:-FirstFromRight, ["\\", "/", ":"], fn);
46 path := max(op(path));
47 ext := StringTools:-FirstFromRight(".", fn[path+1..-1]);
48 if ext = 0 then
49 ext := ".tex";
50 fns := fn;
51 else
52 ext := fn[path+ext..-1];
53 fns := fn[1..-length(ext)-1]
54 fi;
55 if nops(L) = 1 then
56 fns := [fns];
57 else
58 fns := [ seq(cat(fns, i), i=1..nops(L)) ];
59 fi;
60 fns := map(cat, fns, ext);
61 else
62 fns := fn;
63 if nops(fns) <> nops(L) then
64 error "the number of filenames must match "
65 "the number of curves, "
66 "received %1 names but %2 curves.",
67 nops(fns), nops(L);
68 fi;
69 fi;
70 V := select(type, [op(P)], 'specfunc'('anything', 'VIEW'));
71 if nops(V) <> 1 then
72 V := ['DEFAULT', 'DEFAULT'];
73 else
74 V := [ op([1,1], V), op([1,2], V) ];
75 fi;
76 V := subs('DEFAULT'=Float(-infinity)..Float(infinity), V);
77 ttl := select(type, [op(P)], 'specfunc'('anything', 'TITLE'));
78 if nops(ttl) = 1 then
79 ttl := op([1,1], ttl);
80 ttl := StringTools:-SubstituteAll(ttl, "\n", "\n%% ");
81 fi;
82 L := map( `putfile/cleancurve`, L, V);
83 L := map2( map, u->[scl[1]*u[1], scl[2]*u[2]], L );
84 for i from 1 to nops(L) do try
85 FH := fopen(fns[i], 'WRITE', 'TEXT');
86 if type(ttl, 'string') then fprintf(FH, "%%%% %s\n", ttl) fi;
87 if scl <> [1,1] then fprintf(FH, "%% scaled %a\n", scl) fi;
88 V := [ map2(op, 1, L[i]), map2(op, 2, L[i]) ];
89 V := map( u-> min(op(u))..max(op(u)), V );
90 fprintf(FH, "%% horizontal range: %f .. %f\n", op(V[1]));
91 fprintf(FH, "%% vertical range: %f .. %f\n", op(V[2]));
92 for pt in L[i] do
93 fprintf(FH, "%f \t%f\n", op(pt));
94 od;
95 finally fclose(FH);
96 end try end do;
97 end;
98 `putfile/grabcurves` := proc(x)
99 option `Copyright 2000-06 by Mogens Lemvig Hansen`;
100 description "version 1.1";
101 local L;
102 if op(0, x) = 'CURVES' then
103 L := [ op(x) ];
104 L := select(type, L, 'list');
105 RETURN(op(L));
106 else
107 RETURN(NULL);
108 fi;
109 end;
110 `putfile/cleancurve` := proc(CC::list, V::list)
111 local C, B, pts, pt, m, i;
112 option `Copyright 2004-06 by Mogens Lemvig Hansen`;
113 description "version 1.1";
114 C := select(type, CC, ['numeric', 'numeric']);
115 if nops(C) < 1 then error "no numeric points in %1", C fi;
116 B := C[1];
117 for i from 2 to nops(C) do
118 if C[i] = C[i-1] then next fi;
119 pts := NULL;
120 if ( C[i-1][1] < lhs(V[1]) and C[i][1] > lhs(V[1]) )
121 or ( C[i-1][1] > lhs(V[1]) and C[i][1] < lhs(V[1]) ) then
122 m := ( C[i-1][2] - C[i][2] ) / ( C[i-1][1] - C[i][1] );
123 pt := lhs(V[1]);
124 pt := [ pt, m*(pt-C[i][1]) + C[i][2] ];
125 pts := pts, pt;
126 fi;
127 if ( C[i-1][1] < rhs(V[1]) and C[i][1] > rhs(V[1]) )
128 or ( C[i-1][1] > rhs(V[1]) and C[i][1] < rhs(V[1]) ) then
129 m := ( C[i-1][2] - C[i][2] ) / ( C[i-1][1] - C[i][1] );
130 pt := rhs(V[1]);
131 pt := [ pt, m*(pt-C[i][1]) + C[i][2] ];
132 pts := pts, pt;
133 fi;
134 if ( C[i-1][2] < lhs(V[2]) and C[i][2] > lhs(V[2]) )
135 or ( C[i-1][2] > lhs(V[2]) and C[i][2] < lhs(V[2]) ) then
136 m := ( C[i-1][1] - C[i][1] ) / ( C[i-1][2] - C[i][2] );
137 pt := lhs(V[2]);
138 pt := [ m*(pt-C[i][2]) + C[i][1], pt ];
139 pts := pts, pt;
140 fi;
141 if ( C[i-1][2] < rhs(V[2]) and C[i][2] > rhs(V[2]) )
142 or ( C[i-1][2] > rhs(V[2]) and C[i][2] < rhs(V[2]) ) then
143 m := ( C[i-1][1] - C[i][1] ) / ( C[i-1][2] - C[i][2] );
144 pt := rhs(V[2]);
145 pt := [ m*(pt-C[i][2]) + C[i][1], pt ];
146 pts := pts, pt;
147 fi;
148 pts := [pts];
149 if C[i-1][1] < C[i][1] then
150 pts := sort(pts, (u,v)->evalb(u[1]<v[1]));
151 elif C[i-1][1] > C[i][1] then
152 pts := sort(pts, (u,v)->evalb(u[1]>v[1]));
153 elif C[i-1][2] < C[i][2] then
154 pts := sort(pts, (u,v)->evalb(u[2]<v[2]));
155 elif C[i-1][2] > C[i][2] then
156 pts := sort(pts, (u,v)->evalb(u[2]>v[2]));
157 else
158 error "cannot happen";
159 fi;
160 B := B, op(pts), C[i];
161 od;
162 C := [B];
163 C := select( u->evalb(lhs(V[1])<=u[1] and u[1] <= rhs(V[1])), C);
164 C := select( u->evalb(lhs(V[2])<=u[2] and u[2] <= rhs(V[2])), C);
165 C;
166 end;