如何在 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}

*当然,这只对相同宽度的线条有效,如果线条宽度不同,则无法使用此方法正确连接它们......

相关内容