使用 ÌTikZ 繪製形狀

使用 ÌTikZ 繪製形狀

在此輸入影像描述

我畫過這張圖,但不是這樣的

\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{pgf,tikz}
\usepackage{mathrsfs}
\usepackage{xepersian}
\settextfont{Yas}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
%\definecolor{qqqqff}{rgb}{0.,0.,1.}
%\definecolor{zzttqq}{rgb}{0.6,0.2,0.}
%  \definecolor{myaqua}{rgb}{1,1,1}
\definecolor{qqqqff}{HTML}{21bdbe}
\definecolor{zzttqq}{HTML}{19bfe0}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\clip(3.2557266494472623,-1.6520962393119207) rectangle (12.29163742054479,5.813639873201507);
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612] (3.6,-1.2) -- (11.6,-1.2) -- (8.,5.8) -- cycle;
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612] (7.2,3.6) -- (7.2,4.4) -- (11.6,4.4) -- (11.6,3.6) -- cycle;
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612] (7.,3.) -- (7.,2.2) -- (11.6,2.2) -- (11.584597952190476,2.9736025163350672) -- cycle;
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612] (11.6,1.6) -- (11.6,0.8) -- (6.8,0.8) -- (6.8,1.6) -- cycle;
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612] (11.6,0.2) -- (11.6,-0.6) -- (6.6,-0.6) -- (6.6,0.2) -- cycle;
\fill[color=zzttqq,fill=zzttqq,fill opacity=0.10000000149011612] (7.972370141039504,5.756043406199211) -- (7.832933908904861,-1.2) -- (11.6,-1.2) -- cycle;
\draw [color=zzttqq] (3.6,-1.2)-- (11.6,-1.2);
\draw [color=zzttqq] (11.6,-1.2)-- (8.,5.8);
\draw [color=zzttqq] (8.,5.8)-- (3.6,-1.2);
\draw [color=zzttqq,line width=3pt] (7.2,3.6)-- (7.2,4.4);
\draw [color=zzttqq,line width=3pt] (7.2,4.4)-- (11.6,4.4);
\draw [color=zzttqq,line width=3pt] (11.6,4.4)-- (11.6,3.6);
\draw [color=zzttqq,line width=3pt] (11.6,3.6)-- (7.2,3.6);
\draw [color=zzttqq,line width=3pt] (7.,3.)-- (7.,2.2);
\draw [color=zzttqq,line width=3pt] (7.,2.2)-- (11.6,2.2);
\draw [color=zzttqq,line width=3pt] (11.6,2.2)-- (11.584597952190476,2.9736025163350672);
\draw [color=zzttqq,line width=3pt] (11.584597952190476,2.9736025163350672)-- (7.,3.);
\draw [color=zzttqq,line width=3pt] (11.6,1.6)-- (11.6,0.8);
\draw [color=zzttqq,line width=3pt] (11.6,0.8)-- (6.8,0.8);
\draw [color=zzttqq,line width=3pt] (6.8,0.8)-- (6.8,1.6);
\draw [color=zzttqq,line width=3pt] (6.8,1.6)-- (11.6,1.6);
\draw [color=zzttqq,line width=3pt] (11.6,0.2)-- (11.6,-0.6);
\draw [color=zzttqq,line width=3pt] (11.6,-0.6)-- (6.6,-0.6);
\draw [color=zzttqq,line width=3pt] (6.6,-0.6)-- (6.6,0.2);
\draw [color=zzttqq,line width=3pt] (6.6,0.2)-- (11.6,0.2);
\draw(9.566051220738322,4.243465214617333) node[anchor=north west] {hi};
\draw (-0.003126415538731179,0.18471185186201763) node[anchor=north west] {test};
\draw (9.343856693580186,2.8658591462368794) node[anchor=north west] {test};
\draw (9.180914040330887,1.354936361561543) node[anchor=north west] {test};
\draw (8.766150922969032,-0.12636048615937515) node[anchor=north west] {test};
\end{tikzpicture}
\end{document}

