我有以下程式碼來繪製兩個類似拋物線的“函數”:
\begin{tikzpicture}[
remember picture,
overlay
]
\tikzmath{
\w = 4;
\yVs0 = 2;
\yVsl = 1;
\yVsf = 3;
\yss0 = \yVs0*2;
\yssl = \yVsl*1.5;
\yssf = \yVsf*1.1;
}
\tikzset{
shift={(current page.center)}
}
\begin{scope}[
shift={($0.5*(-\w,-\w)$)}
]
\draw[->,thick] (0,0) -- (\w,0);
\draw[
blue]
(0,\yVs0) .. controls (\w*1/4,\yVsl) and (\w*3/4,\yVsl) .. (\w,\yVsf);
\draw[
red]
(0,\yss0) .. controls (\w*1/4,\yssl) and (\w*3/4,\yssl) .. (\w,\yssf);
\end{scope}
\end{tikzpicture}
如何繪製這兩條曲線之間的 y 座標差?例如,透過以規則的 x 座標步長沿著每個函數放置 N 個標記,並取得這些標記的 y 座標差。
答案1
這是一種使用交叉點的蠻力方法。它計算與一些垂直路徑的交點及其 y 值的差,將它們儲存在列表中並繪製列表。
\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{calc,intersections,math}
\begin{document}
\begin{tikzpicture}
\tikzmath{
\w = 4;
\yVs0 = 2;
\yVsl = 1;
\yVsf = 3;
\yss0 = \yVs0*2;
\yssl = \yVsl*1.5;
\yssf = \yVsf*1.1;
}
\tikzset{
shift={(current page.center)}
}
\begin{scope}[
shift={($0.5*(-\w,-\w)$)}
]
\draw[->,thick] (0,0) -- (\w,0);
\draw[name path=A,
blue]
(0,\yVs0) .. controls (\w*1/4,\yVsl) and (\w*3/4,\yVsl) .. (\w,\yVsf);
\draw[name path=B,
red]
(0,\yss0) .. controls (\w*1/4,\yssl) and (\w*3/4,\yssl) .. (\w,\yssf);
\edef\lstCoords{(0,\yss0-\yVs0)}
\foreach \X in {1,...,9}
{\pgfmathsetmacro{\myx}{\X*0.1*\w}
\path[name path=vert,overlay] ([yshift=-1pt]current bounding box.south-|\myx,0)
-- ([yshift=1pt]current bounding box.north-|\myx,0);
\path[name intersections={of=A and vert,by=i1},name intersections={of=B and vert,by=i2}]
let \p1=($(i2)-(i1)$) in \pgfextra{\xdef\lstCoords{\lstCoords (\myx,\y1)}};
}
\edef\lstCoords{\lstCoords (\w,\yssf-\yVsf)}
\draw[orange] plot[smooth] coordinates {\lstCoords};
\end{scope}
\end{tikzpicture}
\end{document}