根据另一个(小)矩形的大小来裁剪一个矩形

根据另一个(小)矩形的大小来裁剪一个矩形

我正在尝试在第二个矩形中按第三个矩形的尺寸打一个洞。 似乎\clip,正如手册中所述,正在剪切所有后续绘图。 有命令吗invert clip? 我对 还不熟悉tikz

基本上,我希望通过从第二个矩形中删除一部分来使第三个矩形具有白色背景。目前,我曾经 \path[fill=white]解决这个问题。

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
%\usepackage{tkz-euclide}
\usetikzlibrary{calc,shapes,decorations,decorations.text, mindmap,shadings,patterns,matrix,arrows.meta,intersections,automata,backgrounds,patterns.meta}


\begin{document}
%\begin{figure}[H]
%\centering

\begin{tikzpicture}[%
  coord/.style = {help lines, color=gray!40, dashed,xstep=0.2cm,ystep=0.2cm},%
  2ndRect/.style = {magenta, pattern=crosshatch, pattern color=magenta},%
  3rdRect/.style = {magenta, pattern=crosshatch dots, pattern color=blue},%
  insideArrow/.style = {black,{latex[scale=2]}-{latex[scale=2]}},%
  simpleArrow/.style = {black,{-latex}},%
  every node/.append style = {font =\bfseries\footnotesize},%
  % scale=.75,
  transform shape
  ]
  %
  % Draw HelpLines just to work faster
  %
   \draw[coord] (-2,-2) grid (20,10);
   \draw[<->,ultra thin] (0,0)--(20,0) node[right]{$x$};
   \draw[<->,ultra thin] (0,0)--(0,5) node[above]{$y$};.
   \foreach \x in {0,0.5,...,19}
     \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
   \foreach \y in {0,0.5,...,3}
     \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};

  %
  % Draw Rectangles
  %
  \draw (2,0.2) rectangle (18,2.4);                       % 1st rectangle
  \draw[2ndRect] (4.5,0.4) rectangle (15.5,2.2);          % 2nd rectangle
  \path[fill=white](6.5,0.6) rectangle (13.5,2);          % workaround 4 clipping background
  \draw[3rdRect] (6.5,0.6) rectangle (13.5,2);            % 3rd rectangle
  %\clip (6.5,0.6) rectangle (13.5,2);          % clipping background
  \node [draw, fill=white] at (7.8,1.3) {S/R1};                              % label
  \node [draw, fill=white]  at (12.3,1.3) {S/R1};                             % label
  \draw[insideArrow] (8.5,1.3) -- (11.6,1.3);             % arrow btwen small rectangles
  \node at (1,1.3) {S4};        % label
  \draw[simpleArrow] (1.4,1.3) -- (2.8,1.3);             % arrow
  \node at (3.25,1.3) {S3};     % label
  \draw[simpleArrow] (3.6,1.3) -- (5.2,1.3);             % arrow
  \node at (5.5,1.3) {S2};      % label
  \draw[simpleArrow] (5.8,1.3) -- (6.4,1.3);             % arrow
  \node at (19,1.3) {R4};       % label
  \draw[simpleArrow] (13.6,1.3) -- (14.2,1.3);           % arrow
  \node at (16.75,1.3) {R3};    % label
  \draw[simpleArrow] (15,1.3) -- (16.4,1.3);             % arrow
  \node at (14.5,1.3) {R2};     % label
  \draw[simpleArrow] (17.2,1.3) -- (18.6,1.3);           % arrow
   
\end{tikzpicture}
%\label{fig:tikz}
%\caption{diagrama}
%\end{figure}
\end{document}

答案1

您可以使用even odd rule来打孔。请参阅第 15.5.2 节手册

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
%\usepackage{tkz-euclide}
\usetikzlibrary{calc,shapes,decorations,decorations.text, mindmap,shadings,patterns,matrix,arrows.meta,intersections,automata,backgrounds,patterns.meta}


\begin{document}
%\begin{figure}[H]
%\centering

\begin{tikzpicture}[%
  coord/.style = {help lines, color=gray!40, dashed,xstep=0.2cm,ystep=0.2cm},%
  2ndRect/.style = {magenta, pattern=crosshatch, pattern color=magenta},%
  3rdRect/.style = {magenta, pattern=crosshatch dots, pattern color=blue},%
  insideArrow/.style = {black,{latex[scale=2]}-{latex[scale=2]}},%
  simpleArrow/.style = {black,{-latex}},%
  every node/.append style = {font =\bfseries\footnotesize},%
  % scale=.75,
  transform shape
  ]
  %
  % Draw HelpLines just to work faster
  %
   \draw[coord] (-2,-2) grid (20,10);
   \draw[<->,ultra thin] (0,0)--(20,0) node[right]{$x$};
   \draw[<->,ultra thin] (0,0)--(0,5) node[above]{$y$};.
   \foreach \x in {0,0.5,...,19}
     \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
   \foreach \y in {0,0.5,...,3}
     \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};

  %
  % Draw Rectangles
  %
  \draw (2,0.2) rectangle (18,2.4);                       % 1st rectangle
  \draw[2ndRect, even odd rule] (4.5,0.4) rectangle (15.5,2.2) (6.5,0.6) rectangle (13.5,2);          % 2nd rectangle
%  \path[fill=white](6.5,0.6) rectangle (13.5,2);          % workaround 4 clipping background
  \draw[3rdRect, even odd rule] (6.5,0.6) rectangle (13.5,2) (7.3,1.05) rectangle node[midway, black]{S/R1} (8.3,1.55) (11.8,1.05) rectangle node[midway, black]{S/R1} (12.8,1.55);            % 3rd rectangle
  %\clip (6.5,0.6) rectangle (13.5,2);          % clipping background
%  \node [draw, opacity=0.5] at (7.8,1.3) {S/R1};                              % label
%  \node [draw, opacity=.5]  at (12.3,1.3) {S/R1};                             % label
  \draw[insideArrow] (8.5,1.3) -- (11.6,1.3);             % arrow btwen small rectangles
  \node at (1,1.3) {S4};        % label
  \draw[simpleArrow] (1.4,1.3) -- (2.8,1.3);             % arrow
  \node at (3.25,1.3) {S3};     % label
  \draw[simpleArrow] (3.6,1.3) -- (5.2,1.3);             % arrow
  \node at (5.5,1.3) {S2};      % label
  \draw[simpleArrow] (5.8,1.3) -- (6.4,1.3);             % arrow
  \node at (19,1.3) {R4};       % label
  \draw[simpleArrow] (13.6,1.3) -- (14.2,1.3);           % arrow
  \node at (16.75,1.3) {R3};    % label
  \draw[simpleArrow] (15,1.3) -- (16.4,1.3);             % arrow
  \node at (14.5,1.3) {R2};     % label
  \draw[simpleArrow] (17.2,1.3) -- (18.6,1.3);           % arrow
   
\end{tikzpicture}
%\label{fig:tikz}
%\caption{diagrama}
%\end{figure}
\end{document}

在此处输入图片描述

相关内容