如何用 TikZ 改进重复图案的创作?

如何用 TikZ 改进重复图案的创作?

我是 LaTex 的初学者,正在尝试使用 TikZ 为工业文档绘制一系列 RF 电缆。在下面的 MWE 中,我找不到一种方法来防止编织线重叠,同时保持螺纹线的对齐。另外,我想知道代码是否可以改进,因为我有很多不同的电线要处理,有些带有双编织线等......

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows,backgrounds}
\begin{document}
\begin{tikzpicture}[ >=latex',auto,inner sep=2mm,node distance=4cm and 6cm]
\tikzset{box1/.style={draw,minimum width=2cm,rectangle, rounded corners =1 pt,  line width=2pt}}

\node[box1, text width = 2 cm,text height= 1 cm] (jacket) {};
\node[box1,  text width = 2cm,text height= 0.8 cm, right=- 2pt of jacket] (braid)  {};
\node[box1, text width = 1.5cm,text height= 0.5 cm, right=-2pt of braid] (insul)  {};

\node[anchor=center] (A) at (0, -0.9) {};
\node[anchor=center] (B) at (0.2,  0.9) {};
\draw[anchor=center] (A) .. controls (-0.2, -0.2) and (0.2, 0.2) .. (B);
\node[anchor=center] (C) at (0.1, -0.9) {};
\node[anchor=center] (D) at (0.3,  0.9) {};
\draw[anchor=center] (C) .. controls (-0.1, -0.2) and (0.3, 0.2) .. (D);

\clip (1.2, -0.6) rectangle (3.6,0.6); %clipping braided area

\foreach \x in {1,2,...,8} %starting drawing patterns for braid
 \draw [anchor=center, xshift=1.5cm, yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8}
\draw  [anchor=center,  xshift=2.1cm,  yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=2.7cm,  yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=3.3cm, yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=3.9cm,  yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);

\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 0.9cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 1.5cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 2.1cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 2.7cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 3.3cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);

\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=1.6cm, yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8}
\draw  [anchor=center,  xshift=2.2cm,  yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=2.8cm,  yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=3.4cm, yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=4cm,  yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);

\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=0.9 cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 1.5cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 2.1cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 2.7cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 3.3cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

怎么样:您定义两个点和拼花图案的尺寸(以厘米为单位),您将得到一个由两个点定义的矩形,其中填充了图案。要将图案限制为非矩形形状,您可以使用命令clip,要保持裁剪局部,您可以将整个构造放在scope

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{xifthen}

\newdimen\xfirst
\newdimen\yfirst
\newdimen\xsecond
\newdimen\ysecond

\newcommand{\ybig}{}
\newcommand{\ysmall}{}
\newcommand{\xbig}{}
\newcommand{\xsmall}{}

