| 1 |
% !TeX root = ./test.tex |
| 2 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 |
% |
| 4 |
% This is the TikZbricks package |
| 5 |
% A package to draw bricks with tikz |
| 6 |
% Maintained by samcarter |
| 7 |
% |
| 8 |
% Project repository and bug tracker: |
| 9 |
% https://github.com/samcarter/TikZbricks |
| 10 |
% |
| 11 |
% Released under the LaTeX Project Public License v1.3c or later |
| 12 |
% See https://www.latex-project.org/lppl.txt |
| 13 |
% |
| 14 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 15 |
\ProvidesPackage{tikzbricks}[2024/12/16 version v0.6 Drawing bricks with TikZ] |
| 16 |
|
| 17 |
\RequirePackage{tikz} |
| 18 |
\RequirePackage{tikz-3dplot} |
| 19 |
\RequirePackage{xkeyval} |
| 20 |
|
| 21 |
% setting a default viewpoint |
| 22 |
\tdplotsetmaincoords{70}{160} |
| 23 |
|
| 24 |
% setting default values |
| 25 |
\DeclareOptionX{color}[red]{\def\brick@default@color{#1}} |
| 26 |
\DeclareOptionX{frontcolor}[\brick@color!60]{\def\brick@default@frontcolor{#1}} |
| 27 |
\DeclareOptionX{topcolor}[\brick@color!40]{\def\brick@default@topcolor{#1}} |
| 28 |
\DeclareOptionX{sidecolor}[\brick@color]{\def\brick@default@sidecolor{#1}} |
| 29 |
\DeclareOptionX{studcolor}[\brick@color]{\def\brick@default@studcolor{#1}} |
| 30 |
\DeclareOptionX{brickheight}[1.3]{\def\brick@default@height{#1}} |
| 31 |
\DeclareOptionX{bricklength}[1.0]{\def\brick@default@length{#1}} |
| 32 |
\DeclareOptionX{bricktext}[]{\def\brick@default@facetext{#1}} |
| 33 |
\DeclareOptionX{brickwidth}[1.0]{\def\brick@default@width{#1}} |
| 34 |
\DeclareOptionX{studradius}[0.35]{\def\brick@default@radius{#1}} |
| 35 |
\DeclareOptionX{studheight}[0.3]{\def\brick@default@studheight{#1}} |
| 36 |
\DeclareOptionX{studtext}[]{\def\brick@default@text{#1}} |
| 37 |
|
| 38 |
|
| 39 |
\ExecuteOptionsX{color,frontcolor,topcolor,sidecolor,studcolor,brickheight,bricklength,bricktext,brickwidth,studradius,studheight,studtext} |
| 40 |
|
| 41 |
\ProcessOptionsX |
| 42 |
|
| 43 |
% counter to dermine the position of a brick in a wall |
| 44 |
\newcounter{brickx} |
| 45 |
\newcounter{bricky} |
| 46 |
\newcounter{brickz} |
| 47 |
|
| 48 |
% draw a standalone brick |
| 49 |
% 1: optional argument |
| 50 |
% 2: lenght |
| 51 |
% 3: width |
| 52 |
\newcommand*{\brick}[3][]{% |
| 53 |
\setcounter{brickx}{0}% |
| 54 |
\setcounter{bricky}{0}% |
| 55 |
\setcounter{brickz}{0}% |
| 56 |
\begin{scope}% |
| 57 |
\tikzset{/brick/.cd,#1}% |
| 58 |
\brick@draw{#2}{#3}% |
| 59 |
\end{scope}% |
| 60 |
} |
| 61 |
|
| 62 |
% draw a brick in a wall |
| 63 |
% 1: optional argument |
| 64 |
% 2: lenght |
| 65 |
% 3: width |
| 66 |
\newcommand*{\wallbrick}[3][]{% |
| 67 |
\begin{scope}% |
| 68 |
\tikzset{/brick/.cd,#1}% |
| 69 |
\brick@draw{#2}{#3}% |
| 70 |
\end{scope}% |
| 71 |
\addtocounter{brickx}{#2}% |
| 72 |
} |
| 73 |
|
| 74 |
% envrinoment to arange the bricks in a wall |
| 75 |
% 1: optional argument passed to the tikzpicture |
| 76 |
\newenvironment{wall}[1][]{% |
| 77 |
\setcounter{brickx}{0}% |
| 78 |
\setcounter{bricky}{0}% |
| 79 |
\setcounter{brickz}{0}% |
| 80 |
\newcommand{\newrow}{% |
| 81 |
\setcounter{brickx}{0}% |
| 82 |
\addtocounter{brickz}{1}% |
| 83 |
}% |
| 84 |
\begin{tikzpicture}[#1] |
| 85 |
}{% |
| 86 |
\end{tikzpicture}% |
| 87 |
} |
| 88 |
|
| 89 |
\tikzset{ |
| 90 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 91 |
% |
| 92 |
% Pass unknown keys on to tikz |
| 93 |
% |
| 94 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 95 |
/brick/.search also={/tikz,/pgf}, |
| 96 |
/brick/.cd, |
| 97 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 98 |
% |
| 99 |
% brick options |
| 100 |
% |
| 101 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 102 |
color/.code = \def\brick@color{#1}, |
| 103 |
color = \brick@default@color, |
| 104 |
frontcolor/.code = \def\brick@frontcolor{#1}, |
| 105 |
frontcolor = \brick@default@frontcolor, |
| 106 |
topcolor/.code = \def\brick@topcolor{#1}, |
| 107 |
topcolor = \brick@default@topcolor, |
| 108 |
sidecolor/.code = \def\brick@sidecolor{#1}, |
| 109 |
sidecolor = \brick@default@sidecolor, |
| 110 |
studcolor/.code = \def\brick@studcolor{#1}, |
| 111 |
studcolor = \brick@default@studcolor, |
| 112 |
brickheight/.code = \def\brick@height{#1}, |
| 113 |
brickheight = \brick@default@height, |
| 114 |
bricklength/.code = \def\brick@length{#1}, |
| 115 |
bricklength = \brick@default@length, |
| 116 |
bricktext/.code = \def\brick@facetext{#1}, |
| 117 |
bricktext = \brick@default@facetext, |
| 118 |
brickwidth/.code = \def\brick@width{#1}, |
| 119 |
brickwidth = \brick@default@width, |
| 120 |
studradius/.code = \def\brick@radius{#1}, |
| 121 |
studradius = \brick@default@radius, |
| 122 |
studheight/.code = \def\brick@studheight{#1}, |
| 123 |
studheight = \brick@default@studheight, |
| 124 |
studtext/.code = \def\brick@text{#1}, |
| 125 |
studtext = \brick@default@text, |
| 126 |
} |
| 127 |
|
| 128 |
\newcommand{\brick@draw}[2]{% |
| 129 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 130 |
% |
| 131 |
% store the current scale factor |
| 132 |
% from https://github.com/samcarter/tikzlings/issues/3#issuecomment-461373494 |
| 133 |
% |
| 134 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 135 |
\pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}% |
| 136 |
\pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))*sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}% |
| 137 |
% |
| 138 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 139 |
% |
| 140 |
% Drawing the brick |
| 141 |
% |
| 142 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 143 |
\begin{scope}[tdplot_main_coords] |
| 144 |
|
| 145 |
% Drawing faces |
| 146 |
\pgfmathparse{sign(sin(\tdplotmainphi))} |
| 147 |
\let\brick@sin\pgfmathresult |
| 148 |
\pgfmathparse{sign(cos(\tdplotmainphi))} |
| 149 |
\let\brick@cos\pgfmathresult |
| 150 |
|
| 151 |
\ifnum\brick@sin<0 |
| 152 |
% Right |
| 153 |
\fill[\brick@sidecolor,thick] |
| 154 |
( |
| 155 |
\brick@length*\value{brickx}, |
| 156 |
\brick@width*\value{bricky}, |
| 157 |
\brick@height*\value{brickz} |
| 158 |
) |
| 159 |
-- ++(0,0,\brick@height) |
| 160 |
-- ++(0,\brick@width*#2,0) |
| 161 |
-- ++(0,0,-\brick@height) |
| 162 |
-- cycle; |
| 163 |
\else |
| 164 |
% Left |
| 165 |
\fill[\brick@sidecolor,thick] |
| 166 |
( |
| 167 |
{\brick@length*(#1+\value{brickx})}, |
| 168 |
\brick@width*\value{bricky}, |
| 169 |
\brick@height*\value{brickz} |
| 170 |
) |
| 171 |
-- ++(0,0,\brick@height) |
| 172 |
-- ++(0,\brick@width*#2,0) |
| 173 |
-- ++(0,0,-\brick@height) |
| 174 |
-- cycle; |
| 175 |
\fi |
| 176 |
|
| 177 |
\ifnum\brick@cos<0 |
| 178 |
% Front |
| 179 |
\fill[\brick@frontcolor,thick] |
| 180 |
( |
| 181 |
\brick@length*\value{brickx}, |
| 182 |
{\brick@width*(#2+\value{bricky})}, |
| 183 |
\brick@height*\value{brickz} |
| 184 |
) |
| 185 |
-- ++(\brick@length*#1,0,0) |
| 186 |
-- ++(0,0,\brick@height) |
| 187 |
-- ++(-\brick@length*#1,0,0) |
| 188 |
-- cycle; |
| 189 |
|
| 190 |
\node[align=center, transform shape, canvas is zx plane at y=0, rotate=-90] at ( |
| 191 |
\brick@length*\value{brickx} + 0.5*\brick@length*#1, |
| 192 |
{\brick@width*(#2+\value{bricky})}, |
| 193 |
\brick@height*\value{brickz} + 0.5*\brick@height |
| 194 |
) {\brick@facetext}; |
| 195 |
|
| 196 |
\else |
| 197 |
% Back |
| 198 |
\fill[\brick@frontcolor,thick] |
| 199 |
( |
| 200 |
\brick@length*\value{brickx}, |
| 201 |
\brick@width*\value{bricky}, |
| 202 |
\brick@height*\value{brickz} |
| 203 |
) |
| 204 |
-- ++(\brick@length*#1,0,0) |
| 205 |
-- ++(0,0,\brick@height) |
| 206 |
-- ++(-\brick@length*#1,0,0) |
| 207 |
-- cycle; |
| 208 |
\fi |
| 209 |
|
| 210 |
% Top |
| 211 |
\fill[\brick@topcolor,thick] |
| 212 |
( |
| 213 |
\brick@length*\value{brickx}, |
| 214 |
\brick@width*\value{bricky}, |
| 215 |
{\brick@height*(1+\value{brickz})} |
| 216 |
) |
| 217 |
-- ++(0,\brick@width*#2,0) |
| 218 |
-- ++(\brick@length*#1,0,0) |
| 219 |
-- +(0,-\brick@width*#2,0) |
| 220 |
-- cycle; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
% Drawing studs |
| 225 |
\foreach \x in {1,...,#1}{ |
| 226 |
\foreach \y in {1,...,#2}{ |
| 227 |
|
| 228 |
% Bottom circle |
| 229 |
\fill[\brick@studcolor] |
| 230 |
( |
| 231 |
{\brick@length*(\x-0.5+\value{brickx})}, |
| 232 |
{\brick@width*(\y-0.5+\value{bricky})}, |
| 233 |
{\brick@height*(1+\value{brickz})} |
| 234 |
) |
| 235 |
circle [radius=\brick@radius]; |
| 236 |
|
| 237 |
% Rectangular stem |
| 238 |
\fill[\brick@studcolor] |
| 239 |
( |
| 240 |
{\brick@length*(\x-0.5+\value{brickx})}, |
| 241 |
{\brick@width*(\y-0.5+\value{bricky})}, |
| 242 |
{\brick@height*(1+\value{brickz})} |
| 243 |
) |
| 244 |
++(canvas cs:x=-\brick@radius/0.35*10) |
| 245 |
-- ++(canvas cs:y=\brick@studheight*17.8) |
| 246 |
-- ++(canvas cs:x=\brick@radius/0.35*20) |
| 247 |
-- ++(canvas cs:y=-\brick@studheight*17.8); |
| 248 |
|
| 249 |
% Top circle with text |
| 250 |
\fill[\brick@topcolor] |
| 251 |
( |
| 252 |
{\brick@length*(\x-0.5+\value{brickx})}, |
| 253 |
{\brick@width*(\y-0.5+\value{bricky})}, |
| 254 |
{\brick@height*(1+\value{brickz})} |
| 255 |
) |
| 256 |
++(canvas cs:y=\brick@studheight*17.8) |
| 257 |
circle [radius=\brick@radius] |
| 258 |
node[\brick@color,scale=\scalingfactor*0.5, font=\sffamily] {\brick@text}; |
| 259 |
|
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
\end{scope} |
| 264 |
} |
| 265 |
|