代码:

代码:

我想要在 tikz 环境中用箭头图案化一个区域。以及如何控制箭头的密度(例如:区域上的箭头数量)?

在此处输入图片描述

答案1

使用该库的一个选项intersections

在此处输入图片描述

代码:

\documentclass[border=5pt]{standalone} 
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{intersections,arrows.meta}

\NewDocumentCommand\DrawArrows{O{0.5}mmO{-latex}mm}{
\def\Step{#1}
\pgfmathsetmacro{\Second}{#5+\Step}
\foreach \Value [count=\xi] in {#5,\Second,...,#6}
  {
  \path[overlay,name path=line\xi] 
    (\Value,100) -- (\Value,-100);
  \path[name intersections={of=#2 and line\xi,by={1\xi}}];
  \path[name intersections={of=#3 and line\xi,by={2\xi}}];
  \ifdim#5pt<\Value pt\relax
    \ifdim\Value pt<#6pt\relax
    \draw[#4]
      (1\xi) -- (2\xi); 
  \fi\fi
  }
}

\begin{document}

\begin{tikzpicture}
\draw[name path=curve1,ultra thick]
  (0,0) .. controls ++(1,5) and ++(0,3) .. (7,2);
\draw[name path=curve2,ultra thick]
  (0,0) -- (7,2);
\DrawArrows{curve1}{curve2}{0}{7}  
\begin{scope}[xshift=8cm]
\draw[name path=curve1,ultra thick]
  (0,0) .. controls ++(1,5) and ++(0,3) .. (7,2);
\draw[name path=curve2,ultra thick]
  (0,0) -- (7,2);
\DrawArrows[0.25]{curve1}{curve2}[Stealth-Stealth,cyan]{0}{7}  
\end{scope}
\begin{scope}[yshift=-5cm]
\draw[name path=curve1,ultra thick]
  (0,0) .. controls ++(1,5) and ++(0,3) .. (7,2);
\draw[name path=curve2,ultra thick]
  (0,0) -- (7,2);
\DrawArrows[1]{curve2}{curve1}[ultra thick,orange,-{Triangle[length=10pt]}]{0}{7}  
\end{scope}
\begin{scope}[xshift=8cm,yshift=-5cm]
\draw[name path=curve1,ultra thick]
  (0,0) .. controls ++(1,5) and ++(0,3) .. (7,2);
\draw[name path=curve2,ultra thick]
  (0,0) -- (7,2);
\DrawArrows[0.125]{curve2}{curve1}{2}{5}  
\end{scope}
\end{tikzpicture}

\end{document}

解释

主要命令是\DrawArrows。绘制两条路径并命名它们<name1><name2>然后使用

\DrawArrows{<name1>}{<name2>}{<start x-coordinate>}{<end x-coordinate>}

可选的第一个参数允许控制用于箭头(密度)的步骤,第二个可选参数可以控制箭头的属性(尖端、颜色、线宽等)。

答案2

如果你可以根据函数定义顶部和底部路径,那么就很简单了:

\documentclass[tikz,border=5]{standalone}
\tikzset{declare function={
  f(\x)=sin(\x/1.5)^2*2;
  g(\x)=-sin(\x*2)*sin(\x);
}}
\begin{document}
\begin{tikzpicture}[x=1pt, very thick, >=stealth]
\foreach \c [count=\k, 
  evaluate={\s=int(30); \e=int(150); \t=\s+\k*5;}] in {red, green, blue}{
\tikzset{shift=(270:\k*3)}
\draw [thick, samples=100, smooth] plot [domain=0:180] (\x, {f(\x)}) -- 
  plot [domain=180:0] (\x, {g(\x)}) -- cycle;
\foreach \i in {\s, \t,..., \e}
   \draw [draw=\c, <->] (\i, {f(\i)}) -- (\i, {g(\i)});
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容