| 1 |
.PS |
| 2 |
# Traintracks.m4 |
| 3 |
# https://tex.stackexchange.com/questions/729226/implementing-train-tracks-in-latex |
| 4 |
gen_init |
| 5 |
define(`robustcode_',1) |
| 6 |
|
| 7 |
divert(-1) |
| 8 |
######################################################################## |
| 9 |
`Default sizes' |
| 10 |
define(`sectionlen',linewid) |
| 11 |
define(`railgauge',(linewid/5)) |
| 12 |
define(`railthick',`1.5') |
| 13 |
define(`buttlen',`(railthick bp__ * 3)') |
| 14 |
define(`sleeperthick',`0.4') |
| 15 |
define(`buttthick',`(sleeperthick*2)') |
| 16 |
|
| 17 |
`straight(position,direction,keys) |
| 18 |
keys: length=expr; (default sectionlen) |
| 19 |
gauge=expr; (default railgauge) |
| 20 |
tiecount=expr; (number of sleepers) |
| 21 |
tie=attributes; |
| 22 |
rail=attributes;' |
| 23 |
define(`straight',`ifelse(`$1',,,move to `$1';) ifelse(`$2',,,`setdir_(`$2')') |
| 24 |
pushkeys_(`$3', |
| 25 |
`length:sectionlen:N; gauge:railgauge; rail::N; tiecount:3:; tie::N;')dnl |
| 26 |
for straighti=1 to m4tiecount do { |
| 27 |
{line from rvec_((straighti-1/2)*m4length/m4tiecount, m4gauge/2+buttlen/2) \ |
| 28 |
to rvec_((straighti-1/2)*m4length/m4tiecount,-m4gauge/2-buttlen/2) \ |
| 29 |
thick sleeperthick m4tie }} |
| 30 |
{strail(m4gauge/2)}; {strail(-m4gauge/2)}; move to rvec_(m4length,0)dnl |
| 31 |
popdef(`m4tie',`m4tiecount',`m4length',`m4gauge',`m4rail') ') |
| 32 |
define(`strail', |
| 33 |
`{line from rvec_(0,`$1') to rvec_(sectionlen,`$1') thick railthick m4rail |
| 34 |
line from rvec_(0,-buttlen/2) to rvec_(0,buttlen/2) thick buttthick} |
| 35 |
{line from rvec_(0,`$1'-buttlen/2) to rvec_(0,`$1'+buttlen/2) \ |
| 36 |
thick buttthick m4rail} ') |
| 37 |
|
| 38 |
`curve(position,direction,L|R,keys) |
| 39 |
keys: radius=expr; (default sectionlen*2) |
| 40 |
gauge=expr; (default railgauge) |
| 41 |
tiecount=expr; (number of sleepers) |
| 42 |
tie=attributes; |
| 43 |
rail=attributes; |
| 44 |
Calls to robust rvec_r instead of rvec_ are for |
| 45 |
use in pic loops' |
| 46 |
define(`curve',`ifelse(`$1',,,move to `$1';) ifelse(`$2',,,`setdir_(`$2')') |
| 47 |
pushkeys_(`$4', |
| 48 |
`radius:sectionlen*2; gauge:railgauge; rail::N; tiecount:3:; tie::N;')dnl |
| 49 |
pushdef(`m4pm',`ifelse(`$3',,+,`$3',L,+,-)')dnl |
| 50 |
M4C: rvec_r(0,m4pm m4radius) # robust rvec |
| 51 |
m4rs = rp_ang*rtod_-(m4pm 90); m4rf = m4rs m4pm 30 |
| 52 |
for curvei=1 to m4tiecount do {{ line \ |
| 53 |
from M4C+(Rect_(m4radius+m4gauge/2+buttlen/2,\ |
| 54 |
m4rs+(curvei-1/2)*(m4rf-m4rs)/m4tiecount))\ |
| 55 |
to M4C+(Rect_(m4radius-m4gauge/2-buttlen/2,\ |
| 56 |
m4rs+(curvei-1/2)*(m4rf-m4rs)/m4tiecount)) thick sleeperthick m4tie }} |
| 57 |
move to M4C+(Rect_(m4radius,m4rs)) |
| 58 |
{crail(M4C,m4radius-(m4pm m4gauge/2), m4gauge/2,m4pm)} |
| 59 |
{crail(M4C,m4radius+(m4pm m4gauge/2),-m4gauge/2,m4pm)} |
| 60 |
move to M4C+(Rect_(m4radius,m4rf)); setdir_(rp_ang*rtod_ m4pm 30) |
| 61 |
popdef(`m4tie',`m4tiecount',`m4rail',`m4gauge',`m4radius',`m4pm') ') |
| 62 |
|
| 63 |
define(`crail', #`crail(center,rad,offset,+|-)' |
| 64 |
`move to rvec_r(0,`$3'); crbuttf = buttlen/2/(`$2') |
| 65 |
{arc ifelse(`$4',-,cw) to rvec_r((`$2')/2,`$4'((1-sqrt(3)/2)*(`$2'))) \ |
| 66 |
with .c at `$1' thick railthick m4rail |
| 67 |
line from (-crbuttf between Here and `$1') \ |
| 68 |
to ( crbuttf between Here and `$1') thick buttthick} |
| 69 |
{line from (-crbuttf between Here and `$1') \ |
| 70 |
to ( crbuttf between Here and `$1') thick buttthick} ') |
| 71 |
|
| 72 |
######################################################################## |
| 73 |
divert(0)dnl |
| 74 |
|
| 75 |
[ straight |
| 76 |
{curve; curve(,,R); straight; curve(,,R); curve; straight} |
| 77 |
for i=1 to 5 do { straight } ] |
| 78 |
|
| 79 |
[ straight |
| 80 |
for i=1 to 6 do { curve(,,R) } |
| 81 |
straight |
| 82 |
{T: Here |
| 83 |
curve(,,R,rail=outlined "red"); curve(,,,rail=outlined "red") |
| 84 |
straight |
| 85 |
crad = sectionlen*2-(Here.y-T.y) |
| 86 |
for i=1 to 6 do { curve(,,R,radius=crad) } |
| 87 |
straight |
| 88 |
define rgbpurp { rgbstring(0.5,0,1) } |
| 89 |
{ curve(,,, rail=outlined rgbpurp); curve(,,R, rail=outlined rgbpurp) |
| 90 |
for i=1 to 5 do { straight(,180) } } |
| 91 |
for i=1 to 3 do { straight(,0) } |
| 92 |
for i=1 to 6 do { curve(,,R,radius=crad) } |
| 93 |
for i=1 to 3 do { straight } } |
| 94 |
for i=1 to 5 do { straight } ] with .nw at last [].sw+(0,-0.2) |
| 95 |
|
| 96 |
[ for dir=90 to 570 by 240 do { |
| 97 |
curve(,dir,,tiecount=5;tie=outlined "gray" thick 2.5) |
| 98 |
curve(, ,,tiecount=5;tie=outlined "gray" thick 2.5) } |
| 99 |
] with .nw at 1st [].ne+(0.2,0) |
| 100 |
|
| 101 |
.PE |