\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{calc}
\usepackage[active,pdftex,tightpage]{preview}
\PreviewEnvironment[]{tikzpicture}


% General IFS calculator
%
% parameters of function IFS:
%  - deep: how many steps we want to make
%  - number of functions
%  - n: 0<= n < (number of functions)^deep
%  - functions
%
% It also uses \x and \y as the coordinates of the original point.
\newlength\x \newlength\y \newlength\sx \newlength\sy
\newcount\ifst \newcount\ifsr \newcount\ifsn 
\newcommand{\IFS}[4]{
	\ifnum #1>0
		\ifsn=#1
		\ifst=#3
		\ifsr=#3
		\ifsn=#1
		\divide\ifst by #2
		\multiply\ifst by #2
		\advance\ifsr by-\ifst
		#4{\ifsr}
		\divide\ifst by #2
		\ifnum #1>1
			\advance\ifsn by-1
			\IFS{\ifsn}{#2}{\ifst}{#4}
		\fi
	\fi
}

% In the following functions be careful. Use
% \x*2 instead of 2*\x (because \x is a correct length, for example 2cm),
% 2cm*2 instead of 2*2cm,
% \x*\real{0.2} instead of \x*0.2.

% The iterated function system of the Sierpinski gasket
\newcommand{\sier}[1]{
        \ifcase #1
	\setlength\x{\x/2} \setlength\y{\y/2} \or 
	\setlength\x{\x/2+0.5cm} \setlength\y{\y/2} \or 
	\setlength\x{\x/2+0.25cm} \setlength\y{\y/2+0.4330127018cm} %0.43301 = sqrt(3)/4
	\fi
}

% The iterated function system of the Koch curve
\newcommand{\koch}[1]{
        \ifcase #1
        \setlength\sx{\x/3} \setlength\sy{\y/3} \or 
	\setlength\sx{\x/6-\y*\real{0.288675}+1cm/3} \setlength\sy{\y/6+\x*\real{0.288675}} \or 
	\setlength\sx{\x/6+\y*\real{0.288675}+0.5cm} \setlength\sy{\y/6-\x*\real{0.288675}+0.288675cm} \or 
	\setlength\sx{\x/3+2cm/3} \setlength\sy{\y/3}
        \fi
        \setlength\x{\sx}
        \setlength\y{\sy}
}

% The iterated function system of the Cantor set
\newcommand{\cantor}[1]{
        \ifcase #1
                \setlength\x{\x/3} \or
		\setlength\x{\x/3+2cm/3}
        \fi
}

\begin{document}
       \begin{tikzpicture}[scale=10]

% Comment or uncomment the following parts. Choose \N as you want, and set \Nall accordingly.

	 %Sierpinski gasket
	  \def\N{3}
	  \def\Nall{26} %= 3^(\N)-1
		\foreach \n in {0,...,\Nall}
		{
			\setlength\x{0cm} \setlength\y{0cm} \IFS{\N}{3}{\n}{\sier} \edef\xa{\the\x} \edef\ya{\the\y}
			\setlength\x{1cm} \setlength\y{0cm} \IFS{\N}{3}{\n}{\sier} \edef\xb{\the\x} \edef\yb{\the\y}
			\setlength\x{0.5cm} \setlength\y{0.8660254037cm} \IFS{\N}{3}{\n}{\sier} \edef\xc{\the\x} \edef\yc{\the\y}
			\draw[fill=black] (\xa,\ya) -- (\xb,\yb) -- (\xc,\yc);
		};

	%Koch curve
%	  \def\N{5}
%	  \def\Nall{1023} %= 4^(\N)-1
%		\foreach \n in {0,...,\Nall}
%		{
%			\setlength\x{0cm} \setlength\y{0cm} \IFS{\N}{4}{\n}{\koch} \edef\xa{\the\x} \edef\ya{\the\y}
%			\setlength\x{1cm} \setlength\y{0cm} \IFS{\N}{4}{\n}{\koch} \edef\xb{\the\x} \edef\yb{\the\y}
%			\draw (\xa,\ya) -- (\xb,\yb);
%		};

	%Cantor set
%	  \def\N{5}
%	  \def\Nall{31} %= 2^(\N)-1
%		\foreach \n in {0,...,\Nall}
%		{
%			\setlength\x{0cm} \setlength\y{0cm} \IFS{\N}{2}{\n}{\cantor} \edef\xa{\the\x} \edef\ya{\the\y}
%			\setlength\x{1cm} \setlength\y{0cm} \IFS{\N}{2}{\n}{\cantor} \edef\xb{\the\x} \edef\yb{\the\y}
%			\draw[thick] (\xa,0) -- (\xb,0);
%		};
        \end{tikzpicture}
\end{document}

