长方形地毯,上面有草

长方形地毯,上面有草

我有一块矩形地毯。是否可以创建特定代码来模拟草坪地毯,就像高尔夫球场上的草坪地毯一样?

\documentclass[tikz,border=5]{standalone}
\usepackage[prefix=]{xcolor-material}
\begin{document}
\begin{tikzpicture}[x=(330:1cm),y=(30:1cm),z=(90:1cm)]
\fill [LightGreen] (-1,-1,0) -- (-.5,1,0) -- (11,2,0) -- (11,-2,0) -- cycle;
\end{tikzpicture}
\end{document} 

在此处输入图片描述

相关问题:抛射运动:改变旧代码,让它更美观

答案1

我不确定是否有更优雅的方式来做到这一点,但这是我的尝试。如果我能管理好草地,我可能会在接下来的几天里改进它看起来更有说服力,但 LaTeX 却不是就像太多的计算,如果你尝试强制太多的“芽”,它将停止编译并出现错误memory size

警告:编译可能需要一点时间。

更新#1:改变草的外观和颜色以获得更好的效果。

更新 #2:改变了一些参数和坐标的计算方式,同时仅使用更亮的绿色来使草更加清晰可见。

输出

在此处输入图片描述

代码

\documentclass[tikz,margin=10pt]{standalone}
\usepackage[prefix=]{xcolor-material}
\usetikzlibrary{calc}

\tikzset{
    grass/.style={thin, LightGreen!#1}
}

\newcommand\myangle{5}

\begin{document}
\begin{tikzpicture}[x=(330:1cm),y=(30:1cm),z=(90:1cm)]
\fill[LightGreen] (-1,-1,0) coordinate (c1) -- (-.5,1,0) coordinate (c2) -- (11,2,0) coordinate (c3) -- (11,-2,0) coordinate (c4) -- cycle ;

\foreach \x [count=\n] in {0,.005,...,1}{%
    \foreach \multiplier in {1,...,12}{%
        \pgfmathsetmacro\grad{random(80,90)}
        \pgfmathsetmacro\varA{random(1,100)}
        \pgfmathsetmacro\varB{random(1,100)}
        \pgfmathsetmacro\varC{random(1,100)}
        \pgfmathsetmacro\varD{random(1,100)}
        \pgfmathsetmacro\ciuf{rand/10}

        \coordinate (a\n) at ($(c1)!\x!(c4)$);
        \coordinate (b\n) at ($(c2)!\x!(c3)$);
    
        \path ($(a\n)!\varA/100!(b\n)$) 
            edge[grass=\grad, bend right=\myangle]++  (0,\ciuf pt,.1pt);

        \path ($(a\n)!\varB/100!(b\n)$) 
            edge[grass=\grad, bend right=\myangle]++  (0,\ciuf pt,.1pt);

        \path ($(a\n)!\varC/100!(b\n)$) 
            edge[grass=\grad, bend right=\myangle]++  (0,\ciuf pt,.1pt);

        \path ($(a\n)!\varD/100!(b\n)$) 
            edge[grass=\grad, bend right=\myangle]++  (0,\ciuf pt,.1pt);
    }
}
\end{tikzpicture}
\end{document} 

答案2

在此处输入图片描述

我提出的解决方案是基于通过装饰来绘制草叶的想法。不过执行代码可能需要一段时间;60如果next \j -\j=.02在循环中则为秒\foreach

grass修饰在开头引入。它使用多个标记,这些标记之间间隔一个给定的步骤,即修饰的参数。

然后,沿着地毯长边的路径构建草坪(参见第二张图,其中两个步骤都增加了10叶片长度2)我认为,如果考虑这个解决方案,主要的选择将围绕获得草地感觉所需的颜色进行。

在此处输入图片描述

代码:

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, math, decorations.markings}

\begin{document}
\xdefinecolor{GY}{RGB}{153, 235, 40}
\xdefinecolor{G}{RGB}{139, 214, 74}
\xdefinecolor{BG}{RGB}{58, 139, 33}


\tikzmath{%
  integer \N;
  real \step;
  \step=.01;
  \N = ceil(1/\step);
}
\tikzset{%
  grass/.style={%
    decorate,
    decoration={markings,
      mark=between positions 0 and 1 step{#1} with {%       
        \pgfmathsetmacro{\tmp}{%
          \pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}
        \pgfmathsetmacro{\tmpYG}{int(100-60*sin((\tmp/\N+.25*rand)*90))}
        \pgfmathsetmacro{\tmpBG}{int(50-40*sin((\tmp/\N+.5*rand)*90))}
        \pgfmathsetmacro{\r}{.1+(.3+.15*rand)*\tmp/\N}
        \pgfmathsetmacro{\a}{155+30*rand}
        \pgfmathsetmacro{\dy}{rand*4}
        \draw[G!\tmpYG!GY] (0, \dy pt) -- ++(\a: \r);
        \draw[BG!\tmpBG!G] (.3*\dy pt, \dy pt) -- ++(\a+30*\r: \r*.6);
      }
    }
  }
}
\begin{tikzpicture}[x=(330:1cm), y=(30:1cm), z=(90:1cm)]
  \path
  (-1, -1) coordinate (BW)
  (-.5, 1) coordinate (BE)
  (11, 2, 0) coordinate (FE)
  (11, -2, 0) coordinate (FW);

  \clip (FW) -- ($(FE)+(0, .5pt)$) -- (BE) -- (BW) -- cycle;
  \fill[top color=BG!30!G, bottom color=G]
  (FW) -- (FE) -- (BE) -- (BW) -- cycle;
  \foreach \j in {0, .01, ..., 1}{%
    \path[postaction={grass={\step}}] ($(BE)!\j!(BW)$) -- ($(FE)!\j!(FW)$);
  }
\end{tikzpicture}
\end{document}

相关内容