임의의 모양에 따라 노드를 포함한 배경을 자르는 방법은 무엇입니까?

임의의 모양에 따라 노드를 포함한 배경을 자르는 방법은 무엇입니까?

다음과 같은WE

\documentclass[border=10pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes, positioning, fit, backgrounds}

\tikzstyle{backA}=[rectangle,
fill=blue!30,
inner sep=0.2cm,
rounded corners=0mm]

\tikzstyle{backB}=[rectangle,
fill=purple!15,
inner sep=0.2cm,
rounded corners=0mm]

\tikzstyle{backC}=[rectangle,
fill=yellow!40,
inner sep=0.2cm,
rounded corners=0mm]

\tikzset{%
  >={Latex[width=2mm,length=2mm]},
            base/.style = {rectangle, rounded corners, draw=black,
                           minimum width=1cm, minimum height=1cm,
                           text centered,inner sep=0.3cm},
  operation/.style = {base, fill=SkyBlue},
}

\begin{document}    
\begin{tikzpicture}[node distance=0.8cm,
    every node/.style={fill=white}, align=center]
    \node (controller) [operation] {Microcontroller};
    \node (regulator) [operation, below = of controller] {Regulator};
    \node (transceiver) [operation, right = of controller, align = center] {CAN \\ Transceiver};
    \node (sensor) [operation, above = of controller] {Sensor};
    \node (flash) [operation, below = of transceiver, yshift=4mm] {Flash \\ Memeory};
    \node (driver1) [operation, right = of sensor] {Driver 1};
    \node (driver2) [operation, left = of sensor] {Driver 2};
    \node (power) [operation, left = of regulator, align=center] {Input \\ Power};
    \node (motor1) [operation, above = of sensor, align=center, xshift=1cm] {Motor 1};
    \node (motor2) [operation, above = of sensor, align=center, xshift=-1cm] {Motor 2};
    \node[circle,draw,fill=SkyBlue] (computer) [right = of driver1] {Computer};
    \coordinate[left = of power] (d1) {};
    \coordinate[above = of d1, yshift=5.5cm] (d2) {};

    \draw[->]             (controller) -- (transceiver);
    \draw[<->]             (controller) -- (sensor);
    \draw[->]             (driver1) -- (motor1);
    \draw[->]             (driver2) -- (motor2);
    \draw[<->]             (sensor) -- (motor2);
    \draw[<->]             (sensor) -- (motor1);
    \draw[->]             (controller) -- (driver1);
    \draw[->]             (controller) -- (driver2);
    \draw[->]             (controller) -- (flash);
    \draw[->]             (regulator) -- (controller);
    \draw[->]             (power) -- (regulator);
    \draw[<->]             (transceiver) -- (computer);
    \draw[->]             (power) -- (d1) |- (motor2);
    \draw[->]             (power) -- (d1) --  (d2) -| (motor1);

    \begin{pgfonlayer}{background}
    \node [backC,
    fit=(driver1) (driver2) (sensor) (motor1) (motor2),
    label=above:{}] {};

    \node [backA,
    fit=(computer) (transceiver),
    label=above:{}] {};

    \node [backB,
    fit=(regulator) (power),
    label=above:{}] {};
    \end{pgfonlayer}
  \end{tikzpicture}
\end{document}

수확량

여기에 이미지 설명을 입력하세요

노드 driver1는 노란색 배경으로만 덮여 있어야 하므로 노란색 배경을 방해하는 보라색 배경의 특정 부분을 빼야 합니다. 특히 보라색 배경에 허용되는 경계는 대략 다음과 같습니다.

여기에 이미지 설명을 입력하세요

어떻게 하면 그런 것을 성취할 수 있나요?

답변1

나는 흰색으로 영역을 과도하게 그리지 않을 것입니다. 유지하고 싶은 배경이 있다고 상상해보십시오. 그리고 \tikzstyle더 이상 사용되지 않습니다.

\documentclass[border=10pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes, positioning, fit, backgrounds}

% based on https://tex.stackexchange.com/a/12033/121799
\tikzset{reverseclip/.style={insert path={(current bounding box.south west)rectangle 
  (current bounding box.north east)} }}

\tikzset{backA/.style={rectangle,
fill=blue!30,
inner sep=0.2cm,
rounded corners=0mm},
backB/.style={rectangle,
fill=purple!15,
inner sep=0.2cm,
rounded corners=0mm},
backC/.style={rectangle,
fill=yellow!40,
inner sep=0.2cm,
rounded corners=0mm}}

\tikzset{%
  >={Latex[width=2mm,length=2mm]},
            base/.style = {rectangle, rounded corners, draw=black,
                           minimum width=1cm, minimum height=1cm,
                           text centered,inner sep=0.3cm},
  operation/.style = {base, fill=SkyBlue},
}

\begin{document}    
\begin{tikzpicture}[node distance=0.8cm,
    every node/.style={fill=white}, align=center]
    \node (controller) [operation] {Microcontroller};
    \node (regulator) [operation, below = of controller] {Regulator};
    \node (transceiver) [operation, right = of controller, align = center] {CAN \\ Transceiver};
    \node (sensor) [operation, above = of controller] {Sensor};
    \node (flash) [operation, below = of transceiver, yshift=4mm] {Flash \\ Memeory};
    \node (driver1) [operation, right = of sensor] {Driver 1};
    \node (driver2) [operation, left = of sensor] {Driver 2};
    \node (power) [operation, left = of regulator, align=center] {Input \\ Power};
    \node (motor1) [operation, above = of sensor, align=center, xshift=1cm] {Motor 1};
    \node (motor2) [operation, above = of sensor, align=center, xshift=-1cm] {Motor 2};
    \node[circle,draw,fill=SkyBlue] (computer) [right = of driver1] {Computer};
    \coordinate[left = of power] (d1) {};
    \coordinate[above = of d1, yshift=5.5cm] (d2) {};

    \draw[->]             (controller) -- (transceiver);
    \draw[<->]             (controller) -- (sensor);
    \draw[->]             (driver1) -- (motor1);
    \draw[->]             (driver2) -- (motor2);
    \draw[<->]             (sensor) -- (motor2);
    \draw[<->]             (sensor) -- (motor1);
    \draw[->]             (controller) -- (driver1);
    \draw[->]             (controller) -- (driver2);
    \draw[->]             (controller) -- (flash);
    \draw[->]             (regulator) -- (controller);
    \draw[->]             (power) -- (regulator);
    \draw[<->]             (transceiver) -- (computer);
    \draw[->]             (power) -- (d1) |- (motor2);
    \draw[->]             (power) -- (d1) --  (d2) -| (motor1);

    \begin{pgfonlayer}{background}
    \node [backC,
    fit=(driver1) (driver2) (sensor) (motor1) (motor2),
    label=above:{}] (F1){};

    \node [backB,
    fit=(regulator) (power),
    label=above:{}] {};
    \clip ([xshift=-5pt,yshift=-5pt]F1.south west) -|
    ([xshift=5pt,yshift=5pt]F1.north east) -| cycle  [reverseclip];
    \node [backA,
    fit=(computer) (transceiver),
    label=above:{}] {};
    \end{pgfonlayer}
  \end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

답변2

이와 같이?

스크린샷

\documentclass[border=10pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes, positioning, fit, backgrounds}
\pgfdeclarelayer{background}
\pgfdeclarelayer{middle}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,middle,foreground}
\tikzstyle{backA}=[rectangle,
fill=blue!30,
inner sep=0.2cm,
rounded corners=0mm]

\tikzstyle{backB}=[rectangle,
fill=purple!15,
inner sep=0.2cm,
rounded corners=0mm]

\tikzstyle{backC}=[rectangle,
fill=yellow!40,
%inner sep=0.2cm,
rounded corners=0mm]

\tikzset{%
  >={Latex[width=2mm,length=2mm]},
            base/.style = {rectangle, rounded corners, draw=black,
                           minimum width=1cm, minimum height=1cm,
                           text centered,inner sep=0.3cm},
  operation/.style = {base, fill=SkyBlue},
}

\begin{document}    
\begin{tikzpicture}[node distance=0.8cm,
    every node/.style={fill=white}, align=center]
    \begin{pgfonlayer}{foreground}
    \node (controller) [operation] {Microcontroller};
    \node (regulator) [operation, below = of controller] {Regulator};
    \node (transceiver) [operation, right = of controller, align = center] {CAN \\ Transceiver};
    \node (sensor) [operation, above = of controller] {Sensor};
    \node (flash) [operation, below = of transceiver, yshift=4mm] {Flash \\ Memeory};
    \node (driver1) [operation, right = of sensor] {Driver 1};
    \node (driver2) [operation, left = of sensor] {Driver 2};
    \node (power) [operation, left = of regulator, align=center] {Input \\ Power};
    \node (motor1) [operation, above = of sensor, align=center, xshift=1cm] {Motor 1};
    \node (motor2) [operation, above = of sensor, align=center, xshift=-1cm] {Motor 2};
    \node[circle,draw,fill=SkyBlue] (computer) [right = of driver1] {Computer};
    \coordinate[left = of power] (d1) {};
    \coordinate[above = of d1, yshift=5.5cm] (d2) {};

    \draw[->]             (controller) -- (transceiver);
    \draw[<->]             (controller) -- (sensor);
    \draw[->]             (driver1) -- (motor1);
    \draw[->]             (driver2) -- (motor2);
    \draw[<->]             (sensor) -- (motor2);
    \draw[<->]             (sensor) -- (motor1);
    \draw[->]             (controller) -- (driver1);
    \draw[->]             (controller) -- (driver2);
    \draw[->]             (controller) -- (flash);
    \draw[->]             (regulator) -- (controller);
    \draw[->]             (power) -- (regulator);
    \draw[<->]             (transceiver) -- (computer);
    \draw[->]             (power) -- (d1) |- (motor2);
    \draw[->]             (power) -- (d1) --  (d2) -| (motor1);
