我有下面这张 Tikz 图片的代码。我怎样才能将定子图的上方灰色环夹住?我已经尝试过了,\clip (0,0,\H) circle (\r) circle (\R);
但结果不是我想要的:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
% isometric axes
\pgfmathsetmacro\xx{1/sqrt(2)}
\pgfmathsetmacro\xy{1/sqrt(6)}
\pgfmathsetmacro\zz{sqrt(2/3)}
\tikzset{isometric/.style={x={(-\xx cm,-\xy cm)},y={(\xx cm,-\xy cm)},z={(0cm,\zz cm)}}}
% parameters
\def\nc{24} % number of cogs (multiple of 4, at least 8)
\def\H {1} % total height
\def\h {0.25} % cog height
\def\R {2} % outer radius
\def\r {1.5} % inner radius
\def\upper {(0,0,\H) circle (\r);(0,0,\H) circle (\R);}
\pgfmathtruncatemacro\nw{\nc/4} % number of cog walls per quadrant
\pgfmathsetmacro\a {180/\nc} % step angle
\pgfmathsetmacro\is{135+2*\a} % inner part step
\pgfmathsetmacro\il{315-4*\a} % inner part last iteration
\pgfmathsetmacro\os{-45+2*\a} % outer part step
\pgfmathsetmacro\ol{135-4*\a} % outer part last iteration
\tikzset
{
inner/.style={fill=gray!30},
outer/.style={fill=gray!10},
cogs/.style= {fill=gray!50},
top/.style= {fill=gray!20},
cut/.style= {color=red!60, dashed},
}
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=1,xscale=1, isometric,line join=round,line cap=round]
% inner cogs side walls
\foreach\i in{-\nw,...,\nw}
{
\pgfmathsetmacro\j{4*\nw-\i}
\pgfmathsetmacro\k{6*\nw+\i}
\draw[cogs] (\a*\j:\R) -- (\a*\j:\r) --++ (0,0,\h) --++ (\a*\j:\R-\r) -- cycle;
\draw[cogs] (\a*\k:\R) -- (\a*\k:\r) --++ (0,0,\h) --++ (\a*\k:\R-\r) -- cycle;
}
% inner part
\draw[inner] (135:\r) foreach\i in {135,\is,...,\il}
{arc (\i:\i+\a:\r) --++ (0,0,\h) arc (\i+\a:\i+2*\a:\r) --++ (0,0,-\h)}
arc (315-2*\a:315-\a:\r) --++ (0,0,\h) arc (315-\a:315:\r) --++ (0,0, \H-\h)
arc (315:135:\r) -- cycle;
% outer cogs side walls
\foreach\i in{-\nw,...,\nw}
{
\pgfmathsetmacro\j{-\i+2*\nw}
\draw[cogs] (\a*\i:\R) -- (\a*\i:\r) --++ (0,0,\h) --++ (\a*\i:\R-\r) -- cycle;
\draw[cogs] (\a*\j:\R) -- (\a*\j:\r) --++ (0,0,\h) --++ (\a*\j:\R-\r) -- cycle;
}
% outer part
\draw[outer] (-45:\R) foreach\i in {-45,\os,...,\ol}
{arc (\i:\i+\a:\R) --++ (0,0,\h) arc (\i+\a:\i+2*\a:\R) --++ (0,0,-\h)}
arc (135-2*\a:135-\a:\R) --++ (0,0,\h) arc (135-\a:135:\R) --++ (0,0,\H-\h)
arc (135:-45:\R) -- cycle;
% top part
\draw[even odd rule,top] (0,0,\H) circle (\R) (0,0,\H) circle (\r);
\foreach \i in {3,5,7,9,11,13,15,17}
\draw[even odd rule,cut] ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},0.25) -- ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},\H);
\foreach \i in {4,6,8,10,12,14,16}
\draw[even odd rule,cut] ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},0) -- ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},\H);
\foreach \i in {27,29,31,33,35,37}
\draw[even odd rule,cut] ({\r*cos(((\i *\a)+((\i+1)*\a))/2)},{\r*sin(((\i *\a)+((\i+1)*\a))/2)},0.25) -- ({\r*cos(((\i *\a)+((\i+1)*\a))/2)},{\r*sin(((\i *\a)+((\i+1)*\a))/2)},\H);
\foreach \i in {28,30,32,34,36,38}
\draw[even odd rule,cut] ({\r*cos(((\i *\a)+((\i+1)*\a))/2)},{\r*sin(((\i *\a)+((\i+1)*\a))/2)},0) -- ({\r*cos(((\i *\a)+((\i+1)*\a))/2)},{\r*sin(((\i *\a)+((\i+1)*\a))/2)},\H);
\begin{scope}
\clip (0,0,\H) circle (\r) circle (\R);
\foreach \i in {3,5,7,9,11,13,15,17}
\draw[even odd rule,cut] ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},\H) -- ({\R*cos(-(((\i *\a)+((\i+1)*\a))/2))},{\R*sin(-(((\i *\a)+((\i+1)*\a))/2))},\H);
\foreach \i in {4,6,8,10,12,14,16,18,19,20,21}
\draw[even odd rule,cut] ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},\H) -- ({\R*cos(-(((\i *\a))+((\i+1)*\a))/2)},{\R*sin(-(((\i *\a)+((\i+1)*\a)))/2)},\H);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
使用:
\clip (0,0,\H) circle[radius=\R] (0,0,\H) circle[x radius=-\r, y radius=\r];
要理解这一点,请参阅: 反转 TikZ 描边方向以与填充规则一起使用
结果:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
% isometric axes
\pgfmathsetmacro\xx{1/sqrt(2)}
\pgfmathsetmacro\xy{1/sqrt(6)}
\pgfmathsetmacro\zz{sqrt(2/3)}
\tikzset{isometric/.style={x={(-\xx cm,-\xy cm)},y={(\xx cm,-\xy cm)},z={(0cm,\zz cm)}}}
% parameters
\def\nc{24} % number of cogs (multiple of 4, at least 8)
\def\H {1} % total height
\def\h {0.25} % cog height
\def\R {2} % outer radius
\def\r {1.5} % inner radius
\def\upper {(0,0,\H) circle (\r);(0,0,\H) circle (\R);}
\pgfmathtruncatemacro\nw{\nc/4} % number of cog walls per quadrant
\pgfmathsetmacro\a {180/\nc} % step angle
\pgfmathsetmacro\is{135+2*\a} % inner part step
\pgfmathsetmacro\il{315-4*\a} % inner part last iteration
\pgfmathsetmacro\os{-45+2*\a} % outer part step
\pgfmathsetmacro\ol{135-4*\a} % outer part last iteration
\tikzset
{
inner/.style={fill=gray!30},
outer/.style={fill=gray!10},
cogs/.style= {fill=gray!50},
top/.style= {fill=gray!20},
cut/.style= {color=red!60, dashed},
}
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=1,xscale=1, isometric,line join=round,line cap=round]
% inner cogs side walls
\foreach\i in{-\nw,...,\nw}
{
\pgfmathsetmacro\j{4*\nw-\i}
\pgfmathsetmacro\k{6*\nw+\i}
\draw[cogs] (\a*\j:\R) -- (\a*\j:\r) --++ (0,0,\h) --++ (\a*\j:\R-\r) -- cycle;
\draw[cogs] (\a*\k:\R) -- (\a*\k:\r) --++ (0,0,\h) --++ (\a*\k:\R-\r) -- cycle;
}
% inner part
\draw[inner] (135:\r) foreach\i in {135,\is,...,\il}
{arc (\i:\i+\a:\r) --++ (0,0,\h) arc (\i+\a:\i+2*\a:\r) --++ (0,0,-\h)}
arc (315-2*\a:315-\a:\r) --++ (0,0,\h) arc (315-\a:315:\r) --++ (0,0, \H-\h)
arc (315:135:\r) -- cycle;
% outer cogs side walls
\foreach\i in{-\nw,...,\nw}
{
\pgfmathsetmacro\j{-\i+2*\nw}
\draw[cogs] (\a*\i:\R) -- (\a*\i:\r) --++ (0,0,\h) --++ (\a*\i:\R-\r) -- cycle;
\draw[cogs] (\a*\j:\R) -- (\a*\j:\r) --++ (0,0,\h) --++ (\a*\j:\R-\r) -- cycle;
}
% outer part
\draw[outer] (-45:\R) foreach\i in {-45,\os,...,\ol}
{arc (\i:\i+\a:\R) --++ (0,0,\h) arc (\i+\a:\i+2*\a:\R) --++ (0,0,-\h)}
arc (135-2*\a:135-\a:\R) --++ (0,0,\h) arc (135-\a:135:\R) --++ (0,0,\H-\h)
arc (135:-45:\R) -- cycle;
% top part
\draw[even odd rule,top] (0,0,\H) circle (\R) (0,0,\H) circle (\r);
\foreach \i in {3,5,7,9,11,13,15,17}
\draw[even odd rule,cut] ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},0.25) -- ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},\H);
\foreach \i in {4,6,8,10,12,14,16}
\draw[even odd rule,cut] ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},0) -- ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},\H);
\foreach \i in {27,29,31,33,35,37}
\draw[even odd rule,cut] ({\r*cos(((\i *\a)+((\i+1)*\a))/2)},{\r*sin(((\i *\a)+((\i+1)*\a))/2)},0.25) -- ({\r*cos(((\i *\a)+((\i+1)*\a))/2)},{\r*sin(((\i *\a)+((\i+1)*\a))/2)},\H);
\foreach \i in {28,30,32,34,36,38}
\draw[even odd rule,cut] ({\r*cos(((\i *\a)+((\i+1)*\a))/2)},{\r*sin(((\i *\a)+((\i+1)*\a))/2)},0) -- ({\r*cos(((\i *\a)+((\i+1)*\a))/2)},{\r*sin(((\i *\a)+((\i+1)*\a))/2)},\H);
\begin{scope}
\clip (0,0,\H) circle[radius=\R] (0,0,\H) circle[x radius=-\r, y radius=\r];
\foreach \i in {3,5,7,9,11,13,15,17}
\draw[even odd rule,cut] ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},\H) -- ({\R*cos(-(((\i *\a)+((\i+1)*\a))/2))},{\R*sin(-(((\i *\a)+((\i+1)*\a))/2))},\H);
\foreach \i in {4,6,8,10,12,14,16,18,19,20,21}
\draw[even odd rule,cut] ({\R*cos(((\i *\a)+((\i+1)*\a))/2)},{\R*sin(((\i *\a)+((\i+1)*\a))/2)},\H) -- ({\R*cos(-(((\i *\a))+((\i+1)*\a))/2)},{\R*sin(-(((\i *\a)+((\i+1)*\a)))/2)},\H);
\end{scope}
\end{tikzpicture}
\end{document}