代码改变世界

如何利用Latex绘制圣诞树

2011-12-25 11:14  Haippy  阅读(1738)  评论(0编辑  收藏  举报

圣诞节到了,凑个热闹,国外的Latex牛人画的圣诞树,使用了tikz包。

1、带阴影的圣诞树

% A Christmas tree
% By Mikko Heiskanen, 24.12.2011
% http://tex.stackexchange.com/a/39235/
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{scopes,svg.path,shapes.geometric,shadows}
\begin{document}
\begin{tikzpicture}[
mystar/.style={star, minimum size=2cm, star point ratio=2.5, shade, thick,
line join=round, color=yellow!80!black, draw=red!20!black,
top color=yellow!80!white, bottom color=yellow!60!black},
mytree/.style={scale=0.5, rotate=180, draw=green!60!black, thick,
line join=round, inner color=green!60!yellow, outer color=green!50!black},
myball/.style={shade, ball color=#1, circular drop shadow={
shadow xshift=0pt, shadow yshift=-.5ex, fill=green!40!black}}
]
{[mytree]
\shadedraw svg "M355,430
q90,10 105,-85 30,0 50,-30 20,30 50,30 50,-20 100,0 10,88 105,85
-45,90 -205,25 Q400,520 355,430";
\shadedraw svg "M380,325
q83,10 105,-80 25,0 35,-30 20,25 40,30 20,-10 35,-25 20,20 40,25
25,90 105,82 -15,50 -120,15 -30,-2 -60,12 -30,0 -52,-28
C490,370 380,360 380,325";
\shadedraw svg "M435,225
q65,-8 90,-70 35,40 70,0 25,60 90,70 -30,52 -90,5 -36,48 -73,-3
C520,254 445,265 435,225";
\shadedraw svg "M470,139
q50,5 90,-80 50,90 90,80 -30,30 -50,20 -40,45 -78,0
Q500,170 470,139";
}
%\shadedraw svg[scale=0.5,rotate=180]
%"M460,532 q50,-8 q77,-45 v-20 a20,13 0 1 1 48,0 v20 q30,40 77,45";
%pgf/tikz doesn't like the arc operation, as stated in manual

\node[mystar] at (-9.85,-1) {$\lambda$};

\shade[myball=blue] (-9.7,-2.2) circle (.2cm);
\shade[myball=red] (-9.2,-3.8) circle (.2cm);
\shade[myball=green] (-10.3,-4) circle (.4cm);
\shade[myball=yellow] (-8.95,-5.4) circle (.4cm);
\shade[myball=red] (-10.7,-6.1) circle (.4cm);
\shade[myball=blue] (-10.8,-5) circle (.2cm);
\shade[myball=yellow] (-9.5,-6.7) circle (.2cm);
\shade[myball=green] (-8.3,-7.6) circle (.4cm);
\shade[myball=yellow] (-11.7,-7.6) circle (.4cm);
\shade[myball=blue] (-10.5,-7.8) circle (.2cm);
\end{tikzpicture}
\end{document}

 

2、更加逼真的圣诞树

 

