summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/wargame/source/hex/coord.dtx
blob: b996ffa3c2814bbda1f34e2cebe5751479b4e5c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
% \iffalse
% --------------------------------------------------------------------
%<*hex>
% \fi
% 
% \subsubsection{Hex coordinate system}
%
% \begin{Macro}{\hex@xx,\hex@yy}
% 
%   Some offsets along $x$ and $y$ due to offset of every second hex
%   column.
%
%   \begin{align*}
%       \delta_x &= \cos60^{\circ}\\
%       \delta_y &= \sin60^{\circ}\\
%   \end{align*}
%
%   These numbers are calculated once here and then used several times
%   in the following code.
%
%    \begin{macrocode}
\pgfmathparse{cos(60)}        \xdef\hex@xx{\pgfmathresult}
\pgfmathparse{sin(60)}        \xdef\hex@yy{\pgfmathresult}
\pgfmathparse{\hex@yy*cos(30)}\xdef\hex@e@xx{\pgfmathresult}
\pgfmathparse{\hex@yy*sin(30)}\xdef\hex@e@yy{\pgfmathresult}
\newdimen\hex@radius\hex@radius=1cm
\newdimen\hex@dx    \expandafter\hex@dx=\hex@xx cm
\newdimen\hex@dy    \expandafter\hex@dy=\hex@yy cm
\newdimen\hex@e@dx  \expandafter\hex@e@dx=\hex@e@xx cm
\newdimen\hex@e@dy  \expandafter\hex@e@dy=\hex@e@yy cm

%    \end{macrocode}
% \end{Macro}
%
% Some code we need for some options
%
%    \begin{macrocode}
\newif\ifhex@label@is@name\hex@label@is@namefalse
\def\hex@short@col{isfalse}
\def\hex@got@short{isfalse}
\pgfmathdeclarefunction{isfalse}{1}{%
  \begingroup
  \def\pgfmathresult{0}%
  \pgfmath@smuggleone\pgfmathresult
  \endgroup}
\pgfmathdeclarefunction{istrue}{1}{%
  \begingroup
  \def\pgfmathresult{1}%
  \pgfmath@smuggleone\pgfmathresult
  \endgroup}
%    \end{macrocode}
%
% What follows is a way to configure the hex coordinate system.  For
% example, if the rows goes down, then we can flag that, but still add
% hexes straightforwardly.  Similar for columns.  We can also specify
% that the first row or column has number 1 (instead of 0).  Since
% this is dealt with a the coordinate level, it means most of the rest
% of the code is agnostic to these choices. 
% 
% Which is the first coordinate (0 or 1)
%
%    \begin{macrocode}
\tikzset{
  hex/first row is/.is choice,
  hex/first row is/0/.code={\def\hex@coords@row@off{0}},
  hex/first row is/1/.code={\def\hex@coords@row@off{-1}},
  hex/first row is=0,
  hex/first column is/.is choice,
  hex/first column is/0/.code={\def\hex@coords@col@off{0}},
  hex/first column is/1/.code={\def\hex@coords@col@off{-1}},
  hex/first column is=0,
  hex/first row and column are/.is choice,
  hex/first row and column are/0/.style={
    hex/first row is=0,%
    hex/first column is=0},
  hex/first row and column are/1/.style={
    hex/first row is=1,%
    hex/first column is=1},
%    \end{macrocode}
% 
% Which way does the column and row numbers go
%
%    \begin{macrocode}
  hex/row direction is/.is choice,
  hex/row direction is/normal/.code={\def\hex@coords@row@fac{1}},
  hex/row direction is/reversed/.code={\def\hex@coords@row@fac{-1}},
  hex/row direction is/up/.style={hex/row direction is=normal},
  hex/row direction is/down/.style={hex/row direction is=reversed},
  hex/row direction is/positive/.style={hex/row direction is=normal},
  hex/row direction is/negative/.style={hex/row direction is=reversed},
  hex/row direction is=normal,
  hex/column direction is/.is choice,
  hex/column direction is/normal/.code={\def\hex@coords@col@fac{1}},
  hex/column direction is/reversed/.code={\def\hex@coords@col@fac{-1}},
  hex/column direction is/right/.style={hex/column direction is=normal},
  hex/column direction is/left/.style={hex/column direction is=reversed},
  hex/column direction is/positive/.style={hex/column direction is=normal},
  hex/column direction is/negative/.style={hex/column direction is=reversed},
  hex/column direction is=normal,
%    \end{macrocode}
% 
% Make labels names of shapes of the hexes so we can use labels to
% place stuff
%
%    \begin{macrocode}
  hex/label is name/.is if=hex@label@is@name,
%    \end{macrocode}
% 
% If we have uneven number of rows in some columns. 
%
%    \begin{macrocode}
  hex/short bottom columns/.is choice,
  hex/short bottom columns/odd/.code={%
    \def\hex@bot@short@col{isodd}
    \def\hex@got@bot@short{istrue}
    \hex@dbg{4}{Short columns (odd): \meaning\hex@bot@short@col}},
  hex/short bottom columns/even/.code={
    \def\hex@bot@short@col{iseven}
    \def\hex@got@bot@short{istrue}
    \hex@dbg{4}{Short column (even): \meaning\hex@bot@short@col}},
  hex/short bottom columns/none/.code={
    \def\hex@bot@short@col{isfalse}
    \def\hex@got@bot@short{isfalse}
    \hex@dbg{4}{Short columns (none): \meaning\hex@bot@short@col}},
  hex/short bottom columns=none,
  hex/short columns/.forward to=hex/short bottom columns,
  hex/short top columns/.is choice,
  hex/short top columns/odd/.code={%
    \def\hex@top@short@col{isodd}
    \def\hex@got@top@short{istrue}
    \hex@dbg{4}{Short columns (odd): \meaning\hex@top@short@col}},
  hex/short top columns/even/.code={
    \def\hex@top@short@col{iseven}
    \def\hex@got@top@short{istrue}
    \hex@dbg{4}{Short column (even): \meaning\hex@top@short@col}},
  hex/short top columns/none/.code={
    \def\hex@top@short@col{isfalse}
    \def\hex@got@top@short{isfalse}
    \hex@dbg{4}{Short columns (none): \meaning\hex@top@short@col}},
  hex/short top columns=none,
}
\message{^^JInitial hex coordinate setup:
  Rows: factor=\hex@coords@row@fac, offset=\hex@coords@row@off
  Columns: factor=\hex@coords@col@fac, offset=\hex@coords@col@off}
