兩條線相交

兩條線相交

我有一個相當簡單的問題:

有沒有簡單的方法可以修改下面的程式碼

\documentclass[border=10pt]{standalone}

\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}

    \draw[red, line width = 1mm] (2, 0) -- (-2, 0);
    \draw[blue, line width = 1mm] (0, 2) -- (0, -2);
  \end{tikzpicture}
\end{document}

這樣紅線在藍線前面,藍線在交叉點有一個「洞」。不改變它們的繪製順序(例如首先繪製紅色)並且不確切了解交點?本質上,最終產品應該看起來像這樣:

在此輸入影像描述

答案1

在簡單的情況下, tikz 選項double=<color>double distance=<dimen>與 tikz 庫一起很有用background。請參閱以下範例。

在複雜的情況下,您可能需要knots包,如 @Sebastiano 建議的那樣問題評論

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}
  \begin{tikzpicture}
    \draw[white, double=red, double distance=1mm, line width=1mm](2, 0) -- (-2, 0);
    \scoped[on background layer] 
      \draw[blue, line width = 1mm] (0, 2) -- (0, -2);
  \end{tikzpicture}
\end{document}

在此輸入影像描述

答案2

這裡有一些方法。一切都很簡單。

在此輸入影像描述

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\tikzset{every path/.style={line width=1mm}}    
\begin{tikzpicture}
\path 
(-1,-1) coordinate (A) 
(1,1)   coordinate (B)
(-1,1)  coordinate (C) 
(1,-1)  coordinate (D);

\draw[blue] (A)--(B);
\draw[white,line width=2mm] (C)--(D);
\draw[red] (C)--(D);
\end{tikzpicture}

\begin{tikzpicture}
\draw[teal] (A)--(B);
\draw[orange,preaction={draw,line width=2mm,white}] (C)--(D);
\end{tikzpicture}

\begin{tikzpicture}
\path (intersection of A--B and C--D) coordinate (I);
\draw[brown] (A)--(B);
\draw[magenta,shorten >=1mm] (C)--(I);
\draw[magenta,shorten >=1mm] (D)--(I);
\end{tikzpicture}

\begin{tikzpicture}
\path (intersection of A--B and C--D) coordinate (I);
\draw[magenta] (C)--(D);
\path (C)--(I) node[pos=1,sloped,fill=white]{};
\draw[brown] (A)--(B);
\end{tikzpicture}

\begin{tikzpicture} % page 178, PGF manual
\draw[cyan] (A)--(B);
\draw[white,double=red,double distance=1mm] (C)--(D);
\end{tikzpicture}
\end{document}

相關內容