# How to divide a segment into n equal pieces in tikz

I want to divide a vector in n pieces. But I want to do this in tikz. Years ago I made my drawing in CorelDraw and the result was this: Now I want to make it in tikz with this code:

\documentclass{article}
\usepackage{amsmath} %voor wiskundige formules
\usepackage{pgf,tikz}
\usetikzlibrary{arrows, decorations.pathmorphing, calc,intersections,through,backgrounds,snakes,patterns}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usetikzlibrary{fit,arrows.meta}

\begin{document}

\begin{center}
\begin{tikzpicture}
\tkzInit[ymax=5,xmax=5]
\tkzDefPoints{1/1/A, 4/4/B};
\tkzClip
%\tkzGrid
\draw[Circle-stealth] (A) -- (B);
\node[right] at (A) {\tiny aangrijpingspunt};
\end{tikzpicture}
\end{center}
\end{document}


Resulting in this: But I want to divide that segment [AB] in n pieces.

Thanks.

(And I've already looked at How to divide a line or a curve into n equal pieces and tick it using PSTricks and I couldn't find an answer their, because it is in pstricks and I prefer tikz.)

Since you're using tkz-euclide, you can use it to calculate the length of the path through the command \tkzCalcLength(A,B) and store it in a variable \tkzGetLength{ABl}.

Then we can set the number of "segments" (with \ticknum) we want the path split into and with a simple calculation, we can divide the path equally into those segments. Finally, we can use \tkzMarkSegment[pos=\myl, mark=|](A,B) to place marks | along the path at the designated points.

To change the number of segments, just change the value of \ticknum.

## Output ## Code

\documentclass[margin=10pt]{standalone}
\usepackage{amsmath} %voor wiskundige formules
\usepackage{pgf,tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{pgfplots}

\pgfplotsset{compat=1.8}
\usetikzlibrary{arrows, decorations.pathmorphing, calc,intersections,through,backgrounds,patterns,fit,arrows.meta}

\newcommand{\ticknum}{10}
\pgfmathsetmacro\min{\ticknum-1}

\begin{document}
\begin{tikzpicture}
\tkzInit[ymax=5,xmax=5]
\tkzDefPoints{1/1/A, 4/4/B};
\tkzClip
%\tkzGrid
\draw[Circle-stealth] (A) -- (B);
\node[right] at (A) {\tiny aangrijpingspunt};

\tkzCalcLength(A,B)\tkzGetLength{ABl} % calculates and stores length

\foreach \x in {1,...,\min}{
\pgfmathsetmacro\myl{((\ABl/\ticknum)/\ABl)*\x}
\tkzMarkSegment[pos=\myl, mark=|](A,B)
}
\end{tikzpicture}
\end{document}


One solution would be to perform a path calculation:

\documentclass[tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc, arrows}

\newcommand\customticknum{4}   % Divides into \ticknum equal parts
\newcommand\customticklen{1em} % Defines the length of a tick
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[auto]
\node[shape=coordinate] (A) at (0,0){};
\node[shape=coordinate] (B) at (3,3){};
\draw[*-*](A) -- (B);
\pgfmathparse{\customticknum-1}
\foreach \i in {1,...,\pgfmathresult}
\draw let \p1=($(B)-(A)$), \n1={veclen(\x1, \y1)}  in ($(A)!\i*(\n1)/(\customticknum)!(B)!0.5*\customticklen!90:(B)$) -- ($(A)!\i*(\n1)/(\customticknum)!(B)!0.5*\customticklen!-90:(B)$);
\end{tikzpicture}
\end{document}


The code divides the line drawn between A and B into \customticknum equal parts. The length of the tick itself can be customized with \customticklen. I'm sure there must be an easier way, but I'm unable to provide one. Cheers, hope it helps.

Explanation: The key are the path:calculations performed by ($(A)!\i*(\n1)/(\customticknum)!(B)!0.5*\customticklen!90:(B)$)and ($(A)!\i*(\n1)/(\customticknum)!(B)!0.5*\customticklen!-90:(B)$). The first part '($(A)!\i*(\n1)/(\customticknum)!(B)' is used to find the correct point between the points A and B. The second part !0.5*\customticklen!90:(B)$) moves the point half the cutomticknumlen perpendicular to the line AB (thanks to the 90 degrees before the coordinate B). This calculation is performed once with 90 degrees and once with -90 degrees.

Edit: now divides into customticknum equal parts