\newcommand{\parquet}[3]% first point, second point, element size in cm
{   
\begin{scope}
    \path (#1);
  \pgfgetlastxy{\xfirst}{\yfirst};
  \path (#2);
  \pgfgetlastxy{\xsecond}{\ysecond};
  \ifthenelse{\lengthtest{\yfirst > \ysecond}}%
  { \xdef\ybig{\yfirst}
    \xdef\ysmall{\ysecond}
  }%
  { \xdef\ybig{\ysecond}
    \xdef\ysmall{\yfirst}
  }
  \ifthenelse{\lengthtest{\xfirst > \xsecond}}%
  { \xdef\xbig{\xfirst}
    \xdef\xsmall{\xsecond}
  }%
  { \xdef\xbig{\xsecond}
    \xdef\xsmall{\xfirst}
  }
  \clip (\xsmall,\ysmall) rectangle (\xbig,\ybig);
  \pgfmathtruncatemacro{\xsteps}{(\xbig-\xsmall)/28.452756/#3+0.51}
  \pgfmathtruncatemacro{\ysteps}{(\ybig-\ysmall)/28.452756/#3+0.51}
  \pgfmathsetmacro{\xsmallll}{\xsmall/28.452756}
  \pgfmathsetmacro{\ysmallll}{\ysmall/28.452756}
  %\node at (0,0) {\xsteps, \ysteps};
  %\node at (0,1) {\xsmall, \ysmall};
  %\node at (0,2) {\xbig, \ybig};
  \foreach \x  in {0,...,\xsteps}
  { \foreach \y  in {0,...,\ysteps}
    {   \foreach \z in {0,...,4}
        {   \draw (\xsmallll+\x*#3-\z*#3/8,{\ysmallll+\y*#3-(4-\z)*#3/8}) -- ({\xsmallll+\x*#3+(4-\z)*#3/8},\ysmallll+\y*#3+\z*#3/8);
            \draw ({\xsmallll+(\x+0.5)*#3+\z*#3/8},\ysmallll+\y*#3+\z*#3/8) -- (\xsmallll+\x*#3+\z*#3/8,{\ysmallll+(\y+0.5)*#3+\z*#3/8});
        }
    }
  }
\end{scope}  
}

\begin{document}

\begin{tikzpicture}
    \clip[draw] (0,0) -- (1,2) -- (4,3) -- (3,1) -- (0,0);
    \parquet{0,0}{4,3}{0.5}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{scope}
        \clip[draw] (0,0) arc (180:360:3) arc (0:180:2) arc (0:180:1);
        \parquet{0,-3}{6,2}{1}
    \end{scope}
    \begin{scope}
        \clip[draw] (4,-1) circle (3 and 1);
        \parquet{1,-2}{7,0}{0.5}
    \end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述


编辑1:我添加了另一个参数来控制每个矩形被划分成多少个“条纹”:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{xifthen}

\newdimen\xfirst
\newdimen\yfirst
\newdimen\xsecond
\newdimen\ysecond

\newcommand{\ybig}{}
\newcommand{\ysmall}{}
\newcommand{\xbig}{}
\newcommand{\xsmall}{}

\newcommand{\parquet}[4]% first point, second point, element size in cm, numer of subdivisions
{   
\begin{scope}
    \path (#1);
  \pgfgetlastxy{\xfirst}{\yfirst};
  \path (#2);
  \pgfgetlastxy{\xsecond}{\ysecond};
  \ifthenelse{\lengthtest{\yfirst > \ysecond}}%
  { \xdef\ybig{\yfirst}
    \xdef\ysmall{\ysecond}
  }%
  { \xdef\ybig{\ysecond}
    \xdef\ysmall{\yfirst}
  }
  \ifthenelse{\lengthtest{\xfirst > \xsecond}}%
  { \xdef\xbig{\xfirst}
    \xdef\xsmall{\xsecond}
  }%
  { \xdef\xbig{\xsecond}
    \xdef\xsmall{\xfirst}
  }
  \clip (\xsmall,\ysmall) rectangle (\xbig,\ybig);
  \pgfmathtruncatemacro{\xsteps}{(\xbig-\xsmall)/28.452756/#3+0.51}
  \pgfmathtruncatemacro{\ysteps}{(\ybig-\ysmall)/28.452756/#3+0.51}
  \pgfmathsetmacro{\xsmallll}{\xsmall/28.452756}
  \pgfmathsetmacro{\ysmallll}{\ysmall/28.452756}
  \foreach \x  in {0,...,\xsteps}
  { \foreach \y  in {0,...,\ysteps}
    {   \foreach \z in {0,...,#4}
        {   \draw (\xsmallll+\x*#3-\z*#3/2/#4,{\ysmallll+\y*#3-(#4-\z)*#3/2/#4}) -- ({\xsmallll+\x*#3+(#4-\z)*#3/2/#4},\ysmallll+\y*#3+\z*#3/2/#4);
            \draw ({\xsmallll+(\x+0.5)*#3+\z*#3/2/#4},\ysmallll+\y*#3+\z*#3/2/#4) -- (\xsmallll+\x*#3+\z*#3/2/#4,{\ysmallll+(\y+0.5)*#3+\z*#3/2/#4});
        }
    }
  }
\end{scope}  
}

\begin{document}

\begin{tikzpicture}
    \clip[draw] (0,0) -- (3,1) -- (2,4) -- (-1,3) -- (0,0);
    \parquet{-1,0}{3,4}{1}{4}
\end{tikzpicture}
\begin{tikzpicture}
    \clip[draw] (0,0) -- (3,1) -- (2,4) -- (-1,3) -- (0,0);
    \parquet{-1,0}{3,4}{1}{2}
\end{tikzpicture}

\begin{tikzpicture}
    \clip[draw] (0,0) -- (3,1) -- (2,4) -- (-1,3) -- (0,0);
    \parquet{-1,0}{3,4}{1}{8}
\end{tikzpicture}
\begin{tikzpicture}
    \clip[draw] (0,0) -- (3,1) -- (2,4) -- (-1,3) -- (0,0);
    \parquet{-1,0}{3,4}{1}{6}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

虽然这不是对你问题的直接回答,但我以前也画过类似的电缆。这是我的解决方案:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{scopes,fadings,patterns}

\begin{tikzpicture}[x=4mm,>=latex]
  \def\cilindro#1#2#3#4{%
  % #1 Diametro
  % #2 Longitud
  % #3 color
  % #4 patron
  \draw[fill=#3] (#2,0) circle(#1/2);
  \draw[#4] (#2,0) circle(#1/2);
  \fill[#3] (#2,#1/2) rectangle (0,-#1/2);
  \fill[#4] (#2,#1/2) rectangle (0,-#1/2);
  \draw[fill=#3] (0,0) circle(#1/2);
  \draw (0,#1/2) --  +(#2,0)
        (0,-#1/2) -- +(#2,0);
  }
  \cilindro{1}{7}{black!20}{shade,shading angle=180, middle color=black!10}
  {[xshift=-.5cm] \cilindro{.8}{1.25}{white}{pattern=crosshatch}}
  {[xshift=-1cm] \cilindro{.7}{1.25}{white}{fill=none}}
  {[xshift=-2cm] \cilindro{.2}{2.5}{black!60}{shade,
     bottom color=black!10,middle color=black!80, 
     shading angle=180}}
  \draw[<-] (-1.5cm,.1) -- +(-2,2) node[above] {\small N\'ucleo de cobre};
  \draw[<-] (-.7cm,0) -- +(-.5,1) node[above] {\small Aislante};
  \draw[<-] (-.1cm,0) -- +(.5,2) node[above] {\small Malla};
  \draw[<-] (1.5cm,0) -- +(1,1) node[above] {\small Funda protectora};
\end{tikzpicture}
\end{document}

结果:

电缆

无可否认,辫子线过于简单。我认为处理它的正确方法是定义一个新的模式,并用它来代替crosshatch

相关内容