我写了以下代码:
\begin{tikzpicture}[scale = 0.8]
% \foreach \x in {0,1,2,3}
% {
% \draw (2*\x,0)--(2*\x+2,0)--(2*\x,1)--cycle;
% }
% \foreach \x in {0,1,2}
% {
% \draw (2*\x,1)--(2*\x+2,1)--(2*\x,2)--cycle;
% }
% \foreach \x in {0,1}
% {
% \draw (2*\x,2)--(2*\x+2,2)--(2*\x,3)--cycle;
% }
% \draw (0,3)--(2,3)--(0,4)--cycle;
\foreach \x in {0,1,2,3}
{
\draw (2*\x+2,0)--(2*\x+2,1)--(2*\x,1)--cycle;
\fill[lightgray] (2*\x+2,0)--(2*\x+2,1)--(2*\x,1)--cycle;
}
\foreach \x in {0,1,2}
{
\draw (2*\x+2,1)--(2*\x+2,2)--(2*\x,2)--cycle;
\fill[lightgray] (2*\x+2,1)--(2*\x+2,2)--(2*\x,2)--cycle;
}
\foreach \x in {0,1}
{
\draw (2*\x+2,2)--(2*\x+2,3)--(2*\x,3)--cycle;
\fill[lightgray] (2*\x+2,2)--(2*\x+2,3)--(2*\x,3)--cycle;
}
\draw (2,3)--(2,4)--(0,4)--cycle;
\fill[lightgray] (2,3)--(2,4)--(0,4)--cycle;
\draw (0,0)--(10,0)--(0,5)--cycle;
\end{tikzpicture}
但我得到的是以下图像: 如红色圆圈所示,顶点从边缘伸出。我想知道如何解决这个问题。
提前致谢。
答案1
您可以剪辑或使用适当的line join
(和line cap
)。但是,这也告诉您,您的代码可能比它应该的要长。
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,
pics/triags/.style={code={
\pgfmathtruncatemacro{\ncorners}{#1/2-1/2}
\ifnum\ncorners>0
\draw[fill=gray!20] foreach \XX in {1,...,\ncorners}
{foreach \YY in {\XX,...,\ncorners}
{(\XX-1,\ncorners-\YY+1) -| (\XX,\ncorners-\YY) -- cycle}};
\fi
\pgfmathtruncatemacro{\ncorners}{#1/2}
\draw (0,\ncorners) |- (\ncorners,0) -- cycle;
}}]
\path[x=0.7cm,y=0.5cm] foreach \NN in {2,...,7}
{({int(\NN/2)*(int(\NN/2-1/2))+\NN-2},0) pic{triags=\NN}};
\end{tikzpicture}
\end{document}