Another possible way would be to use the decorations.markings library, which allows for placing arbitrary markings on a path. The ticks on the arrow can easily be drawn by using the | arrow.

The code is imo self-explanatory: You draw the arrow with the usual \draw command, and place | arrows at the specified positions with decoration={...}.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}

\draw[ *->,
decoration={markings,
mark= at position 0.2 with {\arrow{|}},
mark= at position 0.4 with {\arrow{|}},
mark= at position 0.6 with {\arrow{|}},
mark= at position 0.8 with {\arrow{|}},
},
postaction={decorate}
]
(1,1) node[anchor=west] {aangrijpingspunt} -- (4,4);

\end{tikzpicture}
\end{document} If you know the function of your line (y=-.5*x+5 in my case), you can specify the interval between the segments in \foreach statement:

\foreach[evaluate=\y using -.5*\x+5] \x in {3,4,...,7}
\draw[ultra thick] ($(\x,\y)!2mm!($(\x,\y)!1!270:(2,4)$)$) -- ($(\x,\y)!2mm!($(\x,\y)!1!90:(2,4)$)$); \documentclass[border=5mm,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usetikzlibrary{calc,shapes.arrows}

\begin{document}
\begin{tikzpicture}[decoration={brace}]

% Line
\draw[dashed] (0,5) -- (10,0);
\fill (2,4) circle (.1);
\draw[>=latex,ultra thick,->] (2,4) -- (8,1);

% Divide segments
\foreach[evaluate=\y using -.5*\x+5] \x in {2.857,3.714,...,7.9}
\draw[ultra thick] ($(\x,\y)!2mm!($(\x,\y)!1!270:(2,4)$)$) -- ($(\x,\y)!2mm!($(\x,\y)!1!90:(2,4)$)$);

% Arrows
\node[draw,arrow box,arrow box arrows={north:1cm},rotate=-26.56] (arrowA) at (2,4) {\phantom{X}};
\node[draw,arrow box,arrow box arrows={north:1cm},rotate=-26.56] (arrowB) at (7.85,1.075) {\phantom{X}};
\node[anchor=west,draw,single arrow,rotate=63.44,thick] at (8.75,.625) {\hspace*{1cm}};
\node[anchor=west,draw,single arrow,rotate=63.44,xshift=1mm,yshift=-1mm,fill=white] (arrowC) at (8.75,.625) {\hspace*{1cm}};
\draw[decorate,decoration={brace,amplitude=4pt},ultra thick,xshift=-2mm,yshift=-4mm] (4.571,2.714) -- node (e) {} (3.714,3.143);
\node[anchor=west,draw,single arrow,thick,rotate=-116.56,xshift=1mm,yshift=.5mm] at (e.south west) {\hspace*{1cm}};
\node[anchor=west,draw,single arrow,rotate=-116.56,xshift=2mm,yshift=-.5mm,fill=white] (arrowD) at (e.south west) {\hspace*{1cm}};

% Text
\node[anchor=south west] at (arrowA.north) {aangrijpingspunt};
\node[anchor=south west] at (arrowB.north) {zin};
\node[anchor=south west] at (arrowC.east) {richting};
\node[anchor=north] at (arrowD.east) {eenheid};
\end{tikzpicture}
\end{document}

• Thanks for the efforts, especially for the arrow-part, but I have in mind to use this later in finding the result of two, and more vectors. So it will be a bit difficult to make this with the equations. Dec 28, 2015 at 14:34

Just for comparison, here's a function to do that in plain Metapost.

prologues := 3;
outputtemplate := "%j%c.eps";

vardef draw_n_marks_along(expr P, N) text _t =
save a, dt;
dt = arclength P / (N+1);
for i=1 upto N:
a := arctime dt*i of P;
draw (down--up) scaled 2
rotated angle direction a of P
shifted point a of P _t;
endfor
enddef;
beginfig(1);

path a, b;

a = origin -- (120,40);
b = (0,30) .. (80, 60) .. (120,50);

draw_n_marks_along(a,3);
draw_n_marks_along(b,12) withcolor blue;

drawarrow a;
drawarrow b;

endfig;
end.


It should work for arbitrary paths. • maybe very stupid of me but never heard of metapost :-( Dec 28, 2015 at 15:08
• Certainly not stupid! But see here for more. Dec 28, 2015 at 15:47
• I think I will keep up with tikz and pgfplots :-) Dec 28, 2015 at 16:47