使用 foreach 命令平滑坐标图的语法

使用 foreach 命令平滑坐标图的语法

目前正在实施的解决方案 曲线合成 - 将两条曲线相加得到另一条曲线。我想平滑结果曲线。目前的代码:

% red line
\path[red,name path=one] plot [smooth] coordinates {(0,0) (7.5,0) (8,6) (8.5,0) (10,0)};

% function
\path[blue,name path=three]
plot[domain=0:10,samples=100,smooth] (\x,{0.5*rand+2});

% red line + function
\foreach \c in {0,...,100} {
\pgfmathsetmacro{\x}{\c/10}
\path[name path=line] (\x,-10) -- (\x,20);
\path[name intersections={of=one and line,name=newone}];
\path[name intersections={of=three and line,name=newthree}];
\path let \p1=(newone-1), \p2=(newthree-1) in 
(\x1,\y1+\y2) coordinate (sum-\c);
}
\draw[blue] (sum-0) \foreach \x in {1,...,100}{ -- (sum-\x)};

我应该使用什么\foreach语法来代替:

\draw[blue] (sum-0) \foreach \x in {1,...,100}{ -- (sum-\x)};

获取括号之间的坐标列表,[...]以便我可以执行以下操作:

\draw[blue] plot [smooth] coordinates{ [...] };

答案1

编辑新答案

您可以使用\xdef以下方式将坐标收集到变量中:

\def\pts{}
\foreach \x in {0,...,100} { \xdef\pts{\pts (sum-\x)}; }

这样\pts最终成立,这对于的论证(sum-0) (sum-1) ... (sum-100)来说是恰到好处的。coordinatesplot

示例输出

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections,calc}

\begin{document}

\begin{tikzpicture}
% red line
\path[draw,red,name path=one] plot [smooth] coordinates {(0,0) (7.5,0) (8,6) (8.5,0) (10,0)};
% function
\path[draw,blue,name path=three]
plot[domain=0:10,samples=100,smooth] (\x,{0.5*rand+2});
red line + function
\foreach \c in {0,...,100} {
  \pgfmathsetmacro{\x}{\c/10}
  \path[name path=line] (\x,-1) -- (\x,4);
  \path[name intersections={of=one and line,name=newone}];
  \path[name intersections={of=three and line,name=newthree}];
  \path let \p1=(newone-1), \p2=(newthree-1) in 
  (\x1,\y1+\y2) coordinate (sum-\c);
}
\def\pts{}
\foreach \x in {0,...,100} { \xdef\pts{\pts (sum-\x)}; }
\draw[black] plot[smooth] coordinates {\pts};
\end{tikzpicture}

\end{document}

请注意,我已更改命名路径上的范围,line以便图表不会填充太多超出必要范围的内容并保留在第 1 页。

相关内容