%    \end{macrocode}
% 
% \begin{HexKey*}{
%   hex/coords/column,
%   hex/coords/row,
%   hex/coords/vertex,
%   hex/coords/edge,
%   hex/coords/offset}
%
%   We define the keys for hexagon coordinates.  These are the
%   \spec{row}, \spec{column}, possible \spec{vertex} or
%   \spec{edge}. Vertexes and edges are defined as
%   multiple-choice. \spec{offset} specifies the offset from the
%   centre in the direction of a vertex or edge.  By default, the
%   offset is one, meaning all the way to the vertex or edge.
%
%   The key \spec{inverse row} specifies that the rows are given from
%   the top down, but coordinates should be calculated as if the row
%   was negative.  This (should) allow us to design boards where rows
%   increase downward, while still keeping the interface and remaining
%   code somewhat reasonable and agnostic.
%
%   Similarly, the key \spec{column 1}, will allow us to start the
%   columns with 1.
%
% 
%
%    \begin{macrocode}
\tikzset{
  /hex/coords/.cd,
  column/.store in=\hex@col,
  c/.store in=\hex@col,
  row/.store in=\hex@row,
  r/.store in=\hex@row,
  offset/.store in=\hex@off,
  o/.store in=\hex@off,
  vertex/.is choice,
  vertex/none/.code={\global\let\hex@vtx\@empty},
  vertex/east/.code={\def\hex@vtx{0}},
  vertex/north east/.code={\def\hex@vtx{60}},
  vertex/north west/.code={\def\hex@vtx{120}},
  vertex/west/.code={\def\hex@vtx{180}},
  vertex/south west/.code={\def\hex@vtx{240}},
  vertex/south east/.code={\def\hex@vtx{300}},
  vertex/E/.code={\def\hex@vtx{0}},
  vertex/NE/.code={\def\hex@vtx{60}},
  vertex/NW/.code={\def\hex@vtx{120}},
  vertex/W/.code={\def\hex@vtx{180}},
  vertex/SW/.code={\def\hex@vtx{240}},
  vertex/SE/.code={\def\hex@vtx{300}},
  vertex/.default=none,
  v/.forward to=/hex/coords/vertex=#1,
  edge/.is choice,
  edge/none/.code={\global\let\hex@edg\@empty},
  edge/north east/.code={\def\hex@edg{30}},
  edge/north/.code={\def\hex@edg{90}},
  edge/north west/.code={\def\hex@edg{150}},
  edge/south west/.code={\def\hex@edg{210}},
  edge/south/.code={\def\hex@edg{270}},
  edge/south east/.code={\def\hex@edg{330}},
  edge/NE/.code={\def\hex@edg{30}},
  edge/N/.code={\def\hex@edg{90}},
  edge/NW/.code={\def\hex@edg{150}},
  edge/SW/.code={\def\hex@edg{210}},
  edge/S/.code={\def\hex@edg{270}},
  edge/SE/.code={\def\hex@edg{330}},
  edge/.default=none,
  e/.forward to=/hex/coords/edge,
}
%    \end{macrocode}
% \end{HexKey*}
%
% \begin{Macro}{\hex@coords@reset}
%
%   This macro resets the hex coordinates to default values.  That is
%   row and column 0, no vertex or edge.
%   
%    \begin{macrocode}
\def\hex@coords@reset{%
  \tikzset{%
    /hex/coords/.cd,
    column=0,
    row=0,
    edge=none,
    vertex=none,
    offset=1}}
