%% The LaTeX package tikzfill - version 1.0.1 (2023/08/08)
%% tikzlibraryfill.hexagon.code.tex: filling with hexagon patterns
%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2022-2023 by Prof. Dr. Dr. Thomas F. Sturm <thomas dot sturm at unibw dot de>
%% -------------------------------------------------------------------------------------------
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `author-maintained'.
%%
%% This work consists of all files listed in README.md
%%
\ProvidesFile{tikzlibraryfill.hexagon.code.tex}[2023/08/08 v1.0.1 Hexagon pattern library]

\usetikzlibrary{patterns.meta}

\ifx\ExplSyntaxOn\undefined
  \input expl3-generic\relax
\fi
\ExplSyntaxOn

\int_new:N \l__tikzfill_hex_ring_int
\tl_new:N \l__tikzfill_hex_a_tl
\tl_new:N \l__tikzfill_hex_as_tl
\tl_new:N \l__tikzfill_hex_b_tl
\tl_new:N \l__tikzfill_hex_bs_tl
\tl_new:N \l__tikzfill_hex_linewidth_tl
\tl_new:N \l__tikzfill_hex_pos_tl

\pgfdeclarepattern
  {
    name = hexagon~grid,
    parameters =
      {
        \pgfkeysvalueof{/pgf/pattern~keys/size},
        \pgfkeysvalueof{/pgf/pattern~keys/angle},
        \pgfkeysvalueof{/pgf/pattern~keys/xshift},
        \pgfkeysvalueof{/pgf/pattern~keys/yshift},
        \pgfkeysvalueof{/pgf/pattern~keys/line~width},
      },
    defaults =
      {
        size       /.initial = 8mm,
        angle      /.initial = 0,
        line~width /.initial = 0.4pt,
        xshift     /.initial = 0pt,
        yshift     /.initial = 0pt,
      },
    set~up~code =
      {
        \fp_set:Nn \l_tmpa_fp { \pgfkeysvalueof{/pgf/pattern~keys/size} }
        \tl_set:Nx \l__tikzfill_hex_a_tl { \fp_to_dim:N \l_tmpa_fp }
        \tl_set:Nx \l__tikzfill_hex_b_tl { \fp_to_dim:n { \l_tmpa_fp*sqrt(3)/2 } }
        \tl_set:Nx \l__tikzfill_hex_linewidth_tl  { \fp_to_dim:n { \pgfkeysvalueof{/pgf/pattern~keys/line~width} } }
      },
    bottom~left =
      {
        \pgfpoint{ -\l__tikzfill_hex_b_tl-.1pt }{ -\l__tikzfill_hex_a_tl-\l__tikzfill_hex_linewidth_tl }
      },
    top~right =
      {
        \pgfpoint{ \l__tikzfill_hex_b_tl+.1pt }{ 2*\l__tikzfill_hex_a_tl+.1pt }
      },
    tile~size =
      {
        \pgfpoint{ 2*\l__tikzfill_hex_b_tl }{ 3*\l__tikzfill_hex_a_tl }
      },
    tile~transformation =
      {
        \pgftransformrotate{ \pgfkeysvalueof{/pgf/pattern~keys/angle} }
        \pgftransformshift
          {
            \pgfpoint{ \pgfkeysvalueof{/pgf/pattern~keys/xshift} }
                     { \pgfkeysvalueof{/pgf/pattern~keys/yshift} }
          }
      },
    code=
      {
        \pgfsetlinewidth{\l__tikzfill_hex_linewidth_tl}
        \pgfpathmoveto{\pgfpoint{ 0                 }{ 2*\l__tikzfill_hex_a_tl+.1pt }}
        \pgfpathlineto{\pgfpoint{ 0                 }{ \l__tikzfill_hex_a_tl        }}
        \pgfpathlineto{\pgfpoint{ -\l__tikzfill_hex_b_tl }{ 0.5*\l__tikzfill_hex_a_tl    }}
        \pgfpathlineto{\pgfpoint{ -\l__tikzfill_hex_b_tl }{ -0.5*\l__tikzfill_hex_a_tl   }}
        \pgfpathlineto{\pgfpoint{ 0                 }{ -\l__tikzfill_hex_a_tl       }}
        \pgfpathlineto{\pgfpoint{ \l__tikzfill_hex_b_tl  }{ -0.5*\l__tikzfill_hex_a_tl   }}
        \pgfpathlineto{\pgfpoint{ \l__tikzfill_hex_b_tl  }{ 0.5*\l__tikzfill_hex_a_tl    }}
        \pgfpathlineto{\pgfpoint{ 0                 }{ \l__tikzfill_hex_a_tl        }}
        \pgfusepath{stroke}
      }
  }

\tikzset{
  pattern~hexagon~grid /.style =
    {
      pattern={hexagon~grid[#1]}
    }
}



\pgfdeclarepattern
  {
    name = hexagon,
    parameters =
      {
        \pgfkeysvalueof{/pgf/pattern~keys/size},
        \pgfkeysvalueof{/pgf/pattern~keys/angle},
        \pgfkeysvalueof{/pgf/pattern~keys/xshift},
        \pgfkeysvalueof{/pgf/pattern~keys/yshift},
        \pgfkeysvalueof{/pgf/pattern~keys/line~width},
        \pgfkeysvalueof{/pgf/pattern~keys/pos},
        \pgfkeysvalueof{/pgf/pattern~keys/band},
        \pgfkeysvalueof{/pgf/pattern~keys/_fill_},
      },
    defaults =
      {
        size       /.initial = 8mm,
        angle      /.initial = 0,
        xshift     /.initial = 0pt,
        yshift     /.initial = 0pt,
        line~width /.initial = 0.4pt,
        pos        /.initial = 0.8,
        band       /.initial = 0,
        _fill_     /.initial = 1,
        fill       /.style = { _fill_=1 },
        draw       /.style = { _fill_=0 },
      },
    set~up~code =
      {
        \fp_set:Nn \l_tmpa_fp { \pgfkeysvalueof{/pgf/pattern~keys/size} }
        \tl_set:Nx \l__tikzfill_hex_a_tl { \fp_to_dim:N \l_tmpa_fp }
        \tl_set:Nx \l__tikzfill_hex_b_tl { \fp_to_dim:n { \l_tmpa_fp*sqrt(3)/2 } }
        \tl_set:Nx \l__tikzfill_hex_linewidth_tl  { \fp_to_dim:n { \pgfkeysvalueof{/pgf/pattern~keys/line~width} } }
        \tl_set:Nx \l__tikzfill_hex_pos_tl { \fp_to_decimal:n { min ( 1 , max( 0, \pgfkeysvalueof{/pgf/pattern~keys/pos} )) } }
      },
    bottom~left =
      {
        \pgfpoint{ -\l__tikzfill_hex_b_tl-\l__tikzfill_hex_linewidth_tl }{ -\l__tikzfill_hex_a_tl-\l__tikzfill_hex_linewidth_tl }
      },
    top~right =
      {
        \pgfpoint{ \l__tikzfill_hex_b_tl+\l__tikzfill_hex_linewidth_tl }{ 2*\l__tikzfill_hex_a_tl+\l__tikzfill_hex_linewidth_tl }
      },
    tile~size =
      {
        \pgfpoint{ 2*\l__tikzfill_hex_b_tl }{ 3*\l__tikzfill_hex_a_tl }
      },
    tile~transformation =
      {
        \pgftransformrotate{ \pgfkeysvalueof{/pgf/pattern~keys/angle} }
        \int_compare:nNnTF { \pgfkeysvalueof{/pgf/pattern~keys/band} } = 0
        {
          \pgftransformshift
            {
              \pgfpoint{ \pgfkeysvalueof{/pgf/pattern~keys/xshift} }
                       { \pgfkeysvalueof{/pgf/pattern~keys/yshift} }
            }
        }
        {
          \pgftransformshift
            {
              \pgfpoint{ \pgfkeysvalueof{/pgf/pattern~keys/xshift}-\l__tikzfill_hex_b_tl }
                       { \pgfkeysvalueof{/pgf/pattern~keys/yshift}+1.5*\l__tikzfill_hex_a_tl }
            }
        }
      },
    code=
      {
        \tl_set:Nx \l__tikzfill_hex_as_tl { \fp_to_dim:n { \l__tikzfill_hex_pos_tl*\l__tikzfill_hex_a_tl } }
        \tl_set:Nx \l__tikzfill_hex_bs_tl { \fp_to_dim:n { \l__tikzfill_hex_pos_tl*\l__tikzfill_hex_b_tl } }
        \pgfpathmoveto{\pgfpoint{ 0                  }{ \l__tikzfill_hex_as_tl        }}
        \pgfpathlineto{\pgfpoint{ -\l__tikzfill_hex_bs_tl }{ 0.5*\l__tikzfill_hex_as_tl    }}
        \pgfpathlineto{\pgfpoint{ -\l__tikzfill_hex_bs_tl }{ -0.5*\l__tikzfill_hex_as_tl   }}
        \pgfpathlineto{\pgfpoint{ 0                  }{ -\l__tikzfill_hex_as_tl       }}
        \pgfpathlineto{\pgfpoint{ \l__tikzfill_hex_bs_tl  }{ -0.5*\l__tikzfill_hex_as_tl   }}
        \pgfpathlineto{\pgfpoint{ \l__tikzfill_hex_bs_tl  }{ 0.5*\l__tikzfill_hex_as_tl    }}
        \pgfpathclose
        \int_compare:nNnTF { \pgfkeysvalueof{/pgf/pattern~keys/_fill_} } = 1
          {
            \pgfusepath{fill}
          }
          {
            \pgfsetlinewidth{\l__tikzfill_hex_linewidth_tl}
            \pgfusepath{stroke}
          }
      }
  }


\tikzset{
  pattern~hexagon /.style =
    {
      preaction={pattern={hexagon[#1,band=1]}},
      pattern={hexagon[#1,band=0]}
    }
}



\pgfdeclarepattern
  {
    name = hexagon~cycle,
    parameters =
      {
        \pgfkeysvalueof{/pgf/pattern~keys/size},
        \pgfkeysvalueof{/pgf/pattern~keys/angle},
        \pgfkeysvalueof{/pgf/pattern~keys/xshift},
        \pgfkeysvalueof{/pgf/pattern~keys/yshift},
        \pgfkeysvalueof{/pgf/pattern~keys/rings},
        \pgfkeysvalueof{/pgf/pattern~keys/gap},
        \pgfkeysvalueof{/pgf/pattern~keys/band},
      },
    defaults =
      {
        size       /.initial = 8mm,
        angle      /.initial = 0,
        xshift     /.initial = 0pt,
        yshift     /.initial = 0pt,
        rings      /.initial = 3,
        gap        /.initial = 1,
        band       /.initial = 0,
      },
    set~up~code =
      {
        \fp_set:Nn \l_tmpa_fp { \pgfkeysvalueof{/pgf/pattern~keys/size} }
        \tl_set:Nx \l__tikzfill_hex_a_tl { \fp_to_dim:N \l_tmpa_fp }
        \tl_set:Nx \l__tikzfill_hex_b_tl { \fp_to_dim:n { \l_tmpa_fp*sqrt(3)/2 } }
        \int_set:Nn \l__tikzfill_hex_ring_int { \fp_to_int:n { max ( 0, \pgfkeysvalueof{/pgf/pattern~keys/rings} ) }}
        \fp_set:Nn \l_tmpa_fp { max ( 0.01, \pgfkeysvalueof{/pgf/pattern~keys/gap} ) }
        \tl_set:Nx \l__tikzfill_hex_linewidth_tl  { \fp_to_dim:n
          {
            \l__tikzfill_hex_b_tl / (( \l__tikzfill_hex_ring_int + 0.5 )*( 1 + \l_tmpa_fp ))
          }}
      },
    bottom~left =
      {
        \pgfpoint{ -\l__tikzfill_hex_b_tl }{ -\l__tikzfill_hex_a_tl }
      },
    top~right =
      {
        \pgfpoint{ \l__tikzfill_hex_b_tl }{ 2*\l__tikzfill_hex_a_tl }
      },
    tile~size =
      {
        \pgfpoint{ 2*\l__tikzfill_hex_b_tl }{ 3*\l__tikzfill_hex_a_tl }
      },
    tile~transformation =
      {
        \pgftransformrotate{ \pgfkeysvalueof{/pgf/pattern~keys/angle} }
        \int_compare:nNnTF { \pgfkeysvalueof{/pgf/pattern~keys/band} } = 0
        {
          \pgftransformshift
            {
              \pgfpoint{ \pgfkeysvalueof{/pgf/pattern~keys/xshift} }
                       { \pgfkeysvalueof{/pgf/pattern~keys/yshift} }
            }
        }
        {
          \pgftransformshift
            {
              \pgfpoint{ \pgfkeysvalueof{/pgf/pattern~keys/xshift}-\l__tikzfill_hex_b_tl }
                       { \pgfkeysvalueof{/pgf/pattern~keys/yshift}+1.5*\l__tikzfill_hex_a_tl }
            }
        }
      },
    code=
      {
        \tl_set:Nn  \l__tikzfill_hex_pos_tl { \l__tikzfill_hex_linewidth_tl/2/\l__tikzfill_hex_b_tl }
        \tl_set:Nx \l__tikzfill_hex_as_tl { \fp_to_dim:n { \l__tikzfill_hex_pos_tl*\l__tikzfill_hex_a_tl } }
        \tl_set:Nx \l__tikzfill_hex_bs_tl { \fp_to_dim:n { \l__tikzfill_hex_pos_tl*\l__tikzfill_hex_b_tl } }
        \pgfpathmoveto{\pgfpoint{ 0                  }{ \l__tikzfill_hex_as_tl        }}
        \pgfpathlineto{\pgfpoint{ -\l__tikzfill_hex_bs_tl }{ 0.5*\l__tikzfill_hex_as_tl    }}
        \pgfpathlineto{\pgfpoint{ -\l__tikzfill_hex_bs_tl }{ -0.5*\l__tikzfill_hex_as_tl   }}
        \pgfpathlineto{\pgfpoint{ 0                  }{ -\l__tikzfill_hex_as_tl       }}
        \pgfpathlineto{\pgfpoint{ \l__tikzfill_hex_bs_tl  }{ -0.5*\l__tikzfill_hex_as_tl   }}
        \pgfpathlineto{\pgfpoint{ \l__tikzfill_hex_bs_tl  }{ 0.5*\l__tikzfill_hex_as_tl    }}
        \pgfpathclose
        \pgfusepath{fill}
        %
        \pgfsetlinewidth{\l__tikzfill_hex_linewidth_tl}
        \int_step_function:nN { \l__tikzfill_hex_ring_int } \__tikzfill_hex_draw_ring:n
      }
  }


\cs_set:Npn \__tikzfill_hex_draw_ring:n #1
  {
    \tl_set:Nn  \l__tikzfill_hex_pos_tl { #1/( \l__tikzfill_hex_ring_int + 0.5 )}
    \tl_set:Nx \l__tikzfill_hex_as_tl { \fp_to_dim:n { \l__tikzfill_hex_pos_tl*\l__tikzfill_hex_a_tl } }
    \tl_set:Nx \l__tikzfill_hex_bs_tl { \fp_to_dim:n { \l__tikzfill_hex_pos_tl*\l__tikzfill_hex_b_tl } }
    \pgfpathmoveto{\pgfpoint{ 0                  }{ \l__tikzfill_hex_as_tl        }}
    \pgfpathlineto{\pgfpoint{ -\l__tikzfill_hex_bs_tl }{ 0.5*\l__tikzfill_hex_as_tl    }}
    \pgfpathlineto{\pgfpoint{ -\l__tikzfill_hex_bs_tl }{ -0.5*\l__tikzfill_hex_as_tl   }}
    \pgfpathlineto{\pgfpoint{ 0                  }{ -\l__tikzfill_hex_as_tl       }}
    \pgfpathlineto{\pgfpoint{ \l__tikzfill_hex_bs_tl  }{ -0.5*\l__tikzfill_hex_as_tl   }}
    \pgfpathlineto{\pgfpoint{ \l__tikzfill_hex_bs_tl  }{ 0.5*\l__tikzfill_hex_as_tl    }}
    \pgfpathclose
    \pgfusepath{stroke}
  }


\tikzset{
  pattern~hexagon~cycle /.style =
    {
      preaction={pattern={hexagon~cycle[#1,band=1]}},
      pattern={hexagon~cycle[#1,band=0]}%
    }
}


\ExplSyntaxOff