% A Christmas tree
% Decorations by Andrew Stacey
% Based on a fractal tree (L-System) drawn in TikZ by Stefan Kottwitz
% Tree example from the book "The Science of fractal images" by Peitgen and Saupe.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{%
lindenmayersystems,
decorations.pathmorphing,
decorations.markings,
shapes.geometric,
calc%
}
\tikzset{
tinsel/.style={
#1,
rounded corners=10mm,
ultra thin,
decorate,
decoration={
snake,
amplitude=.1mm,
segment length=10,
}
},
baubles/.style={
decorate,
decoration={
markings,
mark=between positions .3 and 1 step 2cm
with
{
\pgfmathsetmacro{\brad}{2 + .5 * rand}
\path[shading=ball,ball color=#1] (0,0) circle[radius=\brad mm];
}
}
},
lights/.style={
decorate,
decoration={
markings,
mark=between positions 0 and 1 step 1cm
with
{
\pgfmathparse{rand > 0 ? "dart" : "kite"}
\let\lshape\pgfmathresult
\pgfmathsetmacro{\tint}{100*rnd}
\node[rotate=90,\lshape,shading=ball,inner sep=1pt,ball color=red!\tint!yellow] {};
}
}
}
}

\begin{document}
\begin{tikzpicture}
\coordinate (star) at (0,-1);
\path (star) +(-50:7) coordinate (rhs) +(-130:7) coordinate (lhs);
\draw[brown!50!black,line width=5mm,line cap=round] (star) ++(-90:6.8) -- ++(0,-1) coordinate (base);
\node[scale=-1,trapezium,fill=black,minimum size=1cm] at (base) {};
\foreach \height/\colour in {%
.2/blue,
.4/yellow,
.6/red,
.8/orange,
1/pink%
} {
\draw[tinsel=\colour] ($(star)!\height!(lhs)$) to[bend right] ($(star)!\height!(rhs)$);
}
\path (star);
\pgfgetlastxy{\starx}{\stary}
\begin{scope}[xshift=\starx,yshift=\stary,yshift=-7cm]
\draw[color=green!50!black, l-system={rule set={S -> [+++G][---G]TS, G -> +H[-G]L, H -> -G[+H]L, T -> TL, L -> [-FFF][+FFF]F}, step=4pt, angle=18, axiom=+++++SLFFF, order=11}] lindenmayer system -- cycle;
\end{scope}
\foreach \height/\colour in {%
.1/pink,
.3/red,
.5/yellow,
.7/blue,
.9/orange%
} {
\draw[tinsel=\colour] ($(star)!\height!(lhs)$) to[bend right] ($(star)!\height!(rhs)$);
}
\foreach \height in {.15,.35,...,1} {
\draw[lights] ($(star)!\height!(lhs)$) to[bend right] ($(star)!\height!(rhs)$);
}
\foreach \angle/\colour in {
-50/red,
-70/yellow,
-90/blue,
-110/pink,
-130/purple%
} {
\draw[baubles=\colour] (star) -- ++(\angle:7);
}
\node[star,star point ratio=2.5,fill=yellow,minimum size=1cm] at (star) {};
\end{tikzpicture}
\end{document}

3、带有雪花,糖果的圣诞树

% A Christmas tree
% By Alain Matthes
\documentclass[11pt]{article}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{%
shapes,
decorations.shapes,
decorations.fractals,
decorations.markings,
shadows
}

\newsavebox{\mycandle}
\savebox{\mycandle}{
\begin{tikzpicture}[scale=.1]
\shade[top color=yellow,bottom color=red] (0,0) .. controls (1,.2) and (1,.5) .. (0,2) .. controls (-1,.5) and (-1,.2) .. (0,0);
\fill[yellow!90!black] (.8,0) rectangle (-.8,-5);
\end{tikzpicture} }
\tikzset{
paint/.style={draw=#1!50!black, fill=#1!50},
my star/.style={decorate,decoration={shape backgrounds,shape=star},
star points=#1}
}

\begin{document}
\begin{tikzpicture}[ ball red/.style={
decorate,
decoration={
markings,
mark=between positions .2 and 1 step 3cm
with
{
\pgfmathsetmacro{\sz}{2 + .5 * rand}
\path[shading=ball,ball color=red] (0,0) circle[radius=\sz mm];
}
}
} ,ball blue/.style={
decorate,
decoration={
markings,
mark=between positions 0.1 and .9 step 3cm
with
{
\pgfmathsetmacro{\sz}{2 + .5 * rand}
\path[shading=ball,ball color=blue] (0,0) circle[radius=\sz mm];
}
}
}
]

\draw[fill=Maroon,ultra thick]
(.75,-1) .. controls (.5,.5) and (.5,3) .. (0.5,4)
-- (-0.5,4) .. controls (-.5,3) and (-.5,.5) .. (-.75,-1) ;
\draw[ultra thick,fill=green!50!black]
(0,10) .. controls (0,8) and (1,7) .. (1.5,7)
.. controls (1,7) and (1,7) .. (0.5,7.25)
.. controls (1.5,5) and (2.5,4) .. (3,4)
.. controls (2,4) and (1.25,4) .. (1,4.5)
.. controls (2,2) and (3.5,2) .. (4,2)
.. controls (1,1) and (-1,1) .. (-4,2)
.. controls (-3.5,2) and (-2,2) .. (-1,4.5)
.. controls (-1.25,4) and (-2,4) .. (-3,4)
.. controls (-2.5,4) and (-1.5,5) .. (-0.5,7.25)
.. controls (-1,7) and (-1,7) .. (-1.5,7)
.. controls (-1,7) and (0,8) .. (0,10)
;

\foreach \candle in {(2,5),(-2,5),(0.5,7.5),(-0.5,7.5),(-3,2.5), (3,2.5),
(1.5,1.75),(-1.5,1.75)}
\node at \candle {\usebox{\mycandle}} ;
\node [star, star point height=.5cm, minimum size=.5cm,draw,fill=yellow,thick]
at (0,10) {};
\begin{scope}[decoration={shape sep=.2cm, shape size=.25cm}]
\draw [my star=6, paint=red] (-4,2)
.. controls (0,2) and (1,3.5) .. (1,4.40);
\draw [my star=6, paint=red] (-1.5,5.40)
.. controls (0,5.40) and (0.5,6.5) .. (0.5,7);
\draw [my star=6, paint=blue] (4,2)
.. controls (0,2) and (-1,3.5) .. (-1,4.40);
\draw [my star=6, paint=blue] (1.5,5.40)
.. controls (0,5.40) and (-0.5,6.5) .. (-0.5,7);
\end{scope}
% the balls
\path[ball red]
(0,10) .. controls (0,8) and (1,7) .. (1.5,7)
.. controls (1,7) and (1,7) .. (0.5,7.25)
.. controls (1.5,5) and (2.5,4) .. (3,4)
.. controls (2,4) and (1.25,4) .. (1,4.5)
.. controls (2,2) and (3.5,2) .. (4,2)
.. controls (1,1) and (-1,1) .. (-4,2)
.. controls (-3.5,2) and (-2,2) .. (-1,4.5)
.. controls (-1.25,4) and (-2,4) .. (-3,4)
.. controls (-2.5,4) and (-1.5,5) .. (-0.5,7.25)
.. controls (-1,7) and (-1,7) .. (-1.5,7)
.. controls (-1,7) and (0,8) .. (0,10)
;
\path[ball blue]
(0,10) .. controls (0,8) and (1,7) .. (1.5,7)
.. controls (1,7) and (1,7) .. (0.5,7.25)
.. controls (1.5,5) and (2.5,4) .. (3,4)
.. controls (2,4) and (1.25,4) .. (1,4.5)
.. controls (2,2) and (3.5,2) .. (4,2)
.. controls (1,1) and (-1,1) .. (-4,2)
.. controls (-3.5,2) and (-2,2) .. (-1,4.5)
.. controls (-1.25,4) and (-2,4) .. (-3,4)
.. controls (-2.5,4) and (-1.5,5) .. (-0.5,7.25)
.. controls (-1,7) and (-1,7) .. (-1.5,7)
.. controls (-1,7) and (0,8) .. (0,10)
;
% the snow
\foreach \i in {0.5,0.6,...,1.6}
\fill [white!80!blue,decoration=Koch snowflake,opacity=.9]
[shift={(rand*5,rnd*8)},scale=\i]
[double copy shadow={opacity=0.2,shadow xshift=0pt,
shadow yshift=3*\i pt,fill=white,draw=none}]
decorate {
decorate {
decorate {
(0,0) -- ++(60:1) -- ++(-60:1) -- cycle
}
}
};
\end{tikzpicture}

\end{document}

参考来源

http://www.texample.net/tikz/examples/christmas-tree-1/

http://www.texample.net/tikz/examples/christmas-tree-2/

http://www.texample.net/tikz/examples/christmas-tree-3/