# Rotating a segment along a line

I would like to draw a line, something like

\coordinate (A) at (0.42,0.5);
\coordinate (B) at (0.98,0.44);
\draw[ultra thick] (A) -- (B);


and draw a sequence of small segments on this line where

1. the first is centred at (B),
2. the second is a bit further on the direction of (A) and a bit rotated,
3. the thirst a bit further than the second and a bit more rotated,...

and so on up to point (A).

I ended up with the following code, but it does not work at all.

\foreach \i in {0,0.1,..,1}{%
\node [draw, rotate=180*\i] at (A)!\i!(B) {%
\begin{tikzpicture}
\draw (-0.1,0) -- (0.1,0);
\end{tikzpicture}};
}


Extra comments: the angles of the small segments at (B) and (A) should be under control. For example, I would like to draw a sequence of segments that rotates 180º or a sequence that rotates 720º

• The code doesn't work because (1) you nest tikzpictures, and (2) your coordinate need to be surrounded by round brackets. – user156344 Apr 14 at 19:44
• Thanks, I do not know what you mean by "you nest tikzpictures" – MonLau Apr 14 at 20:24
• You put a tikzpicture inside a TikZ node, which is never recommended and which can easily produce errors. – user156344 Apr 14 at 20:25

I don't really understand what you mean but you can take this as a starting point

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (3,0);
\coordinate (b) at (0,1);
\draw (a) -- (b);
\foreach \i in {0,1,...,10} {
\coordinate (x) at ($(a)!{\i/10}!(b)$);
\draw ($(x)+({90-(4.5*\i)}:0.5)$) -- ($(x)+({-90-(4.5*\i)}:0.5)$);
}
\end{tikzpicture}
\end{document}


You can build a macro based on this code

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (3,0);
\coordinate (b) at (0,1);
\draw (a) -- (b);
\def\beginangle{85}
\def\endangle{143}
\def\leng{1}
\foreach \i in {0,...,10} {
\coordinate (x) at ($(a)!{\i/10}!(b)$);
\draw ($(x)+({\beginangle-((\beginangle-\endangle)*\i/10)}:{\leng/2})$) -- (x) coordinate[pos=2] (y) (x) -- (y);
}
\end{tikzpicture}
\end{document}


• Thanks a lot, it is a great starting point – MonLau Apr 15 at 14:01

Just for fun: a version using decorations.markings.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[rotated bar step/.initial=0.0499,
rotated bar length/.initial=2mm,
rotated bar start angle/.initial=90,
rotated bars/.style={%
postaction={decorate,decoration={markings,
mark=between positions 0 and 1 step \pgfkeysvalueof{/tikz/rotated bar step} with
{\pgfmathsetmacro{\barangle}{(\pgfkeysvalueof{/pgf/decoration/mark info/sequence
number}-1)*\pgfkeysvalueof{/tikz/rotated bar step}*#1+\pgfkeysvalueof{/tikz/rotated bar start angle}}
\draw (\barangle:\pgfkeysvalueof{/tikz/rotated bar length}/2)
-- (\barangle+180:\pgfkeysvalueof{/tikz/rotated bar length}/2);}}}}]
\draw[rotated bars=720] (0,1) -- (3,0);
\draw[rotated bar length=4mm,rotated bar step=0.0999,rotated bars=720] (4,1) to[bend left] (7,0);
\end{tikzpicture}
\end{document}


• That is also nice. Unfortunately, I do not understand that much the code. Are the initial and final segments always perpendicular to the line? Can we control the number of segments on the line? – MonLau Apr 15 at 14:04
• @MonLau In this example they are since rotated bar start angle=90 and the difference between initial and final angle, i.e. the argument of rotated bars, is an integer multiple of 360. For different choices of these keys they are in general not. – user121799 Apr 15 at 14:12
• @marmot I thought it was rotated bär start angle = 90 :) @UlrikeFischer – user156344 Apr 15 at 14:16
• @JouleV Doesn't wörk with pdflatex. ;-) – user121799 Apr 15 at 14:18
• @marmot You used wrong engine. It does work with pdflätex. I just checked. – user156344 Apr 15 at 14:19