The code adds some completely useless invisible (or rather white) stuff. The lines
\clip(0pt,403pt) -- (389.957pt,403pt) -- (389.957pt,99.6166pt) -- (0pt,99.6166pt) -- (0pt,403pt);
\color[rgb]{1,1,1}
\fill(3.76406pt,399.236pt) -- (380.923pt,399.236pt) -- (380.923pt,253.19pt) -- (3.76406pt,253.19pt) -- (3.76406pt,399.236pt);
\fill(53.4497pt,394.719pt) -- (374.901pt,394.719pt) -- (374.901pt,289.325pt) -- (53.4497pt,289.325pt) -- (53.4497pt,394.719pt);
draw a white background that is larger than the actual picture. TikZ sees that and thinks it is part of the picture. Simply removing/uncommenting these lines removes most of the whitespace.
Near the end of the first scope,
\color[rgb]{1,1,1}
\fill(3.76406pt,249.426pt) -- (386.193pt,249.426pt) -- (386.193pt,103.381pt) -- (3.76406pt,103.381pt) -- (3.76406pt,249.426pt);
does the same.
Additionally (near the end of the second scope
),
\pgftext[center, base, at={\pgfpoint{220.95pt}{106.392pt}}]{\sffamily\fontsize{9}{0}\selectfont{\textbf{ }}}
adds a blank node below the picture, again enlarging the bounding box.
Removing all those lines gives a tight bounding box.
As far as I know, TikZ cannot do the cropping for you, as it can't know whether the white stuff is intentional or not (there might for example be a dark background behind the image so that white is visible).
It is not possible to fill a line, only an area. So here is a kind of crazy idea. Transform each "line" in your drawing into a rectangle wich can be filled at will.
The following code implements a macro called \tikzsegment
which uses a rectangular node to "draw" a line. It gets three parameters:
- Tikz styles for the node (here you can specify the pattern, fill color, etc. and the "minimum height" of the node, which will be the resulting "line" widht)
- Coordinates of the starting point (can be a named node)
- Coordinates of the ending point
Note that instead of the standard syntax \draw[styles] (a) -- (b)
, you have to use now \tikzsegment{styles}{a}{b}
, so you lose the ability of drawing several lines in a single path (and you cannot close the path). However you can combine your previous technique to draw the required path (and its borders), and apply later this technique to fill each segment, as I did in the following example:
\usetikzlibrary{calc}
\usetikzlibrary{patterns}
\def\tikzsegment#1#2#3{ % This is the macro explained above
\path let
\p1=($(#3)-(#2)$),
\n1={veclen(\p1)}
in (#2) -- (#3)
node[minimum width=\n1,
inner sep=0pt,
pos=0.5,sloped,rectangle,
#1]
(line){};
}
\begin{tikzpicture}
% This is your code untouched
\def\h{2}
\def\l{1}
\def\t{0.1} % thickness
\def\strokewidth{0.01cm}
\coordinate (A) at (0,0);
\coordinate (B) at ($(A) + (-30:\l)$);
\coordinate (C) at ($(B) + (0,-\h)$);
\coordinate (D) at ($(C) + (-150:\l)$);
\coordinate (E) at ($(D) + (150:\l)$);
\coordinate (F) at ($(E) + (0,\h)$);
\coordinate (A1) at ($(A) + (0,\h/2)$);
\coordinate (B1) at ($(B) + (30:\l/2)$);
\coordinate (C1) at ($(C) + (-30:\l/2)$);
\coordinate (D1) at ($(D) + (0,-\h/2)$);
\coordinate (E1) at ($(E) + (-150:\l/2)$);
\coordinate (F1) at ($(F) + (+150:\l/2)$);
\draw[line width=\strokewidth, double=none, double distance=\t*1cm-2*\strokewidth]
(A) -- (B) -- (C) -- (D) -- (E) -- (F) -- cycle
(A) -- (A1)
(B) -- (B1)
(C) -- (C1)
(D) -- (D1)
(E) -- (E1)
(F) -- (F1)
;
% This is the new code to fill each segment
\coordinate (prev) at (F);
\foreach \vertex in {A,B,C,D,E,F} {
\tikzsegment{pattern=crosshatch dots,
pattern color=blue,
minimum height=\t*1cm}
{prev}{\vertex};
\coordinate (prev) at (\vertex);
\tikzsegment{pattern=north west lines,
minimum height=\t*1cm}
{\vertex}{\vertex1};
}
\end{tikzpicture}
This is the result:
Best Answer
By use of
patterns.meta
library the result is far better than withpatterns
(but for pickpockets still not perfect):Ar least, code is much, much simpler: