如何用 TikZ 創造這個圖形?

如何用 TikZ 創造這個圖形?

我想創建一個如下所示的圖形:

在此輸入影像描述

下面是我的程式碼:

    \documentclass[11pt]{article}
    \usepackage{tikz}
    \usetikzlibrary{arrows}

    \newcommand{\psign}{+}
    \newcommand{\ppsign}{++}

    \begin{document}

    \noindent
    \begin{tikzpicture}
    \tikzstyle{my node} = [draw,rectangle,minimum width=2.2cm,minimum height=0.8cm, node distance=2cm]
    %\draw[black!20] (-0.5cm,-0.5cm) grid (14cm,7cm);
    \node [my node] (A) {GDI DDI};
    \node [my node] (B) [above of=A] {GDI};

    \begin{scope}[xshift=3cm]
    \node                       (C)     {Graphics Driver};
    \node [my node] (D) [above of=C]    {GDI\psign Flat API};
    \node [my node] (E) [above of=D]    {GDI\psign\ C\ppsign API};
    \end{scope}

    \begin{scope}[xshift=6cm]
    \node [my node] (F) {DX DDI};
    \node [my node] (G) [above of=F] {DXGI};
    \node [my node] (H) [above of=G] {Direct3D};
    \node [my node] (I) [above of=H,minimum width=4cm,right=0pt] {Direct2D};
    \node [my node] (J) [right of=I,right=0pt,node distance=3cm] {DirectWrite};
    \node [my node] (K) [right of=F, node distance=4cm] {Software Rasterizer};
    \end{scope}

    \begin{scope}[->,>=triangle 45,semithick]
    \path (B) edge (A);
    \path (E) edge (D);
    \path (H) edge (G);
    \path (G) edge (F);
    \path (J) edge (I);
    \end{scope}

    \draw [dashed] (-1.25,1cm) -- (14,1cm);
    \end{tikzpicture}

    \end{document}

答案1

在此輸入影像描述

\documentclass[11pt]{article}
\usepackage{tikz,xcolor}
\usetikzlibrary{arrows,calc,fit,backgrounds}

\definecolor{fcolor}{HTML}{F1F1F1}

\newcommand{\psign}{+}
\newcommand{\ppsign}{++}

\begin{document}

\noindent
\begin{tikzpicture}
\tikzstyle{my node} = [draw,rectangle,minimum width=2.2cm,minimum height=0.8cm, node distance=2cm]
%\draw[black!20] (-0.5cm,-0.5cm) grid (14cm,7cm);
\node [my node] (A) {GDI DDI};
\node [my node] (B) [above of=A] {GDI};

\begin{scope}[xshift=3cm]
\node                       (C)     {Graphics Driver};
\node [my node] (D) [above of=C]    {GDI\psign Flat API};
\node [my node] (E) [above of=D]    {GDI\psign\ C\ppsign API};
\end{scope}

\begin{scope}[xshift=6cm]
\node [my node] (F) {DX DDI};
\node [my node] (G) [fill=white,above of=F] {DXGI};
\node [my node] (H) [fill=white,above of=G] {Direct3D};
\node [my node] (I) [fill=white,above of=H,minimum width=4.5cm,right=0pt] {Direct2D};
\node [my node] (J) [fill=white,right of=I,right=0pt,node distance=3.5cm] {DirectWrite};
\node [my node] (K) [fill=white,right of=G, node distance=4cm] {Software Rasterizer};
\end{scope}

\begin{scope}[->,>=triangle 45,semithick]
\path (B) edge (A);
\path (E) edge (D);
\path (H) edge (G);
\path (G) edge (F);
\path (J) edge (I);
\path (I.south-|K) edge (K);
\path (I.south-|{$(H.north)!.5!(H.north east)$}) edge ($(H.north)!.5!(H.north east)$);
\end{scope}

\begin{scope}[on background layer]
\node[rectangle,fill=fcolor,fit=(G.south west) (J.north east)] {};
\end{scope}

\draw [dashed] (-1.25,1cm) -- (14,1cm);
\end{tikzpicture}

\end{document}

首先我修復了Software Rasterizer節點位置right of=G

\node [my node] (K) [right of=G, node distance=4cm] {Software Rasterizer};

然後,對於剩餘的兩個箭頭連接,我將edge這兩行添加到您的 s 中:

\path (I.south-|K) edge (K);
\path (I.south-|{$(H.north)!.5!(H.north east)$}) edge ($(H.north)!.5!(H.north east)$);

這裡,是節點中心正上方的節點上(I.south-|K)的點。與 的想法相同,但有點複雜。現在它是節點上位於節點北部和東北部之間的中點正上方的點。Direct2DSoftware Rasterizer(I.south-|{$(H.north)!.5!(H.north east)$})Direct2DDirect3D

突出顯示的五個區塊可以由庫獲取fit,但這次在範圍 ( on background layer) 內,以避免繪製節點兩次。我們選擇任兩個最外面的節點,例如G和 ,J並分別指定它們的最外面的角點(G.south west)(J.north east)

