| 1 |
% AXES: Axis/grid-tick formatter, for the METAPLOT package. |
| 2 |
% Copyright(C) 2004, Brooks Moses |
| 3 |
% |
| 4 |
% This work may be distributed and/or modified under the |
| 5 |
% conditions of the LaTeX Project Public License, either |
| 6 |
% version 1.3 of this license or (at your option) any |
| 7 |
% later version. |
| 8 |
% The latest version of the license is in |
| 9 |
% http://www.latex-project.org/lppl.txt |
| 10 |
% and version 1.3 or later is part of all distributions of |
| 11 |
% LaTeX version 2003/06/01 or later. |
| 12 |
% |
| 13 |
% This work has the LPPL maintenance status "author-maintained". |
| 14 |
% |
| 15 |
% Version 0.9. |
| 16 |
% |
| 17 |
% The n < 1.0 release number indicates that this is a beta release; |
| 18 |
% the author at present makes no assurances that the command syntax |
| 19 |
% will remain unchanged between this and the final Version 1.0 |
| 20 |
% release. |
| 21 |
% |
| 22 |
% Bug reports, feature requests, and all other suggestions should |
| 23 |
% be directed to Brooks Moses, [email protected]. |
| 24 |
|
| 25 |
|
| 26 |
input format |
| 27 |
|
| 28 |
% axes_tick(pair tickorigin, |
| 29 |
% numeric ticklength, |
| 30 |
% pair tickdir) |
| 31 |
% |
| 32 |
% Draws a grid tick, located on tickorigin, in the direction indicated by |
| 33 |
% tickdir, with length ticklength. |
| 34 |
|
| 35 |
def axes_tick(expr tickorigin, ticklength, tickdir) = |
| 36 |
begingroup |
| 37 |
interim linecap := butt; |
| 38 |
save theaxistick; picture theaxistick; theaxistick := nullpicture; |
| 39 |
addto theaxistick doublepath ((0,0)--(ticklength * unitvector(tickdir))); |
| 40 |
|
| 41 |
theaxistick shifted tickorigin |
| 42 |
endgroup |
| 43 |
enddef; |
| 44 |
|
| 45 |
|
| 46 |
% axes_ticklabeled(pair tickorigin, |
| 47 |
% numeric ticklength, |
| 48 |
% numeric tickspace, |
| 49 |
% pair tickdir, |
| 50 |
% picture ticklabel) |
| 51 |
% |
| 52 |
% Draws a grid tick, located on tickorigin, in the direction indicated by |
| 53 |
% tickdir, with length ticklength. In addition, ticklabel is placed as |
| 54 |
% a label such that its center is along the line of the tick, and the |
| 55 |
% bounding box is a distance tickspace from the end of the tick. |
| 56 |
|
| 57 |
def axes_ticklabeled(expr tickorigin, ticklength, tickspace, tickdir, ticklabel) = |
| 58 |
begingroup |
| 59 |
interim linecap := butt; |
| 60 |
save tickunitvector; pair tickunitvector; |
| 61 |
tickunitvector := unitvector(tickdir); |
| 62 |
|
| 63 |
% We calculate an intersection point between the bbox of ticklabel |
| 64 |
% and a ray extending from center(ticklabel) in the direction of |
| 65 |
% tickdir (approximated by a line guaranteed to be long enough to |
| 66 |
% exit the bbox), in the coordinates of ticklabel. Thus, shifting |
| 67 |
% ticklabel by the negative of this amount will put this intersection |
| 68 |
% point at (0,0). |
| 69 |
|
| 70 |
save thelabelshift; pair thelabelshift; |
| 71 |
thelabelshift = |
| 72 |
(center(ticklabel) -- (center(ticklabel) |
| 73 |
- tickunitvector * (xpart(urcorner ticklabel - llcorner ticklabel) |
| 74 |
+ ypart(urcorner ticklabel - llcorner ticklabel)))) |
| 75 |
intersectionpoint (llcorner ticklabel -- lrcorner ticklabel |
| 76 |
-- urcorner ticklabel -- ulcorner ticklabel -- cycle); |
| 77 |
|
| 78 |
save thetick; picture thetick; thetick := nullpicture; |
| 79 |
addto thetick doublepath ((0,0)--(ticklength * tickunitvector)); |
| 80 |
addto thetick also ticklabel |
| 81 |
shifted ((ticklength + tickspace)*tickunitvector - thelabelshift); |
| 82 |
|
| 83 |
thetick shifted tickorigin |
| 84 |
endgroup |
| 85 |
enddef; |
| 86 |
|
| 87 |
|
| 88 |
% axes_ticknumbered(pair tickorigin, |
| 89 |
% numeric ticklength, |
| 90 |
% numeric tickspace, |
| 91 |
% pair tickdir, |
| 92 |
% numeric tickvalue) |
| 93 |
% string ticklabelformat) |
| 94 |
% |
| 95 |
% Draws a grid tick with a formatted number (tickvalue) as the label, |
| 96 |
% according to the format given in ticklabelformat, with the remainder |
| 97 |
% of the syntax identical to axes_ticklabeled. |
| 98 |
|
| 99 |
def axes_ticknumbered(expr tickorigin, ticklength, tickspace, tickdir, |
| 100 |
tickvalue, ticklabelformat) = |
| 101 |
axes_ticklabeled(tickorigin, ticklength, tickspace, tickdir, |
| 102 |
format(ticklabelformat, tickvalue)) |
| 103 |
enddef; |
| 104 |
|
| 105 |
|
| 106 |
% axes_tickrow(pair startpoint, |
| 107 |
% pair endpoint, |
| 108 |
% numeric ticklength, |
| 109 |
% pair tickdir, |
| 110 |
% numeric ntickspaces) |
| 111 |
% |
| 112 |
% Draws a row of ntickspaces+1 grid ticks (that is, ticks with ntickspaces |
| 113 |
% spaces between them) from startpoint to endpoint, with parameters given |
| 114 |
% by ticklength and tickdir. |
| 115 |
|
| 116 |
def axes_tickrow(expr startpoint, endpoint, |
| 117 |
ticklength, tickdir, ntickspaces) = |
| 118 |
begingroup |
| 119 |
save thetickrow; picture thetickrow; thetickrow := nullpicture; |
| 120 |
save tickpoint; pair tickpoint; |
| 121 |
for itick=0 upto ntickspaces: |
| 122 |
tickpoint := (itick/ntickspaces) * (endpoint - startpoint) + startpoint; |
| 123 |
addto thetickrow also |
| 124 |
axes_tick(tickpoint, ticklength, tickdir); |
| 125 |
endfor |
| 126 |
thetickrow |
| 127 |
endgroup |
| 128 |
enddef; |
| 129 |
|
| 130 |
|
| 131 |
% axes_tickrow_numbered(pair startpoint, |
| 132 |
% pair endpoint, |
| 133 |
% numeric ticklength, |
| 134 |
% numeric tickspace, |
| 135 |
% pair tickdir, |
| 136 |
% numeric startvalue, |
| 137 |
% numeric endvalue, |
| 138 |
% numeric ntickspaces, |
| 139 |
% string ticklabelformat) |
| 140 |
% |
| 141 |
% Draws a row of ntickspaces+1 labeled grid ticks (that is, ticks with |
| 142 |
% ntickspaces spaces between them) from startpoint to endpoint, with |
| 143 |
% parameters given by ticklength, tickspace, and tickdir. These ticks |
| 144 |
% are labeled with numeric values that range from startvalue (at' |
| 145 |
% startpoint) to endvalue (at endpoint), according to the format given |
| 146 |
% in ticklabelformat. |
| 147 |
|
| 148 |
def axes_tickrow_numbered(expr startpoint, endpoint, |
| 149 |
ticklength, tickspace, tickdir, |
| 150 |
startvalue, endvalue, ntickspaces, |
| 151 |
ticklabelformat) = |
| 152 |
begingroup |
| 153 |
save thetickrow; picture thetickrow; thetickrow := nullpicture; |
| 154 |
save tickpoint; pair tickpoint; |
| 155 |
save tickvalue; numeric tickvalue; |
| 156 |
for itick=0 upto ntickspaces: |
| 157 |
tickpoint := (itick/ntickspaces) * (endpoint - startpoint) + startpoint; |
| 158 |
tickvalue := (itick/ntickspaces) * (endvalue - startvalue) + startvalue; |
| 159 |
addto thetickrow also |
| 160 |
axes_ticknumbered(tickpoint, ticklength, tickspace, tickdir, |
| 161 |
tickvalue, ticklabelformat); |
| 162 |
endfor |
| 163 |
thetickrow |
| 164 |
endgroup |
| 165 |
enddef; |
| 166 |
|
| 167 |
|
| 168 |
% axes_tickscale(pair startpoint, |
| 169 |
% pair endpoint, |
| 170 |
% numeric ticklength, |
| 171 |
% numeric tickspace, |
| 172 |
% pair tickdir, |
| 173 |
% numeric startvalue, |
| 174 |
% numeric endvalue, |
| 175 |
% numeric tickzero, |
| 176 |
% numeric tickstep, |
| 177 |
% string ticklabelformat) |
| 178 |
% |
| 179 |
% Draws a row of grid ticks from startpoint to endpoint, arranged |
| 180 |
% such that their values increase in steps of size tickstep, and |
| 181 |
% located so that the sequence of grid ticks, if continued to |
| 182 |
% infinity, will include a tick with value tickzero. The ticks |
| 183 |
% will be labeled according to format ticklabelformat, or if |
| 184 |
% ticklabelformat is "", they will be unlabeled. |
| 185 |
|
| 186 |
def axes_tickscale(expr startpoint, endpoint, |
| 187 |
ticklength, tickspace, tickdir, |
| 188 |
startvalue, endvalue, tickzero, tickstep, |
| 189 |
ticklabelformat) = |
| 190 |
begingroup |
| 191 |
% tickepsilon is a small tolerance value to avoid roundoff errors. |
| 192 |
save tickepsilon; numeric tickepsilon; |
| 193 |
tickepsilon = 0.005 * min(1.0, abs(endvalue-startvalue)/tickstep); |
| 194 |
|
| 195 |
save firstticknumber; numeric firstticknumber; |
| 196 |
save lastticknumber; numeric lastticknumber; |
| 197 |
firstticknumber = ceiling((min(startvalue,endvalue)-tickzero)/tickstep - tickepsilon); |
| 198 |
lastticknumber = floor((max(startvalue,endvalue)-tickzero)/tickstep + tickepsilon); |
| 199 |
|
| 200 |
save firsttickvalue; numeric firsttickvalue; |
| 201 |
save lasttickvalue; numeric lasttickvalue; |
| 202 |
firsttickvalue = firstticknumber * tickstep + tickzero; |
| 203 |
lasttickvalue = lastticknumber * tickstep + tickzero; |
| 204 |
|
| 205 |
if ticklabelformat="": |
| 206 |
axes_tickrow( |
| 207 |
((firsttickvalue-startvalue)/(endvalue-startvalue))[startpoint, endpoint], |
| 208 |
((lasttickvalue-startvalue)/(endvalue-startvalue))[startpoint, endpoint], |
| 209 |
ticklength, tickdir, lastticknumber - firstticknumber) |
| 210 |
else: |
| 211 |
axes_tickrow_numbered( |
| 212 |
((firsttickvalue-startvalue)/(endvalue-startvalue))[startpoint, endpoint], |
| 213 |
((lasttickvalue-startvalue)/(endvalue-startvalue))[startpoint, endpoint], |
| 214 |
ticklength, tickspace, tickdir, |
| 215 |
firsttickvalue, lasttickvalue, lastticknumber - firstticknumber, |
| 216 |
ticklabelformat) |
| 217 |
fi |
| 218 |
endgroup |
| 219 |
enddef; |