Dưới đây là một số code sử dụng kỹ thuật Đệ quy trong gói vẽ hình TikZ của LaTeX.
Tác giả: Nguyễn Quang Dũng
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\def\hv(#1,#2){
\ifnum #1= 0\else
\fill[cyan,draw=magenta](0,0) arc(180:90:4)--(4,0)--cycle;
\draw(0,0)rectangle(4,4);
\pgfmathtruncatemacro{\k}{#1-1}
\begin{scope}[shift={(45:4*sqrt(2))},rotate=#2,scale={(sqrt(5)-1)/2}]
\hv(\k,#2)
\end{scope}
\fi
}
\foreach \i in{-90,-85,...,90}{
\begin{tikzpicture}[magenta]
\clip(-1,-1)rectangle(12,12);
\hv(20,-\i)
\end{tikzpicture}
}
\end{document}
Tác giả: Nguyễn Quang Dũng
\documentclass[tikz,border=5mm]{standalone}
\pagecolor{black}
\def\r{4}
\begin{document}
\def\hng(#1,#2){
\ifnum #2=0
\draw(18:\r)--(90:\r)--(162:\r)--(234:\r)--(306:\r)--cycle;
\else
\draw(18:\r)--(90:\r)--(162:\r)--(234:\r)--(306:\r)--cycle;
\pgfmathtruncatemacro{\k}{#2-1}
\begin{scope}[rotate=#1,scale=abs(sin(54)/sin(126-#1))]
\hng(#1,\k)
\end{scope}
\fi
}
\foreach \m in{0,1,...,34,35,34,...,0}{
\begin{tikzpicture}
\pgfmathsetmacro{\l}{2*\r*cos(36)}
\foreach \x/\d/\mau in {0/\l/green,72/\l/green,144/\l/green,216/\l/green,288/\l/green}{
\begin{scope}[\mau,shift={(\x:\d)},rotate=126]
\hng(5,\m)
\end{scope}
\begin{scope}[orange,rotate=18]
\hng(5,\m)
\end{scope}
}
\end{tikzpicture}
}
\end{document}
Tác giả: Nguyễn Quang Dũng
\documentclass[tikz, border=10mm]{standalone}
\usepackage[utf8]{vietnam}
\newcommand{\hoa}[1]{
\ifnum #1=1
\else
\begin{scope}[yellow]
\draw(0,0)--(0,8);
\end{scope}
\pgfmathtruncatemacro{\k}{#1-1}
\foreach \i in{1,2,3}{
\begin{scope}[shift={(0,8)},rotate=120*\i,scale=1/2]
\hoa{\k}
\end{scope}}
\fi
}
\pagecolor{red}
\begin{document}
\begin{tikzpicture}
\foreach \g in{0,120,240}{
\begin{scope}[rotate=\g]
\hoa{9}
\end{scope}
}
\end{tikzpicture}
\end{document}
Tác giả: Nguyễn Quang Dũng
\documentclass[tikz, border=5mm]{standalone}
\def\r{2}
\def\n{12}
\pgfmathsetmacro{\g}{180/\n}
\pgfmathsetmacro{\x}{sqrt(2)*\r*sin(\g)}
\newcommand{\hv}[1]{
\ifnum #1=0
\else
\begin{scope}
\foreach \j in {0,30,...,360}{
\fill[cyan!40, draw=magenta,rotate =\j](\r,0)--++(60:\x)--++(150:\x)--++(240:\x)--cycle;
}
\end{scope}
\pgfmathtruncatemacro{\k}{#1-1}
\begin{scope}[rotate=15,scale=1/(sqrt(2))]
\hv{\k}
\end{scope}
\fi
}
\begin{document}
\begin{tikzpicture}
\hv{10}
\end{tikzpicture}
\end{document}
Tác giả: Bùi Quỹ
\documentclass[tikz,border=2mm]{standalone}
\usepackage[utf8]{vietnam}
\begin{document}
\newcommand{\sao}[1]{
\ifnum#1=0
\else
\begin{scope}[join=round,cap=round,ultra thin,brown]
\draw
(90:1)--(234:1)--(18:1)--(162:1)--(306:1)--cycle
(90:1)--(162:1)--(234:1)--(306:1)--(18:1)--cycle
;
\end{scope}
\pgfmathtruncatemacro{\k}{#1-1}
\pgfmathsetmacro{\tile}{2/(1+sqrt(5))}
\pgfmathsetmacro{\tilenguoc}{1-\tile}
\foreach \i in {0,...,4}{
\begin{scope}[shift={(18+72*\i:\tile)},scale=\tilenguoc]
\sao{\k}
\end{scope}
\begin{scope}[shift={(-18+72*\i:\tile)},scale=\tile *\tilenguoc]
\sao{\k}
\end{scope}
}
\fi
}
\begin{tikzpicture}
\sao{4}
\end{tikzpicture}
\end{document}
Tác giả: Hoàng Hải
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\newcommand{\hv}[2][20]{
\ifnum#2=0\else
\pgfmathsetmacro{\R}{rnd*255}
\pgfmathsetmacro{\G}{rnd*255}
\pgfmathsetmacro{\B}{rnd*255}
\definecolor{mau}{RGB}{\R,\G,\B}
\draw[fill=mau] (-1,-1) rectangle (1,1);
\pgfmathtruncatemacro{\k}{#2-1}
\begin{scope}[rotate=#1,scale={1/(sin(#1)+cos(#1))}]
\hv[#1]{\k}
\end{scope}
\fi
}
\tikz{\hv[20]{10}}
\end{document}
Tác giả: Nguyễn Quang Dũng
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\def\hv(#1){
\def\a{3}
\ifnum #1= 0 \else
\fill[blue] (-\a,0)--(0,0)--([turn]120:\a)--cycle;
\fill[orange] (0,0)--(\a,0)--([turn]120:\a)--cycle;
\pgfmathtruncatemacro{\k}{#1-1}
\begin{scope}[shift={(0,{\a*0.5*sqrt(3)})},scale=0.5]
\hv(\k)
\end{scope}
\fi
}
\begin{tikzpicture}
\hv(20)
\end{tikzpicture}
\end{document}
Tác giả: Nguyễn Quang Dũng
\documentclass[tikz,border=2mm]{standalone}
\pagecolor{yellow!20}
\begin{document}
\begin{tikzpicture}
\def\hv(#1){
\ifnum #1=1 \else
\draw[magenta](0,0)rectangle(21,21)(0,0) arc(180:90:21);
\pgfmathtruncatemacro{\k}{#1-1}
\begin{scope}[shift={(45:21*sqrt(2))},rotate=-90,scale={(sqrt(5)-1)/2}]
\hv(\k)
\pgfmathsetmacro{\n}{int((1/(sqrt(5))*(((1+sqrt(5))/2)^(\k-1)-(1-(sqrt(5))/2)^(\k-1)+1)}
\path(21/2,21/2)circle(1.25cm) node{\color{red}$\n$};
\end{scope}
\fi
}
\hv(15)
\end{tikzpicture}
\end{document}
Tác giả: Nguyễn Quang Dũng
\begin{document}
\begin{tikzpicture}
\def\r{4}
\newcommand{\htr}[2][]{
\ifnum#2=0
\draw[red](-\r,0)--(\r,0);
\else
\draw(\r,0)arc(0:180:\r);
\pgfmathtruncatemacro{\i}{#2-1}
\foreach \x/\mau in {-\r*0.5/green,\r*0.5/magenta}{
\begin{scope}[\mau,shift={(\x,0)},scale=1/2]
\htr{\i}
\end{scope}
}
\fi
}
\htr{6}
\end{tikzpicture}
\end{document}
Tác giả: Nguyễn Quang Dũng
\documentclass[tikz,border=2cm]{standalone}
\pgfmathsetmacro{\a}{3*sin(4.5)}
\def\b{\a/3}
\def\r{3}
\pgfmathsetmacro{\g}{9}
\def\hv(#1){
\ifnum #1=0
\else
\foreach \i in{0,1,...,40}{
\begin{scope}[rotate=\g*\i]
\fill[black](0:\r)--++(180:2*\a)--++(90:\a)--++(0:2*\a)--cycle;
\fill[yellow](0:\r)arc(0:360:{\a} and {\b});
\fill[pink!60!red](0:\r)++(90:\a)arc(0:360:{\a} and {\b});
\end{scope}
}
\pgfmathtruncatemacro{\k}{#1-1}
\pgfmathsetmacro{\y}{(\r-2*\a)/\r}
\begin{scope}[scale=\y,rotate=\g/2]
\hv(\k)
\end{scope}
\fi
}
\begin{document}
\begin{tikzpicture}[line join=round, line cap=round]
\foreach \x/\y in{6/0,0/0,12/0,0/6,6/6,12/6}{
\begin{scope}[shift={(\x,\y)}]
\hv(25)
\end{scope}
}
\end{tikzpicture}
\end{document}
Tác giả: Hoàng Hải
\documentclass[tikz,border=1mm]{standalone}
\pagecolor{black}
\begin{document}
\begin{tikzpicture}
\def\hv#1{
\ifnum#1>0
\fill[white] (-135:1) rectangle (45:1);
\pgfmathtruncatemacro{\k}{#1-1}
\foreach \i in {0,...,3}{\begin{scope}[shift={(90*\i:3/2)},scale=1/3]\hv{\k}\end{scope}}
\foreach \i in {0,...,3}{\begin{scope}[shift={(45+90*\i:{3/sqrt(2)})},scale=1/3]\hv{\k}\end{scope}}
\fi
}
\hv{4}
\end{tikzpicture}
\end{document}
Tác giả: Hoàng Hải
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\newcommand{\hv}[3][]{
\ifnum#2>0
\path[#1] (-1,-1) rectangle (1,1);
\pgfmathtruncatemacro{\k}{#2-1}
\foreach \i in {0,1,2,3}{
\pgfmathparse{(90*\i+45-#3)-180 && (90*\i+45-#3)+180}
\ifnum\pgfmathresult=1
\pgfmathsetmacro{\g}{90*\i+45}
\begin{scope}[shift={(\g:{1.5*sqrt(2)})},scale=1/2]
\hv[#1]{\k}{\g}
\end{scope}
\fi
}
\fi
}
\hv[fill=cyan]{7}{0}
\end{tikzpicture}
\end{document}
0 Nhận xét