在另一個形狀“內部”繪製一個形狀

在另一個形狀“內部”繪製一個形狀

我想在另一個形狀(僅具有直角)內繪製一個形狀(僅具有直角)。邊緣應盡可能靠近且不重疊。

我可以手工做一些事情,如下所示:

\documentclass[tikz, border=2mm]{standalone}
\begin{document}
    \begin{tikzpicture}
        \draw [] (0, 0) -- (1, 0) -- (1, 1) -- (3, 1) -- (3, 2) -- (0, 2) -- (0, 0);
        \draw [color=red] (0.02, 0.02) -- (0.98, 0.02) -- (0.98, 1.02) -- (2.98, 1.02) -- (2.98, 1.98) -- (0.5, 1.98) --
        (0.5, 1) -- (0.02, 1) -- % Part that diverges
        (0.02, 0.02);
    \end{tikzpicture}
\end{document}

產生

在此輸入影像描述

但當然這不是很穩健(如果寬度改變,則必須重新計算 0.02 的「移位」),也不是可移植的。有沒有更好的辦法?

答案1

請嘗試以下操作:

\documentclass[tikz, border=2mm]{standalone}

\begin{document}
    \begin{tikzpicture}[
u/.style = {yshift=#1\pgflinewidth},
s/.style = {xshift=#1\pgflinewidth}
                        ]
\draw   (0, 0) -- (1, 0) -- (1, 1) -- (3, 1) -- (3, 2) -- (0, 2) -- cycle;
\draw[color=red] 
        ([u,s] 0,0) -| ([u,s=-] 1,1) -| ([u=-,s=-] 3,2) 
                    -| ([u=-] 0.5,1) -| cycle;
    \end{tikzpicture}
\end{document}

在此輸入影像描述

您可以以與我繪製紅色線條相同的方式繪製黑色線(用於-|正交座標,從而顯著縮短程式碼)。

相關內容