答案1

我想問題是如何更有效或更好地繪製它。圖片其實只是兩個三角形,頂部散佈著一些文字。為了使自訂更容易,我定義了 座標(A)(B)對於(C)三角形的頂點,從頂部開始順時針讀取,然後繪製與這些座標相關的所有內容。

三角形框中的文字在\foreach循環內顯示為逗號分隔的清單。這是輸出:

在此輸入影像描述

....這是代碼:

  \documentclass[10pt]{article}
  \usepackage{tikz}
  \usetikzlibrary{calc}
  \definecolor{leftcolour}{HTML}{21bdbe}
  \definecolor{rightcolour}{HTML}{19bfe0}

  \begin{document}

      \begin{tikzpicture}[
          nodetext/.style={draw=leftcolour, very thick, rectangle, anchor=south west,
                           fill=white,minimum height=2em},
          lefttriangle/.style={draw=leftcolour,fill=leftcolour},
          righttriangle/.style={draw=rightcolour,fill=rightcolour!50}
        ]
        \coordinate (A) at (0,8.5); % top          - formerly (8,5.8);
        \coordinate (B) at (6,0);   % bottom right - formerly(11.6, -1.2);
        \coordinate (C) at (-6,0);  % bottom left  - (3.6,-1.2);
        \draw[lefttriangle]  (A) -- ($ (C)!0.5!(B) $) --(C) -- cycle;
        \draw[righttriangle] (A) -- ($ (C)!0.5!(B) $) --(B) -- cycle;
        \foreach \word [count=\c, evaluate=c as \y using \c/5,]
           in {Test 1, Test 2, Test 3, Test 4} {
              \node[nodetext, minimum width=\c*26mm] at ($ (A)!\y!(C)+(-0.3,-0.42) $){\word};
        }
     \end{tikzpicture}

  \end{document}

以下兩個模糊因素可能需要微調:

  • 文字方塊的偏移量為(-0.3,-0.42),因為 2 乘以 0.3 的平方根約為 0.42
  • 文字方塊編號的最小寬度\c設定為\c*26mm

\c/5最後,可以透過在行中替換來更改文字方塊的數量

\foreach \word [count=\c, evaluate=c as \y using \c/5,]

當然\c/6,正確執行此操作您可能需要調整座標(A)(B)(C)。當然,原則上,這也可以自動化。

編輯

這是 sgmoye 從評論中得到的居中改進。我已將節點的寬度“偏移”更改為\c*25mm

在此輸入影像描述

以及修改後的 MWE:

  \documentclass[10pt]{article}
  \usepackage{tikz}
  \usetikzlibrary{calc}
  \definecolor{leftcolour}{HTML}{21bdbe}
  \definecolor{rightcolour}{HTML}{19bfe0}

  \begin{document}

      \begin{tikzpicture}[
          nodetext/.style={draw=leftcolour, very thick, rectangle, anchor=south,
                           fill=white,minimum height=2em},
          lefttriangle/.style={draw=leftcolour,fill=leftcolour},
          righttriangle/.style={draw=rightcolour,fill=rightcolour!50}
        ]
        \coordinate (A) at (0,8.5); % top          - formerly (8,5.8);
        \coordinate (B) at (6,0);   % bottom right - formerly(11.6, -1.2);
        \coordinate (C) at (-6,0);  % bottom left  - (3.6,-1.2);
        \draw[lefttriangle]  (A) -- ($ (C)!0.5!(B) $) --(C) -- cycle;
        \draw[righttriangle] (A) -- ($ (C)!0.5!(B) $) --(B) -- cycle;
        \foreach \word [count=\c] in {Test 1, Test 2, Test 3, Test 4} {
              \node[nodetext, minimum width=\c*25mm] at (0, 8.5-1.85*\c){\word};
        }
     \end{tikzpicture}

  \end{document}

相關內容