2

I have a rectangular shaped carpet. Is it possible to create a specific code to simulate a grass carpet, like in a golf field?

\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} 

enter image description here

Related question: Projectile motion: change an old code and make it more beautiful

4
  • 3
    If you want a realistic picture, just include an external graphics and project it on the plane. Otherwise recycle something along the lines of the answers to this question. BTW, you could use the tpp library to define the boundaries of the plane.
    – user228539
    Nov 17 '20 at 2:07
  • 1
    @abcdefg Hi and thank you for your advice. In this period I'm very tired with the school. What is the significance of the word BWT, and the tpp library of TikZ? Do you think that I am able to put into green carpet the the chalk effect to reproduce the grass? :-(
    – Sebastiano
    Nov 21 '20 at 21:22
  • 2
    Well, the strip of grass appears to be wider in front than in the back. That is, it seems to represent a perspective view. Such a view can be installed systematically with the tpp library, i.e. you can change some parameters to adjust the view in a systematic, controlled way. As for the the chalk effect: I do not know. This is just a random suggestion for something that might go in the right direction.
    – user228539
    Nov 21 '20 at 21:53
  • 1
    @abcdefg Thank you very much for your replay for my comment. I not know the tpp library, and it is the first time that I see this tikzlibrary. Into your fist comment there is a link tex.stackexchange.com/q/334341 and into there are the similar to the chalk effect. I don't communicate well with English. :-) and your help for me is like the arabic language :-). Now I put a bounty.
    – Sebastiano
    Nov 21 '20 at 21:59
10
+100

I'm not sure whether there might be more elegant ways to do this, but here is my try. I might improve it in the next days if I can manage the grass to look more convincing, but LaTeX does not like too many computations and if you try to force too many "sprouts", it will stop compiling with a memory size error.

Warning: Compiling might take a little while.

Update #1: Changed the looks and color of grass for better effect.

Update #2: Changed a few parameters and how the coordinates are calculated, also using only brighter green color to make the grass more visible.

Output

enter image description here

Code

\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
  • Very nicer :-))))))), and you think I can make these wonderful creations? Tomorrow I will go to the supermarket, storm permitting, to buy the grass mower :-) aahahahahahah
    – Sebastiano
    Nov 21 '20 at 22:19
  • 1
    @Sebastiano ahah thank you! Well, anybody can and there's plenty better than me around.
    – Alenanno
    Nov 21 '20 at 22:48
11

enter image description here

The solution I propose is based on the idea of drawing grass blades through a decoration. Executing the code might take a while though; 60 seconds if next \j -\j=.02 in the \foreach loop.

grass decoration is introduced at the beginning. It uses multiple marks spaced apart by a given step, the argument of the decoration.

Then the grass is constructed along paths that follow the long side of the carpet (see the second figure where both steps are increased by a factor of 10 and the length of the blades by 2). In my opinion, in case this solution is considered, the main choices will turn around the colors needed to obtain the grass feeling.

enter image description here

The code:

\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}
4
  • Beautiful interpretation and excellent technical skills. But now who should I give the bounty to now? :-( You have put me in trouble.
    – Sebastiano
    Nov 25 '20 at 21:59
  • Don't bother; it is not the bounty that pushed me to look for a construction.
    – Daniel N
    Nov 26 '20 at 6:45
  • Ok! :-) Thank you very much also for your job.
    – Sebastiano
    Nov 26 '20 at 12:51
  • Same for me, I was already working on it before the bounty, but I also liked your approach, Daniel so I +1'ed your answer. I tried to compile it on my system but I couldn't. One thing I would have changed is the calculation of where the decorations appear, removing the need for clip.
    – Alenanno
    Nov 26 '20 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.