在 TikZ 中绘制裂缝的有效方法?

在 TikZ 中绘制裂缝的有效方法?

如何在 TikZ 中有效地绘制如下所示的裂缝?

摘自网络

--++像我在 MWE 中那样绘制具有角度和长度的裂缝似乎太麻烦了。一定有更好的方法吗?也许一种方法是让路径逐渐变细,初始侧的线条较粗,最终侧的线条较细?

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

输出

答案1

这是一个建议(使用fractal line来自https://tex.stackexchange.com/a/152492/14500)。

的最后一个参数\drawcraks种子(整数)为伪随机生成器。更改它可获得其他结果。

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

在此处输入图片描述

相关内容