\end{pgfonlayer}
    \begin{pgfonlayer}{middle}
    \node [backC,
    fit=(driver1) (driver2) (sensor) (motor1) (motor2),
    label=above:{}] {};
\end{pgfonlayer}
    \begin{pgfonlayer}{main}
    \node [fill=white,inner sep=3mm,
    fit=(driver1) (driver2) (sensor) (motor1) (motor2),
    label=above:{}] {};
\end{pgfonlayer}
\begin{pgfonlayer}{background}
    \node [backA,
    fit=(computer) (transceiver),
    label=above:{}] {};
\end{pgfonlayer}
    \node [backB,
    fit=(regulator) (power),
    label=above:{}] {};
  \end{tikzpicture}
\end{document}

답변3

노드가 아닌 임의의 모양의 경우 피팅을 사용할 수 없습니다. 데모

\documentclass[border=10pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes, positioning, calc, backgrounds}

\tikzset{%
  >={Latex[width=2mm,length=2mm]},
            base/.style = {rectangle, rounded corners, draw=black,
                           minimum width=1cm, minimum height=1cm,
                           text centered,inner sep=0.3cm},
  operation/.style = {base, fill=SkyBlue},
}

\begin{document}    
\begin{tikzpicture}[node distance=0.8cm,
    every node/.style={fill=white}, align=center]
    \node (controller) [operation] {Microcontroller};
    \node (regulator) [operation, below = of controller] {Regulator};
    \node (transceiver) [operation, right = of controller, align = center] {CAN \\ Transceiver};
    \node (sensor) [operation, above = of controller] {Sensor};
    \node (flash) [operation, below = of transceiver, yshift=4mm] {Flash \\ Memeory};
    \node (driver1) [operation, right = of sensor] {Driver 1};
    \node (driver2) [operation, left = of sensor] {Driver 2};
    \node (power) [operation, left = of regulator, align=center] {Input \\ Power};
    \node (motor1) [operation, above = of sensor, align=center, xshift=1cm] {Motor 1};
    \node (motor2) [operation, above = of sensor, align=center, xshift=-1cm] {Motor 2};
    \node[circle,draw,fill=SkyBlue] (computer) [right = of driver1] {Computer};
    \coordinate[left = of power] (d1) {};
    \coordinate[above = of d1, yshift=5.5cm] (d2) {};

    \draw[->]             (controller) -- (transceiver);
    \draw[<->]             (controller) -- (sensor);
    \draw[->]             (driver1) -- (motor1);
    \draw[->]             (driver2) -- (motor2);
    \draw[<->]             (sensor) -- (motor2);
    \draw[<->]             (sensor) -- (motor1);
    \draw[->]             (controller) -- (driver1);
    \draw[->]             (controller) -- (driver2);
    \draw[->]             (controller) -- (flash);
    \draw[->]             (regulator) -- (controller);
    \draw[->]             (power) -- (regulator);
    \draw[<->]             (transceiver) -- (computer);
    \draw[->]             (power) -- (d1) |- (motor2);
    \draw[->]             (power) -- (d1) --  (d2) -| (motor1);

    \begin{pgfonlayer}{background}
    \path (driver1.east |- computer.north) ++ (0.2,0.2) coordinate(int1);
    \path (driver2.south -| transceiver.west) ++ (-0.2,-0.2) coordinate(int2);

    \fill[yellow!40] ($(driver2.south west)+(-0.2,-0.2)$) |- ($(motor2.north)+(0,0.2)$) -| (int1) -- (int2) -- cycle;
    \fill[blue!30] ($(transceiver.south west)+(-0.2,-0.2)$) -- (int2) -- (int1) -- 
      ($(computer.north)+(0,0.2)$) -| ($(computer.east)+(0.2,0)$) |- cycle;
    \fill[purple!15] ($(power.south west)+(-0.2,-0.2)$) |- ($(power.north)+(0,0.2)$) -| ($(regulator.east)+(0.2,0.2)$) |- cycle;
    \end{pgfonlayer}
  \end{tikzpicture}
\end{document}

관련 정보