如何在 TikZ 中連接兩條拋物線路徑?

如何在 TikZ 中連接兩條拋物線路徑?

我正在嘗試繪製一條由兩條拋物線路徑組成的簡單曲線,一條水平,另一條垂直。到目前為止,我決定使用parabola,但我可能會不這樣做。下面是我目前的程式碼:

\documentclass[12pt, tikz, border=0mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc,intersections,through}
\tikzset{every label/.style = {label distance=2pt, inner sep=0pt}}
\tikzset{every node/.style={font=\footnotesize}}
\tikzset{> = {Stealth[width=4pt, length=5pt, inset=1pt]}}

\begin{document}

\newlength{\unit}
\setlength{\unit}{0.5cm}
\begin{tikzpicture}[x=\unit, y=\unit, line width=2pt]
  % Begin axes
  \begin{scope}[line width=0.5pt]
    \draw[->] (-3.5,0) -- (5.5,0);
    \draw[->] (0,-0.5) -- (0,5.5);
    \foreach \x in {-3, -2, -1, 1, 2, 3, 4, 5}
    \draw (\x,2pt) -- (\x,-2pt) node [anchor=base, shift={(0,-8pt)}, inner sep=1pt] {$\x$};
    \foreach \y in {1, ..., 5}
    \draw (2pt,\y) -- (-2pt,\y) node [anchor=east, inner sep=1pt] {$\y$};
  \end{scope}
  % End axes
  \draw [rotate around={-90:(3,5)}](3,5) parabola (5.5,-1);
  \draw (3,5) parabola (5,0);
\end{tikzpicture}      
\end{document}

在包含的輸出中顯而易見的問題是兩條路徑看起來「脫節」。有沒有辦法讓它們看起來像一條連續的路徑?

答案1

兩條單獨的線不能連接起來。您需要在單一路徑中繪製兩條拋物線。嘗試:

\documentclass[12pt, tikz, border=0mm]{standalone}
\usetikzlibrary{arrows.meta, % <-- only this is needed 
                calc, intersections, through}

\newlength{\unit}
\tikzset{every label/.style = {label distance=2pt, inner sep=0pt},
         every node/.style={font=\footnotesize},
         > = {Stealth[width=4pt, length=5pt, inset=1pt]}
         }

\begin{document} 
\setlength{\unit}{0.5cm}
\begin{tikzpicture}[x=\unit, y=\unit, line width=2pt]
  % Begin axes
  \begin{scope}[line width=0.5pt]
    \draw[->] (-3.5,0) -- (5.5,0);
    \draw[->] (0,-0.1) -- (0,5.5);
    \foreach \x in {-3, -2,...,5}
        \draw (\x,2pt) -- ++ (0,-4pt) node[below] {$\x$};
    \foreach \y in {1,...,5}
        \draw (2pt,\y) -- ++ (-4pt,0) node[left] {$\y$};    
  \end{scope}% End axes
  % the first parabola start at (5,0) and end at (3,5) 
  % wherends start the second, rotated one
  % for this the bend of the first parabola is moved to the end of path
  \draw (5,0) parabola[bend at end] (3,5) {[rotate around={-90:(3,5)}] parabola (5.5,-1)} ;
    \end{tikzpicture}
\end{document}

在此輸入影像描述

答案2

扎科的做法是最正確的,這是肯定的。,有時統一路徑可能有點棘手...

然後,有一個棘手的方法來製作路徑團結起來,而事實上他們是不同的道路。為此,可以使用line cap最初設定為 的密鑰butt。在垂直接頭中,可以使用line cap=rect這將使路徑看起來像是銳利連接的,或者在所有情況下line cap=round都會使路徑看起來像是圓形連接的。

由於這裡的情況是垂直連接,因此可以使用rect,但我在 MWE 中添加了更多繪圖,只是為了展示如何line cap=round始終有效,但rect並非總是有效。另外,在左上角還有所有三個線帽的範例,以便了解差異。

在此輸入影像描述

完整的 MWE:

\documentclass[12pt, tikz, border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc,intersections,through}
\tikzset{every label/.style = {label distance=2pt, inner sep=0pt}}
\tikzset{every node/.style={font=\footnotesize}}
\tikzset{> = {Stealth[width=4pt, length=5pt, inset=1pt]}}

\begin{document}

\newlength{\unit}
\setlength{\unit}{0.5cm}
\begin{tikzpicture}[x=\unit, y=\unit, line width=2pt]
  % Begin axes
  \begin{scope}[line width=0.5pt]
    \draw[->] (-3.5,0) -- (5.5,0);
    \draw[->] (0,-0.5) -- (0,5.5);
    \foreach \x in {-3, -2, -1, 1, 2, 3, 4, 5}
    \draw (\x,2pt) -- (\x,-2pt) node [anchor=base, shift={(0,-8pt)}, inner sep=1pt] {$\x$};
    \foreach \y in {1, ..., 5}
    \draw (2pt,\y) -- (-2pt,\y) node [anchor=east, inner sep=1pt] {$\y$};
  \end{scope}
  % End axes
  \draw[line cap=rect] [rotate around={-90:(3,5)}](3,5) parabola (5.5,-1);
  \draw (3,5) parabola (5,0);
  %%MWE ends here -- the rest is for demonstration purposes only
  \draw (1,0) -- (1,1);\draw[line cap=round] (1,1) -- +(135:1);
  \draw (2,0) -- (2,1);\draw[line cap=rect] (2,1) -- +(135:1);
  \draw (-3,5) -- +(1,0);\draw[ultra thin, white] (-3,5) -- +(1,0) node[font={\tiny\ttfamily},right,black]{butt};
  \draw[line cap=round] (-3,4.5) -- +(1,0); \draw[ultra thin, white] (-3,4.5) -- +(1,0) node[font={\tiny\ttfamily},right,black]{round};
  \draw[line cap=rect] (-3,4) -- +(1,0);\draw[ultra thin, white] (-3,4) -- +(1,0) node[font={\tiny\ttfamily},right,black]{rect};
\end{tikzpicture}      
\end{document}

*當然,這僅對相同寬度的線有效,如果線具有不同的寬度,則用此方法將無法正確連接...

相關內容