summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/tikzbricks/tikzbricks.sty
blob: 5cc38d67a0a0c0de97909b6afde1d4fb14f51628 (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
% !TeX root = ./test.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This is the TikZbricks package
% A package to draw bricks with tikz
% Maintained by samcarter
%
% Project repository and bug tracker:
% https://github.com/samcarter/TikZbricks
%
% Released under the LaTeX Project Public License v1.3c or later
% See http://www.latex-project.org/lppl.txt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{tikzbricks}[2021/08/13 version v0.3 Drawing bricks with TikZ]

\RequirePackage{tikz}
\RequirePackage{tikz-3dplot}

% setting a default viewpoint
\tdplotsetmaincoords{70}{160}

% counter to dermine the position of a brick in a wall
\newcounter{brickx}
\newcounter{bricky}
\newcounter{brickz}

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

% draw a brick in a wall
% 1: optional argument
% 2: lenght
% 3: width
\newcommand*{\wallbrick}[3][]{%
  \begin{scope}%
    \tikzset{/brick/.cd,#1}%
    \brick@draw{#2}{#3}%
  \end{scope}%
  \addtocounter{brickx}{#2}%
}

% envrinoment to arange the bricks in a wall
% 1: optional argument passed to the tikzpicture
\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,
  frontcolor/.code    = \def\brick@frontcolor{#1},
  frontcolor          = \brick@color!60,
  topcolor/.code      = \def\brick@topcolor{#1},
  topcolor            = \brick@color!40,
  sidecolor/.code     = \def\brick@sidecolor{#1},
  sidecolor           = \brick@color,  
  studcolor/.code     = \def\brick@studcolor{#1},
  studcolor           = \brick@color,    
  brickheight/.code   = \def\brick@height{#1},
  brickheight         = 1.3,
  bricklength/.code   = \def\brick@length{#1},
  bricklength         = 1.0,
  brickwidth/.code    = \def\brick@width{#1},
  brickwidth          = 1.0,    
  studradius/.code    = \def\brick@radius{#1},
  studradius          = 0.35,
  studheight/.code    = \def\brick@studheight{#1},
  studheight          = 5,
  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)))}%
  %
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % Drawing the brick
  % 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \begin{scope}[tdplot_main_coords]
  
    % Drawing faces
    
    % Front
    \fill[\brick@frontcolor,thick]
      (
        \brick@length*\value{brickx},
        {\brick@width*(#2+\value{bricky})},
        \brick@height*\value{brickz}
      )
      -- ++(\brick@length*#1,0,0) 
      -- ++(0,0,\brick@height) 
      -- ++(-\brick@length*#1,0,0) 
      -- cycle;
      
    % Top 
    \fill[\brick@topcolor,thick] 
      (
        \brick@length*\value{brickx},
        \brick@width*\value{bricky},
        {\brick@height*(1+\value{brickz})}
      )
      -- ++(0,\brick@width*#2,0) 
      -- ++(\brick@length*#1,0,0) 
      -- +(0,-\brick@width*#2,0) 
      -- cycle;
      
    % Left
    \fill[\brick@sidecolor,thick] 
      (
        {\brick@length*(#1+\value{brickx})},
        \brick@width*\value{bricky},
        \brick@height*\value{brickz}
      ) 
      -- ++(0,0,\brick@height) 
      -- ++(0,\brick@width*#2,0) 
      -- ++(0,0,-\brick@height) 
      -- cycle; 
    
    % Drawing studs
    \foreach \x in {1,...,#1}{
      \foreach \y in {1,...,#2}{
      
        % Bottom circle
        \fill[\brick@studcolor]   
          (
            {\brick@length*(\x-0.5+\value{brickx})},
            {\brick@width*(\y-0.5+\value{bricky})},
            {\brick@height*(1+\value{brickz})}
          ) 
          circle [radius=\brick@radius]; 
        
        % Rectangular stem
        \fill[\brick@studcolor] 
          (
            {\brick@length*(\x-0.5+\value{brickx})},
            {\brick@width*(\y-0.5+\value{bricky})},
            {\brick@height*(1+\value{brickz})}
          ) 
          ++(canvas cs:x=-\brick@radius/0.35*10) 
          -- ++(canvas cs:y=\brick@studheight) 
          -- ++(canvas cs:x=\brick@radius/0.35*20) 
          -- ++(canvas cs:y=-\brick@studheight);
          
        % Top circle with text
        \fill[\brick@topcolor] 
          (
            {\brick@length*(\x-0.5+\value{brickx})},
            {\brick@width*(\y-0.5+\value{bricky})},
            {\brick@height*(1+\value{brickz})}
          ) 
          ++(canvas cs:y=\brick@studheight) 
          circle [radius=\brick@radius] 
          node[\brick@color,scale=\scalingfactor*0.5, font=\sffamily] {\brick@text};

      }
    }
    
  \end{scope}
}