# tikz loops, creating triangle with jagged hypotenuse

I am in dire need of constructing the image below. In short I am trying to create an exact replica of the image, and an alternative image, with a much more jagged hypotenuse.

I thought about using a for loop in latex, but being as inexperienced as me, this proved difficult. My intital progress has been shown below

\documentclass[10pt,a4paper]{minimal}
\usepackage{tikz,tkz-euclide}
\usepackage{mathtools,amssymb,amsmath}
\begin{document}

\begin{tikzpicture}[scale=4]
\def \a{5}
\tkzDefPoint(0,0){O} \tkzDefPoint(1,0){A} \tkzDefPoint(1,1){B}
\tkzDrawSegments(O,A A,B)
\tkzLabelSegment[below](O,A){1} \tkzLabelSegment[right](A,B){1}
\tkzLabelPoints[below left](O) \tkzLabelPoints[below right](A) \tkzLabelPoints[above right](B)
\foreach \i in {0,...,\a}{
\tkzDrawSegment( ( \i:\a, \i:\a ),( \i:\a,(\i+1):\a ) )}
\end{tikzpicture}
%\tkzDrawSegment( ( \i:\a,(\i+1):\a ),((\i+1):\a,(\i+1):\a))

\end{document}


Any help would be greatly appreciated. My problem is creating the for loop. I just can not get it right, I think the problem is latex evaluating 0/4 and 1/3 as symbols not numbers

-

I speak tkz but the problem comes from bad tikz's syntax. You can divide numbers with /and not with :. Then with tkz or with tikz, you need sometimes to hide (...) and the separator ,. Here if you want to use coordinates (a,b) and (c,d) inside a tkz's macro, you need to write

 \tkzDrawSegment({a,b},{c,d})


 \tkzDrawSegment((a,b),(c,d)) % The parser don't understand in this case.


Now, I think it's more easy to use shift to get the 'jagged hypotenuse'. Like other answers, you can use tikz to draw the jagged hypotenuse or tkz like me.

\documentclass[10pt,a4paper]{minimal}
\usepackage{tikz,tkz-euclide}
\usepackage{mathtools,amssymb,amsmath}
\begin{document}

\begin{tikzpicture}[scale=4]
\def \a{5}
\tkzDefPoint(0,0){O} \tkzDefPoint(1,0){A} \tkzDefPoint(1,1){B}
\tkzDrawSegments(O,A A,B)
\tkzLabelSegment[below](O,A){1} \tkzLabelSegment[right](A,B){1}
\tkzLabelPoints[below left](O) \tkzLabelPoints[below right](A) \tkzLabelPoints[above right](B)
\pgfmathsetmacro{\endl}{\a-1}
\foreach \i in {0,...,\endl}{
\begin{scope}[shift={(\i/\a,\i/\a)}]
\tkzDrawSegment({0,0},{0,1/5})
\tkzDrawSegment({0,1/5},{1/5,1/5})
% or \tkzDrawSegments({0,0},{0,1/5} {0,1/5},{1/5,1/5})
\tkzLabelSegment[left]({0,0},{0,1/5}){$\frac{1}{\a}$}
\end{scope}
}
\end{tikzpicture}
\end{document}


-
You can avoid one step with \tkzDrawSegments({0,0},{0,1/5} {0,1/5},{1/5,1/5}). Here there are two separators , for the coordinates and a blank space between the points. –  Alain Matthes Jun 6 '12 at 6:48
Very good answers thanks alot! I will accept the other one though, because it is a tad closer. But I learned much from this. Thanks =) –  N3buchadnezzar Jun 6 '12 at 7:37
Oh, and to "complete" your figure, I changed the lines into \tkzDrawSegments[green!50!black,thick]({0,0},{0,1/\a} {0,1/\a},{1/\a,1/\a}) \tkzDrawSegments[green!50!black,dashed]({0,0},{1-\i/\a,0} {0,0},{0,-\i/\a}) That did the trick =) –  N3buchadnezzar Jun 6 '12 at 8:11

Something is wrong with my use of the foreach macro key (initially 0). It gets stuck on the first point so I had to define it before the loop. The following works without too much emphasis on the cosmetics. The nodes A,B,C etc. can be added easily.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=3]
\def\lastx{0} % I don't know what the problem is with (initially 0) key
\def\divno{8} % Division number
\pgfmathsetmacro\initpoint{1/\divno}
\pgfmathsetmacro\incr{2/\divno}
\draw (0,0) -| (1,1);
\foreach \x [remember=\x as \lastx ]in {\initpoint,\incr,...,1}{
\draw (\x , \x)  -| ( \lastx , \lastx);
\draw[dashed] (\x , \x)   -| ( \x ,0);
\draw[dashed] (\x , \x)  -- ( 1,\x);
\node[left] at ([yshift=0.25*\incr cm]\lastx,\lastx) {$\frac{1}{\divno}$};
}
\end{tikzpicture}
\end{document}


Examples are for 8 and 5

-

I don't speak TkZ, but here is another pretty straightforward TikZ implementation (note that I change the x and y lengths instead of scaling as this usually leads to nicer results):

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[x=4cm, y=4cm]
\def\steps{5}
\pgfmathsetmacro\stepsize{1/\steps}

\draw (0,0)
node[below] {$A$}
-- node[below] {$1$}
(1,0) node[below right] {$B$}
-- node[right] {$1$}
(1,1) node[right] {$C$};

\foreach \i in {1,2,...,\steps} {
\draw [green!50!black,thick]
({(\i-1)*\stepsize},{(\i-1)*\stepsize})
-- node[left] {\normalcolor$\frac{1}{\steps}$}
({(\i-1)*\stepsize}, {\i*\stepsize})
-- ({\i*\stepsize}, {\i*\stepsize});
}
\pgfmathsetmacro\steps{int(\steps-1)}
\foreach \i in {1,2,...,\steps} {
\draw [green!50!black,dashed]
({\i*\stepsize}, {\i*\stepsize})
edge (1,{\i*\stepsize})
edge ({\i*\stepsize}, 0);
}
\end{tikzpicture}
\end{document}


-

Here another answer only with tikz to get a jagged hypothenuse

\documentclass[10pt,a4paper]{minimal}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[scale=4]
\def \a{5}
\foreach \i in {0,...,\number\numexpr\a-1\relax}{
\draw[shift={(\i/\a,\i/\a)}]  (0,0) -- (0,1/\a) -- (1/\a,1/\a);
}
\end{tikzpicture}
\end{document}


Here the picture with 5 and 10 for \a

-

\documentclass[10pt,a4paper]{minimal}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[scale=4]
\draw[color=blue] plot [domain=0:1,samples=6,const plot mark right,mark= ,] (\x,\x) coordinate(end);
\draw (0,0) -| (end);
\end{tikzpicture}
\end{document}


-