自动化可扩展正弦波代码

自动化可扩展正弦波代码

从答案到这个帖子,灵感来自Qrrbrbirlbel 的评论,我想出了这个解决方案;我将其发布在这个答案

现在我如何使用 \foreach 循环来自动执行该解决方案?

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\xinc}{1.}
\pgfmathsetmacro{\yinc}{1.5}
\begin{tikzpicture}[scale=.5, transform shape]
\draw[ultra thick,red,x=0.5cm,y=1cm] (0,0)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
 sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)
;
\end{tikzpicture}
\vskip 1cm
\begin{tikzpicture}[scale=.5, transform shape]
\draw[ultra thick,blue,x=0.5cm,y=1cm] (0,0)
 sin ++(\xinc,\yinc*1) cos ++(\xinc,-\yinc*1) sin ++(\xinc,-\yinc*1) cos ++(\xinc,\yinc*1)
 sin ++(\xinc,\yinc*2) cos ++(\xinc,-\yinc*2) sin ++(\xinc,-\yinc*2) cos ++(\xinc,\yinc*2)
 sin ++(\xinc,\yinc*3) cos ++(\xinc,-\yinc*3) sin ++(\xinc,-\yinc*3) cos ++(\xinc,\yinc*3)
 sin ++(\xinc,\yinc*4) cos ++(\xinc,-\yinc*4) sin ++(\xinc,-\yinc*4) cos ++(\xinc,\yinc*4)
 sin ++(\xinc,\yinc*5) cos ++(\xinc,-\yinc*5) sin ++(\xinc,-\yinc*5) cos ++(\xinc,\yinc*5)
 sin ++(\xinc,\yinc*6) cos ++(\xinc,-\yinc*6) sin ++(\xinc,-\yinc*6) cos ++(\xinc,\yinc*6)
 sin ++(\xinc,\yinc*7) cos ++(\xinc,-\yinc*7) sin ++(\xinc,-\yinc*7) cos ++(\xinc,\yinc*7)
 sin ++(\xinc,\yinc*8) cos ++(\xinc,-\yinc*8) sin ++(\xinc,-\yinc*8) cos ++(\xinc,\yinc*8)
 sin ++(\xinc,\yinc*7) cos ++(\xinc,-\yinc*7) sin ++(\xinc,-\yinc*7) cos ++(\xinc,\yinc*7)
 sin ++(\xinc,\yinc*6) cos ++(\xinc,-\yinc*6) sin ++(\xinc,-\yinc*6) cos ++(\xinc,\yinc*6)
 sin ++(\xinc,\yinc*5) cos ++(\xinc,-\yinc*5) sin ++(\xinc,-\yinc*5) cos ++(\xinc,\yinc*5)
 sin ++(\xinc,\yinc*4) cos ++(\xinc,-\yinc*4) sin ++(\xinc,-\yinc*4) cos ++(\xinc,\yinc*4)
 sin ++(\xinc,\yinc*3) cos ++(\xinc,-\yinc*3) sin ++(\xinc,-\yinc*3) cos ++(\xinc,\yinc*3)
 sin ++(\xinc,\yinc*2) cos ++(\xinc,-\yinc*2) sin ++(\xinc,-\yinc*2) cos ++(\xinc,\yinc*2)
 sin ++(\xinc,\yinc*1) cos ++(\xinc,-\yinc*1) sin ++(\xinc,-\yinc*1) cos ++(\xinc,\yinc*1)
;
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

我会选择\draw plot这个,因为将不同尺度的正弦和余弦连接起来,我们会得到一个非平滑函数。

更新:我也添加了几个\draw plot版本)。

也就是说,您可以做这样的事情。第一个正弦非常简单,但对于第二个正弦,您需要(例如)声明一个计算增量的函数:

\documentclass[tikz,border=1.618mm]{standalone}
\tikzset{declare function={f(\x)=8-abs(8-\x);}}

\begin{document}
\pgfmathsetmacro{\xinc}{1.}
\pgfmathsetmacro{\yinc}{1.5}

\begin{tikzpicture}[scale=.5, transform shape]
\draw[ultra thick,red,x=0.5cm,y=1cm] (0,0) foreach\i in {1,...,15}
  {sin ++(\xinc,\yinc) cos ++(\xinc,-\yinc) sin ++(\xinc,-\yinc) cos ++(\xinc,\yinc)};
\draw[ultra thick,blue,x=0.5cm,y=1cm] (0,-16) foreach\i in {1,...,15}
  {sin ++(\xinc,{\yinc*f(\i)}) cos ++(\xinc,{-\yinc*f(\i)}) sin ++(\xinc,{-\yinc*f(\i)}) cos ++(\xinc,{\yinc*f(\i)})};
% with \draw plot
\node at (16,-30) {\huge with \verb|\draw plot|:};
\draw[ultra thick,blue,x=0.5cm,y=1cm] plot[domain=0:64,samples=1001] (\x,{-40+f(0.25*\x)*sin(104*\x)});
\draw[ultra thick,blue,x=0.5cm,y=1cm] plot[domain=0:64,samples=1001] (\x,{-56+7*sin(2.8*\x)*sin(104*\x)});
\end{tikzpicture}
\end{document}

在此处输入图片描述

附言:比例因子过多 ( \xinc\yincscale=.5x=0.5cmy=1cm)。您确定需要全部吗?

相关内容