我打算定义第一个圆设置在 x = 0 和 y = 0 处,以及其他依赖于它的圆。
我将必须多次改变这些圆的直径,因此这些圆的中心将会改变。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[very thick] (0mm,0mm) circle (10mm/2);
\draw[very thick] (20mm,0mm) circle (30mm/2);
\draw[very thick] (43mm,0mm) circle (25mm/2);
\draw[very thick] (43mm,0mm) circle (16mm/2);
\draw[very thick] (66.5mm,0mm) circle (22mm/2);
\end{tikzpicture}
\end{document}
优化此绘图的最佳方法是什么?
答案1
第一个版本
只是为了好玩。
\documentclass{standalone}
\usepackage{tikz}
\tikzset{
% gear with same center
gearbox/s/.code n args={3}{% prevdiameter, diameter, style
\path[#3] (c) circle (#2/2);
},
% gear on the right
gearbox/r/.code n args={3}{% prevdiameter, diameter, style
\path (c) ++({#1/2 + #2/2},0) coordinate (c);
\path[#3] (c) circle (#2/2);
},
% a styled chain of gears
gearbox/chain/.code 2 args={% start point, list of gears
\coordinate (c) at #1;
\foreach \diameter/\typeofgear/\mystyle
[remember=\diameter as \prevdiameter (initially 0)] in {#2}{
\tikzset{gearbox/\typeofgear={\prevdiameter}{\diameter}{\mystyle}}
}
},
}
\begin{document}
\begin{tikzpicture}
\tikzset{s1/.style={draw,thick},s2/.style={draw=red,thick},s3/.style={draw=blue,thick,dashed}}
\tikzset{
gearbox/chain={(0,0)}{10mm/s/s1,30mm/r/s1,16mm/r/s1,25mm/s/s1,22mm/r/s1},
gearbox/chain={(0,3cm)}{30mm/s/s2,9mm/r/s3,25mm/s/s1,13mm/r/s3,22mm/s/s1,30mm/r/s2},
}
\end{tikzpicture}
\end{document}
新版本
带有注释和图层的新版本。
\documentclass{standalone}
\usepackage{tikz}
\pgfdeclarelayer{a}
\pgfdeclarelayer{b}
\pgfsetlayers{b,a,main}
\usepackage{amssymb}
\tikzset{
% draw gear with diameter
gearbox/gear with diameter/.code n args={4}{% diameter, angle, style, layer
\pgfmathsetmacro{\r}{#1 mm/2};
\begin{pgfonlayer}{\layer}
\path[#3] (c) circle (\r pt);
\end{pgfonlayer}
\draw[gray!50!black,-latex] (c) +(#2:\r pt+1.5cm) -- node[pos=.2,above,sloped]{$\varnothing #1$} +(#2:\r pt);
\draw[gray!50!black] (c) +(#2:\r pt) -- +(180+#2:\r pt);
\draw[gray!50!black,-latex] (c) +(180+#2:\r pt+5mm) -- +(180+#2:\r pt);
\draw[gray!50,dashed] (c)
+(0:\r pt + 1cm) -- +(180:\r pt)
+(90:20mm) -- +(-90:20mm);
},
% gear with same center
gearbox/s/.code n args={4}{% prevdiameter, diameter, style, layer
\tikzset{gearbox/gear with diameter={#2}{110}{#3}{#4}}
},
% gear on the right
gearbox/r/.code n args={4}{% prevdiameter, diameter, style, layer
\pgfmathsetmacro{\gearpos}{\gearpos+#1/2+#2/2}
\global\let\gearpos\gearpos
\draw[-latex] ([yshift=-20mm]c) -- ++({#1 mm/2 + #2 mm/2},0);
\path (c) ++({#1 mm/2 + #2 mm/2},0) coordinate (c);
\tikzset{gearbox/gear with diameter={#2}{70}{#3}{#4}}
\path ([yshift=-20mm]c) node[below=1mm,fill=white,inner sep=.1em]{\gearpos};
},
% a styled chain of gears
gearbox/chain/.code 2 args={
\pgfmathsetmacro{\gearpos}{0};
\coordinate (c) at #1;
\foreach \diameter/\typeofgear/\mystyle/\layer
[remember=\diameter as \prevdiameter (initially 0)] in {#2}{
\tikzset{gearbox/\typeofgear={\prevdiameter}{\diameter}{\mystyle}{\layer}}
}
},
}
\begin{document}
\begin{tikzpicture}
\tikzset{
s1/.style={draw,thick,fill=white},
s2/.style={draw=red,thick,fill=red!10},
s3/.style={draw=blue,thick,fill=blue!10},
}
\tikzset{
gearbox/chain={(0,0)}{10/s/s1/a,30/r/s1/a,16/r/s1/a,25/s/s1/b,22/r/s1/a},
gearbox/chain={(0,6cm)}{30/s/s2/a,8/r/s2/a,25/s/s1/b,22/r/s1/b,13/s/s3/a,30/r/s3/a},
}
\end{tikzpicture}
\end{document}
答案2
您可以使用coordinates
圆心。定义这些时,您可以使用相当多的派生。这里我定义了一些可以更改的圆的直径。这不是完整的图景,但可能是一个好的开始。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[x=1mm,y=1mm,>=latex]
\newcommand\Di{10} %% Diamater of left circle
\newcommand\Dii{30} %% Diameter of next circle
\newcommand\Diiia{16} %% Diamater of inner circle
\newcommand\Diiib{25} %% Diameter of outer circle
\newcommand\Div{22} %% Diameter of right circle
\coordinate (C1) at (0,0);
\coordinate (C2) at (\Di/2+\Dii/2,0);
\coordinate (C3) at ($(C2)+(\Dii/2+\Diiia/2,0)$);
\coordinate (C4) at ($(C3)+(\Diiib/2+\Div/2,0)$);
%%
\draw[thick] (C1) circle (\Di/2);
\draw[thick] (C3) circle (\Diiia/2);
\draw[thick] (C3) circle (\Diiib/2);
\draw[fill=white,thick] (C2) circle (\Dii/2);
\draw[thick] (C4) circle (\Div/2);
%%
\draw[gray!70] ($(C1)+(0,-30)$) -- +(0,30+\Di/2+3) node[pos=0,left,rotate=90,black]{0};
\pgfmathparse{\Di/2+\Dii/2}\edef\res{\pgfmathresult}
\draw[gray!70] ($(C2)+(0,-30)$) -- +(0,30+\Dii/2+3) node[pos=0,left,rotate=90,black]{\res};
\pgfmathparse{\res+\Dii/2+\Diiia/2}\edef\res{\pgfmathresult}
\draw[gray!70] ($(C3)+(0,-30)$) -- +(0,30+\Diiib/2+3) node[pos=0,left,rotate=90,black]{\res};
\pgfmathparse{\res+\Diiib/2+\Div/2}\edef\res{\pgfmathresult}
\draw[gray!70] ($(C4)+(0,-30)$) -- +(0,30+\Div/2+3) node[pos=0,left,rotate=90,black]{\res};
\draw[gray!70] ($(C1)+(-\Di/2-3,0)$) -- ($(C4)+(\Div/2+3,0)$);
%%
\draw (C1 |- {(0,-28)}) circle (1pt);
\draw[->] (C1 |- {(0,-28)}) -- (C2 |- {(0,-28)});
\draw[->] (C2 |- {(0,-28)}) -- (C3 |- {(0,-28)});
\draw[->] (C3 |- {(0,-28)}) -- (C4 |- {(0,-28)});
\end{tikzpicture}
\end{document}
编辑
要添加直径负载\usepackage{wasysym}
并将以下内容附加到图片中:
\newcommand\DrawDiameter[3]{% {<center>}{<angle>}{<diameter>}
\ifnum#2<90\relax
\draw[->] (#1) +(#2:#3/2+15) -- +(#2:#3/2) node[anchor=south east,pos=0,rotate=#2]{\diameter#3};
\else
\draw[->] (#1) +(#2:#3/2+15) -- +(#2:#3/2) node[anchor=south west,pos=0,rotate=180+#2]{\diameter#3};
\fi
\draw[gray!70] (#1) +(#2:#3/2) -- +(180+#2:#3/2);
\draw[<-] (#1) ++(180+#2:#3/2) -- +(180+#2:5);
}
\DrawDiameter{C1}{110}{\Di}
\DrawDiameter{C2}{110}{\Dii}
\DrawDiameter{C3}{110}{\Diiia}
\DrawDiameter{C3}{70}{\Diiib}
\DrawDiameter{C4}{110}{\Div}