答案2

這就是你正在尋找的東西。請注意,fit在頂部繪製,如果您需要在下面使用全顏色,則可能需要繪製這些節點兩次(一次是為了獲取用於計算的節點fit,一次是為了將它們再次繪製到適合區域上,您可以只儲存在巨集中繪製程式碼並運行該巨集兩次)。

\documentclass[11pt]{article}
  \usepackage{tikz}
  \usetikzlibrary{arrows,calc}
  \usetikzlibrary{positioning,fit}
    \newcommand{\psign}{+}
    \newcommand{\ppsign}{++}

    \begin{document}

    \noindent
    \begin{tikzpicture}
    \tikzstyle{my node} = [draw,rectangle,minimum width=2.2cm,minimum height=0.8cm, node distance=2cm]
    %\draw[black!20] (-0.5cm,-0.5cm) grid (14cm,7cm);
    \node [my node] (A) {GDI DDI};
    \node [my node] (B) [above of=A] {GDI};

    \begin{scope}[xshift=3cm]
    \node                       (C)     {Graphics Driver};
    \node [my node] (D) [above of=C]    {GDI\psign Flat API};
    \node [my node] (E) [above of=D]    {GDI\psign\ C\ppsign API};
    \end{scope}

    \begin{scope}[xshift=6cm]
    \node [my node] (F) {DX DDI};
    \node [my node] (G) [above of=F] {DXGI};
    \node [my node] (H) [above of=G] {Direct3D};
    \node [my node] (I) [above of=H,minimum width=4cm,right=0pt] {Direct2D};
    \node [my node] (J) [right of=I,right=0pt,node distance=3cm] {DirectWrite};
    \node [my node] (K) [right of=G, node distance=4cm] {Software Rasterizer};
    \end{scope}


  \begin{scope}[ transform canvas={xshift=1cm},
    ->,>=triangle 45,semithick]
    \draw (I)  --  (I |- K.north);
  \end{scope}
  \begin{scope}[ transform canvas={xshift=-4em},
    ->,>=triangle 45,semithick]
    \draw (I)  --  (I |- H.north);
  \end{scope}

    \begin{scope}[->,>=triangle 45,semithick]
    \path (B) edge (A);
    \path (E) edge (D);
    \path (H) edge (G);
    \path (G) edge (F);
    \path (J) edge (I);
    \end{scope}

    \draw [dashed] (-1.25,1cm) -- (14,1cm);


    \node [fill=gray,opacity=0.2,dotted,fit = (H) (I) (J) (K) ] {};



    \end{tikzpicture}

    \end{document}

在此輸入影像描述

我假設您可以弄清楚如何在灰色區域上方添加圖例。

答案3

稍微修改了 ind 簡化了 @daleif 和 @AboAmmar 解決方案(在我看來它們幾乎相同:-)):

\documentclass[11pt, border=1mm,
               tikz,
               preview]{standalone}
\usetikzlibrary{arrows,calc,fit,backgrounds}
    \newcommand{\psign}{+}
    \newcommand{\ppsign}{++}

    \begin{document}
\begin{tikzpicture}[
    node distance = 2cm,
every node/.style = {draw, rectangle,
                    fill=white, % if you liked, otherwise should be omitted
                     minimum width=2.2cm, minimum height=0.8cm}
                    ]
\node  (A) {GDI DDI};
\node  (B) [above of=A] {GDI};

\begin{scope}[xshift=3cm]
\node  (C) [draw=none]     {Graphics Driver};
\node  (D) [above of=C]    {GDI\psign Flat API};
\node  (E) [above of=D]    {GDI\psign\ C\ppsign API};
\end{scope}

\begin{scope}[xshift=6cm]
\node  (F) {DX DDI};
\node  (G) [above of=F] {DXGI};
\node  (H) [above of=G] {Direct3D};
\node  (I) [above of=H,minimum width=4.5cm,right=0pt] {Direct2D};
\node  (J) [right of=I,right=0pt,node distance=3.5cm] {DirectWrite};
\node  (K) [right of=G, node distance=4cm] {Software Rasterizer};
\end{scope}

\coordinate (L) at ([xshift=7mm] H.north);
\path[-triangle 45,semithick]
    (B) edge (A)    (E) edge (D)    (H) edge (G)
    (G) edge (F)    (J) edge (I)    (I.south-|K) edge (K)
    (I.south -| L) edge (L)
    (I.south-|{$(H.north)!.5!(H.north east)$}) -- ($(H.north)!.5!(H.north east)$);

\scoped[on background layer]
    \node (M) [rectangle,fill=gray!30,fit=(G.south west) (J.north east)] {};

\coordinate (N) at ($(A.west)!0.5!(B.west)$);
\draw [dashed]  (N) -- (N -| M.east);
\end{tikzpicture}
        \end{document}

這段程式碼給出:

在此輸入影像描述

相關內容