在 Tikz 中绘制游泳池

在 Tikz 中绘制游泳池

有谁知道我如何使用 TikZ 绘制下图? 在此处输入图片描述

非常感谢。

答案1

对于这样的任务hobby似乎是一个合适的工具。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{hobby}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=0.3*\x+2*cos(20*\x-40);
    r=0.5;},>=stealth]
 \def\lsty{6.2,7.2,6.8,5.6,5.0,4.8,4.8}
 \def\lstB{}
 \def\x{0}
 \loop
  \pgfmathsetmacro{\y}{0.2*{\lsty}[\x]+f(\x)}
  \ifnum\x=0
   \edef\lstA{(-1,{f(0)-0.25*{\lsty}[0]})}
   \edef\lstP{(\x,\y)}
  \else
   \edef\lstP{\lstP,(\x,\y)}
  \fi
  \edef\lstA{\lstA (\x,\y)}
  \pgfmathsetmacro{\y}{\y-r*{\lsty}[\x]}
  \edef\lstB{(\x,\y) \lstB}
  \edef\x{\the\numexpr\x+1}
 \ifnum\x<7\repeat
 %
 \draw[closed hobby,fill=blue!20] plot coordinates {\lstA (\x,{\y+0.5*r*{\lsty}[\x-1]}) \lstB} ;
%  %
 \foreach \x [count=\n from 0] in \lstP
 {\pgfmathsetmacro{\y}{{\lsty}[\n]}
 \path \x coordinate (t-\n) -- node(n-\n){$\y$} ++ (0,{-r*\y}) coordinate (b-\n)
   (n-\n) edge[->] (t-\n) edge[->] (b-\n);
 }
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者用foreach。(我偷了一个函数Dimhttps://tex.stackexchange.com/a/570712因为我找不到其他可行的方法,出于某种原因,其他地方建议的内置但未记录的函数dim没有产生好的结果。也许有更好的方法。)

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{hobby}
\makeatletter
\pgfmathdeclarefunction{Dim}{1}{% from https://tex.stackexchange.com/a/570712
  \begingroup%
    \pgfmath@count=0\relax
    \edef\pgfutil@tmpb{#1}%
    \pgfutil@for\pgfutil@tmpa:={\pgfutil@tmpb}\do{%
      \advance\pgfmath@count by1\relax}%
    \edef\pgfmathresult{\the\pgfmath@count}% 
    \pgfmath@smuggleone\pgfmathresult% 
  \endgroup}  
\makeatother
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=0.3*\x+2*cos(20*\x-40);},>=stealth]
 \def\lsty{6.2,7.2,6.8,5.6,5.0,4.8,4.8}
 \pgfmathtruncatemacro{\mydim}{Dim("\lsty")}
 \foreach \y [count=\x from 0] in \lsty
 {
 \path (\x,{0.2*\y+f(\x)}) coordinate (t-\x)
  -- coordinate (n-\x)
  (\x,{-0.3*\y+f(\x)}) coordinate[alias=b-\x] 
  (t-\the\numexpr2*\mydim-1-\x);
 }
 \draw[closed hobby,fill=blue!20]  plot[samples
    at={0,...,\the\numexpr2*\mydim-1}] (t-\x);
 \path foreach \y [count=\x from 0]in \lsty 
 {(n-\x) node{$\y$} edge[->] (t-\x) edge[->] (b-\x)};
\end{tikzpicture}
\end{document}

答案2

只是我的尝试。

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{arrows, hobby, calc}
\begin{document}
\begin{tikzpicture}
\foreach \dy [count=\i] in {6.2,7.2,6.8,5.6,5.0,4.8,4.8} {
   \coordinate (A\i) at (2*\i,{3*sin(0.4*(\i-1) r)}); 
   \coordinate  (B\i) at ($ (A\i)+(0,\dy) $);
}
\draw[very thick, fill=blue!20] plot[closed hobby] coordinates  {(0,3)(A1)(A2)(A3)(A4)(A5)(A6)(A7)(16,4)(B7)(B6)(B5)(B4)(B3)(B2)(B1)};
\foreach \dy [count=\i] in {6.2,7.2,6.8,5.6,5.0,4.8,4.8}
   \draw[very thick,<->,>=stealth] (A\i) -- node[fill=blue!20]{\dy} (B\i);
\end{tikzpicture}
\end{document}

带有垂直箭头的形状

相关内容