Debian Codegraph @

texmf-dist/source/latex/wargame/hex/coord.dtx

raw
1 % \iffalse
2 % --------------------------------------------------------------------
3 %<*hex>
4 % \fi
5 %
6 % \subsubsection{Hex coordinate system}
7 %
8 % \begin{Macro}{\hex@xx,\hex@yy}
9 %
10 % Some offsets along $x$ and $y$ due to offset of every second hex
11 % column.
12 %
13 % \begin{align*}
14 % \delta_x &= \cos60^{\circ}\\
15 % \delta_y &= \sin60^{\circ}\\
16 % \end{align*}
17 %
18 % These numbers are calculated once here and then used several times
19 % in the following code.
20 %
21 % \begin{macrocode}
22 \pgfmathparse{cos(60)} \xdef\hex@xx{\pgfmathresult}
23 \pgfmathparse{sin(60)} \xdef\hex@yy{\pgfmathresult}
24 \pgfmathparse{\hex@yy*cos(30)}\xdef\hex@e@xx{\pgfmathresult}
25 \pgfmathparse{\hex@yy*sin(30)}\xdef\hex@e@yy{\pgfmathresult}
26 \newdimen\hex@radius\hex@radius=1cm
27 \newdimen\hex@dx \expandafter\hex@dx=\hex@xx cm
28 \newdimen\hex@dy \expandafter\hex@dy=\hex@yy cm
29 \newdimen\hex@e@dx \expandafter\hex@e@dx=\hex@e@xx cm
30 \newdimen\hex@e@dy \expandafter\hex@e@dy=\hex@e@yy cm
31
32 % \end{macrocode}
33 % \end{Macro}
34 %
35 % Some code we need for some options
36 %
37 % \begin{macrocode}
38 \newif\ifhex@label@is@name\hex@label@is@namefalse
39 \def\hex@short@col{isfalse}
40 \def\hex@got@short{isfalse}
41 \pgfmathdeclarefunction{isfalse}{1}{%
42 \begingroup
43 \def\pgfmathresult{0}%
44 \pgfmath@smuggleone\pgfmathresult
45 \endgroup}
46 \pgfmathdeclarefunction{istrue}{1}{%
47 \begingroup
48 \def\pgfmathresult{1}%
49 \pgfmath@smuggleone\pgfmathresult
50 \endgroup}
51 % \end{macrocode}
52 %
53 % What follows is a way to configure the hex coordinate system. For
54 % example, if the rows goes down, then we can flag that, but still add
55 % hexes straightforwardly. Similar for columns. We can also specify
56 % that the first row or column has number 1 (instead of 0). Since
57 % this is dealt with a the coordinate level, it means most of the rest
58 % of the code is agnostic to these choices.
59 %
60 % Which is the first coordinate (0 or 1)
61 %
62 % \begin{macrocode}
63 \tikzset{
64 hex/first row is/.is choice,
65 hex/first row is/0/.code={\def\hex@coords@row@off{0}},
66 hex/first row is/1/.code={\def\hex@coords@row@off{-1}},
67 hex/first row is=0,
68 hex/first column is/.is choice,
69 hex/first column is/0/.code={\def\hex@coords@col@off{0}},
70 hex/first column is/1/.code={\def\hex@coords@col@off{-1}},
71 hex/first column is=0,
72 hex/first row and column are/.is choice,
73 hex/first row and column are/0/.style={
74 hex/first row is=0,%
75 hex/first column is=0},
76 hex/first row and column are/1/.style={
77 hex/first row is=1,%
78 hex/first column is=1},
79 % \end{macrocode}
80 %
81 % Which way does the column and row numbers go
82 %
83 % \begin{macrocode}
84 hex/row direction is/.is choice,
85 hex/row direction is/normal/.code={\def\hex@coords@row@fac{1}},
86 hex/row direction is/reversed/.code={\def\hex@coords@row@fac{-1}},
87 hex/row direction is/up/.style={hex/row direction is=normal},
88 hex/row direction is/down/.style={hex/row direction is=reversed},
89 hex/row direction is/positive/.style={hex/row direction is=normal},
90 hex/row direction is/negative/.style={hex/row direction is=reversed},
91 hex/row direction is=normal,
92 hex/column direction is/.is choice,
93 hex/column direction is/normal/.code={\def\hex@coords@col@fac{1}},
94 hex/column direction is/reversed/.code={\def\hex@coords@col@fac{-1}},
95 hex/column direction is/right/.style={hex/column direction is=normal},
96 hex/column direction is/left/.style={hex/column direction is=reversed},
97 hex/column direction is/positive/.style={hex/column direction is=normal},
98 hex/column direction is/negative/.style={hex/column direction is=reversed},
99 hex/column direction is=normal,
100 % \end{macrocode}
101 %
102 % Make labels names of shapes of the hexes so we can use labels to
103 % place stuff
104 %
105 % \begin{macrocode}
106 hex/label is name/.is if=hex@label@is@name,
107 % \end{macrocode}
108 %
109 % If we have uneven number of rows in some columns.
110 %
111 % \begin{macrocode}
112 hex/short bottom columns/.is choice,
113 hex/short bottom columns/odd/.code={%
114 \def\hex@bot@short@col{isodd}
115 \def\hex@got@bot@short{istrue}
116 \hex@dbg{4}{Short columns (odd): \meaning\hex@bot@short@col}},
117 hex/short bottom columns/even/.code={
118 \def\hex@bot@short@col{iseven}
119 \def\hex@got@bot@short{istrue}
120 \hex@dbg{4}{Short column (even): \meaning\hex@bot@short@col}},
121 hex/short bottom columns/none/.code={
122 \def\hex@bot@short@col{isfalse}
123 \def\hex@got@bot@short{isfalse}
124 \hex@dbg{4}{Short columns (none): \meaning\hex@bot@short@col}},
125 hex/short bottom columns=none,
126 hex/short columns/.forward to=hex/short bottom columns,
127 hex/short top columns/.is choice,
128 hex/short top columns/odd/.code={%
129 \def\hex@top@short@col{isodd}
130 \def\hex@got@top@short{istrue}
131 \hex@dbg{4}{Short columns (odd): \meaning\hex@top@short@col}},
132 hex/short top columns/even/.code={
133 \def\hex@top@short@col{iseven}
134 \def\hex@got@top@short{istrue}
135 \hex@dbg{4}{Short column (even): \meaning\hex@top@short@col}},
136 hex/short top columns/none/.code={
137 \def\hex@top@short@col{isfalse}
138 \def\hex@got@top@short{isfalse}
139 \hex@dbg{4}{Short columns (none): \meaning\hex@top@short@col}},
140 hex/short top columns=none,
141 }
142 \message{^^JInitial hex coordinate setup:
143 Rows: factor=\hex@coords@row@fac, offset=\hex@coords@row@off
144 Columns: factor=\hex@coords@col@fac, offset=\hex@coords@col@off}
145 % \end{macrocode}
146 %
147 % \begin{HexKey*}{
148 % hex/coords/column,
149 % hex/coords/row,
150 % hex/coords/vertex,
151 % hex/coords/edge,
152 % hex/coords/offset}
153 %
154 % We define the keys for hexagon coordinates. These are the
155 % \spec{row}, \spec{column}, possible \spec{vertex} or
156 % \spec{edge}. Vertexes and edges are defined as
157 % multiple-choice. \spec{offset} specifies the offset from the
158 % centre in the direction of a vertex or edge. By default, the
159 % offset is one, meaning all the way to the vertex or edge.
160 %
161 % The key \spec{inverse row} specifies that the rows are given from
162 % the top down, but coordinates should be calculated as if the row
163 % was negative. This (should) allow us to design boards where rows
164 % increase downward, while still keeping the interface and remaining
165 % code somewhat reasonable and agnostic.
166 %
167 % Similarly, the key \spec{column 1}, will allow us to start the
168 % columns with 1.
169 %
170 %
171 %
172 % \begin{macrocode}
173 \tikzset{
174 /hex/coords/.cd,
175 column/.store in=\hex@col,
176 c/.store in=\hex@col,
177 row/.store in=\hex@row,
178 r/.store in=\hex@row,
179 offset/.store in=\hex@off,
180 o/.store in=\hex@off,
181 vertex/.is choice,
182 vertex/none/.code={\global\let\hex@vtx\@empty},
183 vertex/east/.code={\def\hex@vtx{0}},
184 vertex/north east/.code={\def\hex@vtx{60}},
185 vertex/north west/.code={\def\hex@vtx{120}},
186 vertex/west/.code={\def\hex@vtx{180}},
187 vertex/south west/.code={\def\hex@vtx{240}},
188 vertex/south east/.code={\def\hex@vtx{300}},
189 vertex/E/.code={\def\hex@vtx{0}},
190 vertex/NE/.code={\def\hex@vtx{60}},
191 vertex/NW/.code={\def\hex@vtx{120}},
192 vertex/W/.code={\def\hex@vtx{180}},
193 vertex/SW/.code={\def\hex@vtx{240}},
194 vertex/SE/.code={\def\hex@vtx{300}},
195 vertex/.default=none,
196 v/.forward to=/hex/coords/vertex=#1,
197 edge/.is choice,
198 edge/none/.code={\global\let\hex@edg\@empty},
199 edge/north east/.code={\def\hex@edg{30}},
200 edge/north/.code={\def\hex@edg{90}},
201 edge/north west/.code={\def\hex@edg{150}},
202 edge/south west/.code={\def\hex@edg{210}},
203 edge/south/.code={\def\hex@edg{270}},
204 edge/south east/.code={\def\hex@edg{330}},
205 edge/NE/.code={\def\hex@edg{30}},
206 edge/N/.code={\def\hex@edg{90}},
207 edge/NW/.code={\def\hex@edg{150}},
208 edge/SW/.code={\def\hex@edg{210}},
209 edge/S/.code={\def\hex@edg{270}},
210 edge/SE/.code={\def\hex@edg{330}},
211 edge/.default=none,
212 e/.forward to=/hex/coords/edge,
213 }
214 % \end{macrocode}
215 % \end{HexKey*}
216 %
217 % \begin{Macro}{\hex@coords@reset}
218 %
219 % This macro resets the hex coordinates to default values. That is
220 % row and column 0, no vertex or edge.
221 %
222 % \begin{macrocode}
223 \def\hex@coords@reset{%
224 \tikzset{%
225 /hex/coords/.cd,
226 column=0,
227 row=0,
228 edge=none,
229 vertex=none,
230 offset=1}}
231 % \end{macrocode}
232 % \end{Macro}
233 %
234 % The following calculates the Cartesian coordinates from Hex
235 % coordinates
236 %
237 % \begin{Syntax}
238 % (cs:hex column=\meta{C},row=\meta{R},vertex=\meta{V},edge=\meta{E})
239 % \end{Syntax}
240 %
241 % Given the hexagon column $C$ and row $R$ with hexagon radius $r$,
242 % the centre of the hexagon is at
243 %
244 % \begin{eqnarray*}
245 % x &=& 2C\frac{3}{4}r\\
246 % y &=& r(R - (C \% 2)\sin60^{\circ})
247 % \end{eqnarray*}
248 %
249 % If \meta{V} or \meta{E} are given, then these are added to the
250 % centre point.
251 %
252 % Note, $C$ and $R$ may be fractional numbers, which will specify a
253 % point inside a hex.
254 %
255 % We set-up the translation to Cartesian coordinates. First
256 % thing is to reset keys in \spec{/hex/coords}, and then parse out the
257 % keys given.
258 %
259 % \begin{macrocode}
260 \def\hex@coords@conv#1{%
261 \hex@coords@reset%
262 \tikzset{/hex/coords/.cd, #1}%
263 % \end{macrocode}
264 %
265 % Then we calculate the $x$ coordinate and set the dimension
266 % \cs{pgf@x}. We do this by
267 %
268 % $$x=c_e \frac{3}{2}\quad,$$
269 %
270 % where
271 %
272 % $$c_e = f_c (c+o_c)\quad,$$
273 %
274 % is the effective column (stored in \cs{hex@eff@col}) calculated from
275 % is the direction factor $f_c$ (set by \spec{hex/column direction
276 % is}) and the offset $o_c$ (set by \spec{hex/first column is}).
277 %
278 % \begin{macrocode}
279 \pgfmathparse{int(\hex@coords@col@fac*(\hex@col+\hex@coords@col@off))}%
280 \xdef\hex@eff@col{\pgfmathresult}%
281 \hex@dbg{2}{Effective column: \hex@coords@col@fac * (\hex@col +
282 \hex@coords@col@off) -> \hex@eff@col}%
283 \pgfmathparse{\hex@eff@col*1.5}%
284 \xdef\hex@x{\pgfmathresult}%
285 % \end{macrocode}
286 %
287 % And then for the $y$ coordinate and set the dimension \cs{pgf@y}.
288 %
289 % $$y = 2\left(r_e - c_e \operatorname{mod} 2\right) \cos60^{\circ}\quad,$$
290 %
291 % where
292 %
293 % $$r_e= 2 f_r (r+o_r) - (c+o_c) \operatorname{mod} 2\quad,$$
294 %
295 % is the effective row (stored as \cs{hex@eff@row}) calculated from
296 % the the direction factor $f_r$ (set by \spec{hex/row
297 % direction is}) and the offset $o_r$ (set by \spec{hex/first
298 % row is}).
299 %
300 % \begin{macrocode}
301 \pgfmathparse{int(\hex@coords@row@fac*(\hex@row+\hex@coords@row@off))}%
302 \xdef\hex@eff@row{\pgfmathresult}%
303 \hex@dbg{2}{Effective row: \hex@coords@row@fac * (\hex@row +
304 \hex@coords@row@off) -> \hex@eff@row}%
305 %\pgfmathparse{(2*\hex@eff@row-mod(round((\hex@col+\hex@coords@col@off)),2))*\hex@yy}%
306 \pgfmathparse{(2*\hex@eff@row-mod(abs(round(\hex@col+\hex@coords@col@off)),2))*\hex@yy}%
307 \xdef\hex@y{\pgfmathresult}%
308 % \end{macrocode}
309 %
310 % If we have a vertex specification add that location to the current
311 % coordinates. If not, set the point.
312 %
313 % \begin{macrocode}
314 \ifx\hex@vtx\@empty\else%
315 \pgfmathparse{\hex@x+\hex@off*cos(\hex@vtx)}\xdef\hex@x{\pgfmathresult}
316 \pgfmathparse{\hex@y+\hex@off*sin(\hex@vtx)}\xdef\hex@y{\pgfmathresult}
317 \fi%
318 % \ifx\hex@vtx\@empty\pgfpointxy{\hex@x}{\hex@y}\else%
319 % \pgfpointadd{\pgfpointxy{\hex@x}{\hex@y}}{%
320 % \pgfpointscale{\hex@off}{\pgfpointpolarxy{\hex@vtx}{1}}}\fi%
321 % \end{macrocode}
322 %
323 % If we have an edge specification add that location to the current
324 % coordinates.
325 %
326 % \begin{macrocode}
327 \ifx\hex@edg\@empty\else%
328 \pgfmathparse{\hex@x+\hex@off*\hex@yy*cos(\hex@edg)}%
329 \xdef\hex@x{\pgfmathresult}%
330 \pgfmathparse{\hex@y+\hex@off*\hex@yy*sin(\hex@edg)}%
331 \xdef\hex@y{\pgfmathresult}%
332 \fi%
333 % \ifx\hex@edg\@empty\else%
334 % \pgfpointadd{\pgfpointxy{\hex@x}{\hex@y}}{%
335 % \pgfpointscale{\hex@off}{\pgfpointpolarxy{\hex@edg}{\hex@yy}}}\fi
336 % \end{macrocode}
337 %
338 % For debugging, we can print out stuff.
339 %
340 % \begin{macrocode}
341 \pgfpointxy{\hex@x}{\hex@y}
342 \hex@dbg{2}{Hex coordinates: #1
343 ^^J c=`\hex@col'
344 ^^J r=`\hex@row'
345 ^^J v=`\hex@vtx'
346 ^^J e=`\hex@edg'
347 ^^J o=`\hex@off'
348 ^^J x=`\hex@x'
349 ^^J y=`\hex@y'}%
350 \global\let\hex@x\hex@x%
351 \global\let\hex@y\hex@y%
352 \global\let\hex@row\hex@row%
353 \global\let\hex@col\hex@col%
354 }
355 \tikzdeclarecoordinatesystem{hex}{%
356 \hex@coords@conv{#1}}
357 % \end{macrocode}
358 % \iffalse
359 %</hex>
360 % --------------------------------------------------------------------
361 % \fi