11

How do I draw cracks effectively in TikZ like those shown below?

Collected from the Internet

Drawing cracks with --++ and angles and length like the way I did in the MWE seems too cumbersome. There must be some better way? Perhaps one way might be to make the path gradually tapering, with the initial side having thicker lines and the final side having thinner lines?

\documentclass{article}

\usepackage{tikz}
\definecolor{ZircMetalDark}{RGB}{107,107,107}
\definecolor{ZircMetalLight}{RGB}{203, 203, 203}

\begin{document}

\begin{tikzpicture}
\shade[
    left color = ZircMetalDark,
    right color = ZircMetalDark,
    middle color = ZircMetalLight,
    shading angle = 90,draw
](-4.5,0) rectangle (0,4.75);
\shade[ball color=red] (0,81.70pt) coordinate (a) circle (3pt);

%Cracks
\draw[black,thick]  (a) --++ (110:7pt) --++(140:6pt) --+(120:4pt)
            (a) --++ (110:7pt) --++(100:6pt) --+(110:4pt)
            (a) --++ (160:5pt) --++(190:6pt) --+(170:4pt)
            (a) --++ (160:5pt) --++(120:6pt) --+(160:4pt)
            (a) --++ (200:7pt) --++(190:6pt) --+(210:4pt)
            (a) --++ (200:7pt) --++(220:6pt) --+(210:4pt)
            (a) --++ (240:5pt) --++(230:6pt) --+(210:4pt)
            (a) --++ (240:5pt) --++(260:6pt) --+(230:4pt);
\shade[ball color=red] (0,81.70pt) coordinate (a) circle (3pt);
\end{tikzpicture}

\end{document}

Output

3
  • 1
    Somewhat related: tex.stackexchange.com/questions/147385/… Commented Mar 12, 2022 at 21:25
  • This one's useful, but the line thickness is uniform there. I am wondering how about making the line path wider at start and then tapering gradually to a point at the end.
    – SolidMark
    Commented Mar 12, 2022 at 21:29
  • Also everytime you recompile with fractal line in the code, it does draw some arbitrary ragged lines randomly at each recompile. Is there a way to make it static once I found my choice of arbitrary lines, so that they don't change in the next recompilation?
    – SolidMark
    Commented Mar 12, 2022 at 21:40

1 Answer 1

15

Here is a proposal (using fractal line from https://tex.stackexchange.com/a/152492/14500).

The last parameter of \drawcraks is the seed (an integer) for the pseudo-random generator. Change it to get other results.

\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
  fractal line/.style args={#1 and #2}{%
    % #1 is ratio of length to move the middle of each segment
    % #2 is the minimum length to apply the recurrence
    to path={
      let
      \p1=(\tikztostart), % start point
      \p2=(\tikztotarget), % end point
      \n1={veclen(\x1-\x2,\y1-\y2)}, % distance 
      \p3=($(\p1)!.5!(\p2)$), % middle point
      \p4=(rand*#1*\n1,rand*#1*\n1), % random vector
      \p5=(\x3+\x4,\y3+\y4) % random moved middle point
      in \pgfextra{
        %\typeout{#1, #2, \n1}
        \pgfmathtruncatemacro\mytest{(\n1<#2)?1:0}
        \ifnum\mytest=1 %
        \tikzset{fractal line/.style args={#1 and #2}{line to}}
        \fi
      } to[fractal line=#1 and #2] (\p5) to[fractal line=#1 and #2] (\p2)
    },
  },
}

\newcommand\drawcrak[3][fill=black]{
  % [style] start, target
  \path[#1] (#2) to[fractal line=.04 and 1mm] (#3) to[fractal line=.04 and 1mm] (#2);
}

\newcommand\drawcraks[8][fill=black]{
  % [style] start, anglemin, anglestep, anglemax, distance, numsep, seed
  \pgfmathsetseed{#8}
  \pgfmathsetmacro\crackdistone{#6/5*2}
  \pgfmathsetmacro\crackdisttwo{#6/5*3}
  \pgfmathsetmacro\angletwo{#3+#4}
  \foreach \angle in {#3,\angletwo,...,#5}{
    \path (#2) ++(\angle+rand*#4*.5:\crackdistone pt) coordinate (crack1);
    \drawcrak[#1]{#2}{crack1}
    \foreach \mynum in {1,...,#7}{
      \path (crack1) ++(\angle+rand*#4:\crackdisttwo pt) coordinate (crack2);
      \drawcrak[#1]{crack1}{crack2}
    }
  }
}

\begin{document}
\begin{tikzpicture}
  \drawcraks[fill=orange]{0,0}{0}{60}{359}{2cm}{3}{9999}
  \drawcraks[fill=gray]{4,0}{90}{45}{270}{2cm}{2}{1}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .