2

I need to draw a lightning bolt which starts at a point and its sharp edge ending at another point. For instance, let me define the points

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (p1) at (1,3);
\coordinate (p2) at (5,5);
\end{tikzpicture}

Now how do I plot a Lightning bolt between p1 and p2. I know how to draw it explicitly calculating co-ordinates with respect to (1,3) and (5,5). However, if this values change, the calculation has to be re-done again which is not a clean solution.

7

Here's a lightning bolt decoration. It's not hugely customizable as it stands, as all the dimensions of the bolt are determined by the distance between the two points:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations}
\pgfdeclaredecoration{lightning bolt}{draw}{
\state{draw}[width=\pgfdecoratedpathlength]{
  \pgfpathmoveto{\pgfpointorigin}%
  \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength*0.6}%
    {-\pgfdecoratedpathlength*.1}}%
  \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength*0.55}{0pt}}%
  \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}%
  \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength*0.4}%
    {\pgfdecoratedpathlength*.1}}%
  \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength*0.45}{0pt}}%
  \pgfpathclose%
}%
}
\begin{document}
\begin{tikzpicture}
\foreach \i [evaluate={\p=rnd*75+25;\q=rnd*75+25;}] in {1,...,50}
  \fill [yellow!\p!orange!\q!blue, decoration=lightning bolt, decorate] 
  (rand*4, rand*4) -- ++ (rand*180:rnd*4+2);
\end{tikzpicture}
\end{document}

enter image description here

And here is a lightning bolt to path:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{calc}
\tikzset{lightning bolt to/.style={to path={
let \p1=(\tikztostart), \p2=(\tikztotarget), \n1={veclen(\y2-\y1,\x2-\x1)} in
  (\p1) -- ($($(\p1)!0.6!(\p2)$)!\n1*.1!-90:(\p2)$) -- ($(\p1)!0.55!(\p2)$) --
  (\p2) -- ($($(\p1)!0.4!(\p2)$)!\n1*.1!90:(\p2)$) -- ($(\p1)!0.45!(\p2)$) -- 
  cycle (\p2)% Move to end point
}}}
\begin{document}
\begin{tikzpicture}
\foreach \i [evaluate={\p=rnd*75+25;\q=rnd*75+25;}] in {1,...,50}
  \fill [yellow!\p!orange!\q!blue]
  (rand*4, rand*4) to [lightning bolt to] ++ (rand*180:rnd*4+2);
\end{tikzpicture}
\end{document}

The result is the same as before (apart from the randomness).

6

Here a starting point for you usgin the pic environment. You can pass two coordinates to the pic and so control the start- and end-point of your lightning-path.

The following code uses static offsets to draw the zig-zag (-.25 and .25) whicht could be replaced by a dynamically calculated value. This could pretty up the lighting when used in different lengths and scales.

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{calc, arrows.meta}

\tikzset{
  pics/lightning/.style 2 args={code={
    \draw [thick, arrows={-Stealth[scale=2]}] (#1) -- 
               ($(#1)!.5!(#2) + (.25,-.25)$) -- 
               ($(#1)!.5!(#2) + (-.25,.25)$) --
               (#2);
  }}}

\begin{document}
  \begin{tikzpicture}
    \pic {lightning={0,2}{0,0}};
    \pic [blue] {lightning={1,3}{1,-1}};
    \pic [red] {lightning={0,3}{-2,0}};
  \end{tikzpicture}
\end{document}

rendered image

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.