summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/tikzbricks/tikzbricks.sty
blob: 261f29780f4f0f7ec2511b36aea22573cee0955e (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
 % !TeX root = ./test.tex
\ProvidesPackage{tikzbricks}[2021/08/10 version v0.1 Drawing bricks with TikZ]

\RequirePackage{tikz}
\RequirePackage{tikz-3dplot}
\tdplotsetmaincoords{70}{160}
\newcounter{brickx}
\newcounter{bricky}
\newcounter{brickz}
\setcounter{brickx}{0}
\setcounter{bricky}{0}
\setcounter{brickz}{0}

\newcommand*{\brick}[3][]{%
  \setcounter{brickx}{0}%
  \setcounter{bricky}{0}%
  \setcounter{brickz}{0}%
  \begin{scope}%
    \tikzset{/brick/.cd,#1}%
    \brick@draw{#2}{#3}%
  \end{scope}%
}

\newcommand*{\wallbrick}[3][]{%
  \begin{scope}%
    \tikzset{/brick/.cd,#1}%
    \brick@draw{#2}{#3}%
  \end{scope}%
  \addtocounter{brickx}{#2}%
}

\newenvironment{wall}[1][]{%
  \setcounter{brickx}{0}%
  \setcounter{bricky}{0}%
  \setcounter{brickz}{0}%
  \newcommand{\newrow}{%
    \setcounter{brickx}{0}%
    \addtocounter{brickz}{1}%
  }%
  \begin{tikzpicture}[#1]
}{%
  \end{tikzpicture}%
}

\tikzset{
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % Pass unknown keys on to tikz
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  /brick/.search also={/tikz,/pgf},
  /brick/.cd,
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % brick options
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  color/.code         = \def\brick@color{#1},
  color               = red,
  brickheight/.code   = \def\brick@height{#1},
  brickheight         = 1.3,
  studtext/.code      = \def\brick@text{#1},
  studtext            = {},
}

\newcommand{\brick@draw}[2]{%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % store the current scale factor
  % from https://github.com/samcarter/tikzlings/issues/3#issuecomment-461373494
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
  \pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))*sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}%
  \pgfmathsetmacro{\xscalefactor}{\tmpscaleA}
  \pgfmathsetmacro{\yscalefactor}{\tmpscaleD}
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % Drawing the brick
  % 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \begin{scope}[tdplot_main_coords]
  
    % drawing faces
    \fill[\brick@color!60,thick] (0+\value{brickx},#2+\value{bricky},0+\brick@height*\value{brickz}) -- ++(#1,0,0) -- ++(0,0,\brick@height) -- ++(-#1,0,0) -- cycle; % top
    \fill[\brick@color!40,thick] (0+\value{brickx},0+\value{bricky},\brick@height+\brick@height*\value{brickz}) -- ++(0,#2,0) -- ++(#1,0,0) -- +(0,-#2,0) -- cycle; % front
    \fill[\brick@color,thick] (#1+\value{brickx},0+\value{bricky},0+\brick@height*\value{brickz}) -- ++(0,0,\brick@height) -- ++(0,#2,0) -- ++(0,0,-\brick@height) -- cycle; % right
    
    %drawing studs
    \foreach \x in {1,...,#1}{
      \foreach \y in {1,...,#2}{
        \fill[\brick@color] (\x-0.5+\value{brickx},\y-0.5+\value{bricky},\brick@height+\brick@height*\value{brickz}) circle [radius=0.35]; 
        \fill[\brick@color] (\x-0.5+\value{brickx},\y-0.5+\value{bricky},\brick@height+\brick@height*\value{brickz}) ++(canvas cs:x=-10) -- ++(canvas cs:y=3) -- ++(canvas cs:x=20) -- ++(canvas cs:y=-3);
        \fill[\brick@color!60] (\x-0.5+\value{brickx},\y-0.5+\value{bricky},\brick@height+\brick@height*\value{brickz}) ++(canvas cs:y=3) circle [radius=0.35]  node[\brick@color,scale=\scalingfactor*0.5, font=\sffamily] {\brick@text};
      }
    }
    
  \end{scope}
}