%    \end{macrocode}
% \end{Macro}
% 
% The following calculates the Cartesian coordinates from Hex
% coordinates
%
% \begin{Syntax}
%   (cs:hex column=\meta{C},row=\meta{R},vertex=\meta{V},edge=\meta{E})
% \end{Syntax}
%
% Given the hexagon column $C$ and row $R$ with hexagon radius $r$,
% the centre of the hexagon is at 
%
% \begin{eqnarray*}
%     x &=& 2C\frac{3}{4}r\\
%     y &=& r(R - (C \% 2)\sin60^{\circ})
%   \end{eqnarray*}
%
% If \meta{V} or \meta{E} are given, then these are added to the
% centre point.
%
% Note, $C$ and $R$ may be fractional numbers, which will specify a
% point inside a hex.
%
% We set-up the translation to Cartesian coordinates. First
% thing is to reset keys in \spec{/hex/coords}, and then parse out the
% keys given.
% 
%    \begin{macrocode}
\def\hex@coords@conv#1{%
  \hex@coords@reset%
  \tikzset{/hex/coords/.cd, #1}%
%    \end{macrocode}
%
% Then we calculate the $x$ coordinate and set the dimension
% \cs{pgf@x}. We do this by
%
% $$x=c_e \frac{3}{2}\quad,$$
%
% where
%
% $$c_e = f_c  (c+o_c)\quad,$$ 
%
% is the effective column (stored in \cs{hex@eff@col}) calculated from
% is the direction factor $f_c$ (set by \spec{hex/column direction
% is}) and the offset $o_c$ (set by \spec{hex/first column is}).
% 
%    \begin{macrocode}
  \pgfmathparse{int(\hex@coords@col@fac*(\hex@col+\hex@coords@col@off))}%
  \xdef\hex@eff@col{\pgfmathresult}%
  \hex@dbg{2}{Effective column: \hex@coords@col@fac * (\hex@col -
    \hex@coords@col@off) -> \hex@eff@col}%
  \pgfmathparse{\hex@eff@col*1.5}%
  \xdef\hex@x{\pgfmathresult}%
  \expandafter\pgf@x=\hex@x cm%
%    \end{macrocode}
%
% And then for the $y$ coordinate and set the dimension \cs{pgf@y}.
% 
% $$y  = 2\left(r_e - c_e \operatorname{mod} 2\right) \cos60^{\circ}\quad,$$
%
% where
% 
% $$r_e= 2 f_r (r+o_r) - (c+o_c) \operatorname{mod} 2\quad,$$
%
% is the effective row (stored as \cs{hex@eff@row}) calculated from
% the the direction factor $f_r$ (set by \spec{hex/row
% direction is}) and the offset $o_r$ (set by \spec{hex/first
% row is}). 
% 
%    \begin{macrocode}
  \pgfmathparse{int(\hex@coords@row@fac*(\hex@row+\hex@coords@row@off))}%
  \xdef\hex@eff@row{\pgfmathresult}%
  \hex@dbg{2}{Effective row: \hex@coords@row@fac * (\hex@row +
    \hex@coords@row@off) -> \hex@eff@row}%
  \pgfmathparse{(2*\hex@eff@row-mod(round((\hex@col+\hex@coords@col@off)),2))*\hex@yy}%
  \xdef\hex@y{\pgfmathresult}%
  \expandafter\pgf@x=\hex@y cm%
%    \end{macrocode}
%
% If we have a vertex specification add that location to the current
% coordinates.  If not, set the point. 
% 
%    \begin{macrocode}
  \ifx\hex@vtx\@empty\pgfpointxy{\hex@x}{\hex@y}\else%
  \pgfpointadd{\pgfpointxy{\hex@x}{\hex@y}}{%
    \pgfpointscale{\hex@off}{\pgfpointpolarxy{\hex@vtx}{1}}}\fi%
%    \end{macrocode}
%
% If we have an edge specification add that location to the current
% coordinates.  
% 
%    \begin{macrocode}
  \ifx\hex@edg\@empty\else%
  \pgfpointadd{\pgfpointxy{\hex@x}{\hex@y}}{%
    \pgfpointscale{\hex@off}{\pgfpointpolarxy{\hex@edg}{\hex@yy}}}\fi
%    \end{macrocode}
%
% For debugging, we can print out stuff. 
% 
%    \begin{macrocode}
  \hex@dbg{2}{Hex coordinates: #1
    ^^J c=\hex@col
    ^^J r=\hex@row
    ^^J v=\hex@vtx
    ^^J e=\hex@edg
    ^^J x=\hex@x
    ^^J y=\hex@y}%
  \global\let\hex@x\hex@x%
  \global\let\hex@y\hex@y%
  \global\let\hex@row\hex@row%
  \global\let\hex@col\hex@col%
}
\tikzdeclarecoordinatesystem{hex}{%
  \hex@coords@conv{#1}}
%    \end{macrocode}
% \iffalse
%</hex>
% --------------------------------------------------------------------
% \fi