www.eamoncaddigan.net

Content and configuration for https://www.eamoncaddigan.net
git clone https://git.eamoncaddigan.net/www.eamoncaddigan.net.git
Log | Files | Refs | Submodules | README

hexgrid.ps (1681B)


      1 %!PS
      2 % Adjust these to suit your needs, units specified in "points" (1/72 inch)
      3 /w 612 def /h 792 def       % US Letter paper
      4 /st 1 72 mul 300 div def    % Stroke width (1 'dots' in a 300 dpi resolution)
      5 /co 0.6 def                 % Gray-level of the line (0 = black, 1 = white)
      6 /hw 1 72 mul def            % Width of a hex (1 inch)
      7 
      8 % Derived variables
      9 /hh hw 2 mul 3 sqrt div def % Hex height
     10 /hhw hw 2 div def           % Half hex width
     11 /qhh hh 4 div def           % Quarter hex height
     12 /th hh 1.5 mul def          % Tile height
     13 /tw hw def                  % Tile width
     14 /hcx hhw def                % Center of a hex within the tile (x)
     15 /hcy hh 1.25 mul def        % Center of a hex within the tile (y)
     16 
     17 % Draw one tile of a repeating hex pattern. This coves one hex width and 1.5
     18 % hex heights
     19 % (x y -- )
     20 /drawtile {
     21     moveto
     22     hhw qhh rlineto
     23     0 qhh 2 mul rlineto
     24     hhw -1 mul qhh rlineto
     25     0 qhh 2 mul rlineto
     26     hhw qhh -5 mul rmoveto
     27     hhw qhh -1 mul rlineto
     28     hhw -1 mul qhh 3 mul rmoveto
     29     hhw qhh rlineto
     30     stroke
     31 } def
     32 
     33 % Find the starting point of the first tile along the given coordinate
     34 % (page-size tile-size hex-center -- tile-start)
     35 /start-tiles {
     36     /hex-center exch def
     37     /tile-size exch def
     38     /page-size exch def
     39     page-size 2 div hex-center sub
     40     page-size tile-size div 2 div floor
     41     tile-size mul
     42     sub
     43 } def
     44 
     45 % Page and line setup
     46 << /PageSize [w h] >> setpagedevice
     47 co setgray
     48 st setlinewidth
     49 
     50 % Draw the grid so that one hex is centered in the page center
     51 h th hcy start-tiles
     52 th h
     53 {
     54     w tw hcx start-tiles
     55     tw w
     56     {
     57         1 index     % (y x -- y x y)
     58         drawtile    % (y x y -- y)
     59     } for
     60     pop
     61 } for