我有以下代码来绘制两个类似抛物线的“函数”:
\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}