I'd like to wish all my readers a Merry Christmas 2016- R style! Behold my 3d Christmas tree created using the <code>plot3D</code> R package:<br><br><br><div align="center"> <br></div><div align="justify">While this might seem like yet another Christmas decoration done in R, it is unique in that the tree is rendered in 3d perspective. I myself wrote some code for a 2d Christmas tree a while ago (see this <a target href="https://unamatematicaseltigre.blogspot.com/2013/12/feliz-navidad-en.html">entry in Spanish</a>), in turn based on the code in the <a target="_blank" href="http://wiekvoet.blogspot.com/2013/12/merry-christmas.html">Wiekvoet blog</a>. This time around though, I took it a bit further- the challenge now being to create a 3d model of a Christmas tree that I could rotate and animate in perspective. Heck, if others with enough leisure/boredom time to spare were to join in and showcase their own Christmas R creations for this time of the year, we might be witnessing the start of a new Christmas tradition for all R geekdom across the planet.<br><br></div><h2>Mathematical model of a Christmas tree</h2><div align="justify">Long before the birth of Jesus, most pagan cultures in Europe were sun worshipers and in December they celebrated the rebirth of the sun and the rich harvests. This is the significance of the Christmas tree in these festivities of fire and light, abounding in evergreens and holly branches. In Spanish America, instead of Christmas trees, people would place Nativity scenes (<i>Nacimientos</i>) in their homes, but with globalization and the prevalence of the Anglo-Saxon culture, the Christmas tree eventually found its way into our homes. At any rate, I for one have always found the lights and the bright decorations of the Christmas tree captivating and reminiscent of happier moments when friends are remembered and grudges are forgotten. So in a sort of weird tribute to the spirit of these festivities, I decided I would make a mathematical model of the Christmas tree. How geeky is that?<br><br></div><div align="justify">Mathematically, a Christmas tree is nothing but a fractal. A fractal consisting of a stub or segment of a tree trunk with two or three branches that separate outward from the trunk, and an extension, or outward projection of the trunk in the same direction as the trunk. The branches can in turn be considered as trunk segments, each comprising more branches and an extension outward in the same direction. The extension or outward projection of a trunk is in turn another stub with more branching off points and so on. Consequently, the tree construction process is recursive. Being a descendant of LISP, base R supports the list as the data structure of choice for implementing this recursive definition of a tree.<br><br></div><div align="justify">There are, to be sure, some considerations to be made so that this recursively-built tree ends up looking like a pine tree and not a palm tree, a cherry blossom tree, a mango tree or a bonzai tree. For one thing, pine trees are characteristically conical. This means that the angle of separation of the branches from the trunk is initially larger but &quot;closes up&quot; as the pine tree extends out or up. To control the degree of &quot;outwardness&quot; or &quot;upness&quot; associated to a particular tree-trunk stub there will be a depth parameter. Initially, the pine tree starts as one little green trunk stub of depth 1. As this stub branches off and extends upward, each of the children stubs has a lower depth than the parent stub. The most outward branches and leafs of the tree have a depth of almost 0. This will be useful for the dressing the tree with lights and decorations, as these lights are usually found on the more superficial or outward parts of the tree.<br><br></div><div align="justify">Another aspect of our tree growth model worth mentioning is how the tree trunk becomes thicker as the tree grows out an up. This is done via a width parameter that will eventually determine how thick the lines representing the trunk and the branches should be drawn. This width parameter also determines when the tree stem changes color from dark green to brown. In each growth cycle, the growth algorithm increases the width of each stub by a factor of 1.4, it then goes through the branches and the extension looking for the ending stubs (those whose branches and extensions are NULL or nonexistent). It then adds the branches and the extension and backtracks down to find the remaining end stubs. The width of the new branches and extensions is initialized to 1, but as can be seen, the width of the older stubs has been increased more.<br><br></div><div align="justify">The Christmas tree data structure is a list whose components are:<br><br></div><ul><li><div align="justify">The starting point of the trunk segment, given as a vector with coordinates in \(\mathbb{R}^3\) as \((x_0,y_0,z_0)\).<br></div></li> <li><div align="justify">The ending point of the trunk segment, as given by the vector with coordinates \((x_1,y_1,z_1)\). Together, the start and end points determine the direction vector of the tree stem as \(\vec{u}=(x_1-x_0,y_1-y_0,z_1-z_0)\). This direction vector will be useful for creating the extension stub, since the extension stub grows in the same direction as the parent stub. It is also used for determining where along the stub the branches start off and in what direction those branches are created.<br></div></li> <li><div align="justify">The width parameter <code>lwd</code> which is also the thickness with which the tree stem is drawn as a segment when plotted.<br></div></li> <li><div align="justify">The depth, which indicates how outward a branch or tree stem is, as already explained above.<br></div></li> <li><div align="justify">Slots for three branches and one extension (<code>branch1</code>, <code>branch2</code>, <code>branch3</code> and <code>extension</code>), which are nothing but lists recursively defined like this one. When a branch or extension is created, these slots are initialized to <code>NULL</code>.<br></div></li></ul><div align="justify">Creating the extension of a tree stem poses no major problem. One simply takes as starting point of the extension the ending point of the parent tree stem. The ending point of the extension is determined by adding to its starting point a multiple of the direction vector \(\vec{u}=(x_1-x_0,y_1-y_0,z_1-z_0)\). This scalar multiple is such that the resulting length of the extension stem is slightly shorter than that of the parent stem. The most difficult issue in creating the Christmas tree lies with the creation of the branches. How can we create the branches so that they branch out from the stem in apparently uniform angles around the stem and not have all the branches branch out from the same side of the stem? How can we obtain the direction vectors for those branches so that they extend outwards from the stem? A little trigonometry and vector geometry is of use to us here.<br><br></div><div align="justify">If the \(\vec{u}\) vector is the direction of the tree stem being considered, then what we need is a vector perpendicular to \(\vec{u}\) - let's call it \(\vec{v}\) - so that we can obtain the direction vector of the branch \(\vec{b}\) by adding \(\vec{u}+\vec{v}\) and then multiplying \(\vec{b}\) by a scalar to set its length to the desired magnitude (see Fig. 1). However, considering we're dealing with the \(\mathbb{R}^3\) metric space, there are infinitely many such vectors. In fact, there is an entire two-dimensional space - a plane \(\mathcal{V}\) comprised of vectors that are all perpendicular to $\vec{u}$ (see Fig. 2). So what we need to obtain $\vec{v}$ is a orthonormal base of two vectors \(\{\vec{v_1},\vec{v_2}\}\) that will permit us to express \(\vec{v}\) as a linear combination of the two base vectors of \(\mathcal{V}\).<br><br></div><div align="center"><div align="justify">Fig. 1 Geometrical relationship between the main tree stem (\(\vec{u}\)) and one of its branches (\(\vec{b}\)). \(\theta\) is the angle at which \(\vec{b}\) branches outward from the main stem.<br></div><br></div><div align="justify">This is easy enough. Two vectors are said to be orthogonal if their dot-product is zero. So if we have a vector, say \((x,y,z)\) all we need to do is pick any non-zero coordinate and switch it with one of the remaining coordinates, changing one of the signs to negative and setting the remaining third coordinate to zero. So for example \((-z,0,x)\), \((0,z,-y)\), and \((-y,x,0)\) are all perpendicular to \((x,y,z)\), provided they are all non-null vectors. In short, to find an orthonormal base for the plane perpendicular to a vector \(\vec{u}\), pick any non-zero component of this vector, pick a second component to interchange it with while changing the sign of any of the two and set the third component to zero. This will give you a perpendicular vector to \(\vec{u}\). To find the other perpendicular vector, just repeat the above process, but interchanging the chosen non-zero component of \(\vec{u}\) with the component you had set to zero for the first vector. Finally, once you have your two vectors \(\vec{v_1}\) and \(\vec{v_2}\), you must multiply them by the inverse of their norms to set their norms to one. The procedure is better illustrated in the R code on this post.<br><br></div><div align="center"><div align="justify">Fig. 2 Finding vector \(\vec{v}\) as a linear combination of an orthonormal base of perpendicular plane \(\mathcal{V}\) and then using it to find the direction vector \(\vec{b}\) of the branch.<br></div><br></div><div align="justify">So once you have the base of the perpendicular plane to a given tree stem \(\vec{u}\), all you need is two scalar components \(c_1\) and \(c_2\) to define the perpendicular vector $v$ used for constructing the branch, as a linear combination of the base. How are we to go about this? Well, let's think about what we really need. For some parts of the tree - the deeper parts - we need three branches to a stem. For the more outward parts, two branches to a stem will suffice. We want these branches to be evenly distributed about the stem: in the case of 3 branches, we would like the angle of separation between the branches to be \(120^\circ\) (like the Mercedes-Benz star symbol), in the case of 2 branches, an angle of separation between them of about \(180^\circ\pm 20^\circ\) should be adequate. <br><br></div><div align="justify">What this means is that we want to choose the \(\vec{v}\) vectors in \(\mathcal{V}\) to have the sort of layout shown in Fig. 3. This is to ensure the pine tree will have a nice conical shape and not have its branches all projecting to the same side.<br><br></div><div align="center"><table><tr><td></td><td></td></tr><tr><td>Fig. 3a 3 branch defining vectors</td><td>Fig. 3b 2 branch defining vectors</td></tr><tr><td colspan="2"><br>Fig. 3 Distribution of two and three branch generating vectors on \(\mathcal{V}\)</td></tr></table><br><br></div><div align="justify">Our problem now is to generate 2 or 3 vectors on a plane with such angles of separations. The good news is that we can define the vectors on the regular 2d plane with the canonical base \(\{\hat{i},\hat{j}\}\), and, because linear algebra is so cool, we can use the components of those vectors as \(c_1\) and \(c_2\) - the scalar coeficients used to define the \(\vec{v}\) vectors as linear combinations of \(\vec{v_1}\) and \(\vec{v_2}\). This is so because \(\{\vec{v_1},\vec{v_2}\}\) is an orthonormal base of a plane in the 3d space. So for example if the angle between \((0,1)\) and \((-\tfrac{1}{2},\tfrac{\sqrt{3}}{2})\) is \(120^\circ\), then the angle between \(\vec{v_2}\) and \(-\tfrac{1}{2}\vec{v_1} + \tfrac{\sqrt{3}}{2}\vec{v_2}\) will also be \(120^\circ\). Not only that, but the two vectors will lie on the \(\mathcal{V}\) plane in three dimensional space. Thus, we can translate the 2 dimensional vectors into vectors located on the \(\mathcal{V}\) plane perpendicular to the tree stem \(\vec{u}\). <br><br></div><div align="justify">Choosing the 2 or 3 vectors on the regular 2d plane later translating them as vectors on the $\mathcal{V}$ plane is a question of doing the following:<br><br></div><ul><li><div align="justify">2 branches: We first consider \((1,0)\) as the first vector and we determine the second unit-length vector by choosing a random angle between \(160^\circ\) and \(200^\circ\). We choose another random angle between \(0^\circ\) and \(360^\circ\) to rotate the entire set of two vectors.<br></div></li> <li><div align="justify">3 branches: Our three vectors will initially be \((1,0)\), \((\tfrac{\sqrt{3}}{2},-\tfrac{1}{2})\), and \((-\tfrac{\sqrt{3}}{2},-\tfrac{1}{2})\). We then choose a random angle between \(0^\circ\) and \(360^\circ\) to rotate the entire set of three vectors.<br></div></li></ul><div align="justify">The reason we subsequently rotate the two or three vector set is to randomize the orientation of the branches so that our trees branches will project in all sorts of different directions. In either case, to rotate a 2d vector by an angle of \(\theta\) about the origin, we use the following linear transformation:<br><br></div><div align="center">\[ (x,y)\quad\mapsto (x\,cos(\theta)-y\,sin(\theta),x\,sin(\theta)+y\,cos(\theta)) \] <br></div><div align="justify">There are a few remaining details to explain before we go into the R code. As we have already mentioned, the more superficial stems of the three have two branches whereas the deeper parts of the tree (those closest to the original thick stub from which the entire Christmas tree branches out) have three branches. This is controlled by the depth parameter. Whenever \(depth>0.8\), the tree stem will branch out into three branches, otherwise it will have only two. The depth parameter also controls where along the stem the branches are chosen. Stems of greater depth tend to branch off closer to its ending point \((x_1,y_1,z_1)\) whereas the more superficial stems branch off at points closer to its starting point \((x_0,y_0,z_0)\). Needless to say, the branching off points are chosen randomly. Finally, the placement of the lights and the tree decorations is also controlled by the depth parameter. The more superficial parts of the tree have a higher likelihood of containing lights and decorations. Again, the placement of these decorations is done stochastically.<br><br></div><h2>R script for the 3d Christmas tree</h2><div><table><tr><td><pre>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br>40<br>41<br>42<br>43<br>44<br>45<br>46<br>47<br>48<br>49<br>50<br>51<br>52<br>53<br>54<br>55<br>56<br>57<br>58<br>59<br>60<br>61<br>62<br>63<br>64<br>65<br>66<br>67<br>68<br>69<br>70<br>71<br>72<br>73<br>74<br>75<br>76<br>77<br>78<br>79<br>80<br>81<br>82<br>83<br>84<br>85<br>86<br>87<br>88<br>89<br>90<br>91<br>92<br>93<br>94<br>95<br>96<br>97<br>98<br>99<br>100<br>101<br>102<br>103<br>104<br>105<br>106<br>107<br>108<br>109<br>110<br>111<br>112<br>113<br>114<br>115<br>116<br>117<br>118<br>119<br>120<br>121<br>122<br>123<br>124<br>125<br>126<br>127<br>128<br>129<br>130<br>131<br>132<br>133<br>134<br>135<br>136<br>137<br>138<br>139<br>140<br>141<br>142<br>143<br>144<br>145<br>146<br>147<br>148<br>149<br>150<br>151<br>152<br>153<br>154<br>155<br>156<br>157<br>158<br>159<br>160<br>161<br>162<br>163<br>164<br>165<br>166<br>167<br>168<br>169<br>170<br>171<br>172<br>173<br>174<br>175<br>176<br>177<br>178<br>179<br>180<br>181<br>182<br>183<br>184<br>185<br>186<br>187<br>188<br>189<br>190<br>191<br>192<br>193<br>194<br>195<br>196<br>197<br>198<br>199<br>200<br>201<br>202<br>203<br>204<br>205<br>206<br>207<br>208<br>209<br>210<br>211<br>212<br>213<br>214<br>215<br>216<br>217<br>218<br>219<br>220<br>221<br>222<br>223<br>224<br>225<br>226<br>227<br></pre></td><td><pre><code><span>library</span>(plot3D)<br><br>trunk &lt;-<span> </span><span>list</span>(<br><span>x0=</span><span>0</span>, <span>y0=</span><span>0</span>, <span>z0=</span><span>0</span>,<br><span>x1=</span><span>0</span>, <span>y1=</span><span>0</span>, <span>z1=</span><span>6</span>,<br><span>extension=</span><span>NULL</span>,<br><span>branch1=</span><span>NULL</span>, <span>branch2=</span><span>NULL</span>,<br><span>branch3=</span><span>NULL</span>, <span>branch4=</span><span>NULL</span>,<br><span>depth=</span><span>1</span>,<br><span>lwd=</span><span>1</span>,<br><span>stub_color=</span><span>&quot;brown&quot;</span><br>)<br><br>draw_tree &lt;-<span> </span>function(tree) {<br><span>#function to draw the tree recursively</span><br>  if (<span>is.null</span>(tree)) <span>return</span>()<br><span>with</span>(tree,{<br><span>segments3D</span>(x0,y0,z0,x1,y1,z1,<span>col=</span>stub_color,<br><span>lwd=</span>lwd,<span>add=</span><span>TRUE</span>)<br><span>draw_tree</span>(branch1)<br><span>draw_tree</span>(branch2)<br><span>draw_tree</span>(branch3)<br><span>draw_tree</span>(extension)<br>  })<br>}<br><br>extend &lt;-<span> </span>function(tree) {<br><span>#creates an extension of the main branch</span><br><span>#in the same direction vector u=p1-p0</span><br><span>with</span>(tree,{<br>    growth_factor &lt;-<span> </span><span>runif</span>(<span>1</span>,<span>min=</span><span>0.9</span>,<span>max=</span><span>1</span>)<br><span>return</span>(<span>list</span>(<span>x0=</span>x1,<span>y0=</span>y1,<span>z0=</span>z1,<br><span>x1=</span>x1+growth_factor*(x1-x0),<br><span>y1=</span>y1+growth_factor*(y1-y0),<br><span>z1=</span>z1+growth_factor*(z1-z0),<br><span>extension=</span><span>NULL</span>,<br><span>branch1=</span><span>NULL</span>, <span>branch2=</span> <span>NULL</span>,<br><span>branch3=</span><span>NULL</span>, <span>branch4=</span> <span>NULL</span>,<br><span>depth=</span>depth*<span>0.9</span>,<br><span>lwd=</span><span>1</span>,<br><span>stub_color=</span><span>&quot;darkgreen&quot;</span>))<br>  })<br>}<br><br>create_branch &lt;-<span> </span>function(tree,c1,c2) {<br><span>#this function returns a branch for tree</span><br><span>#c1 and c2 are the components for the vector</span><br><span>#base {v1,v2}. v1 and v2 are two orthonrmal</span><br><span>#vectors, both perpedicular to the direction</span><br><span>#vector u=p1-p0 (p1 and p0 are the endpoints</span><br><span>#of the tree trunk).</span><br><span>#where_at is a scalar value in (0,1), indicating</span><br><span>#where along p0=(x0,y0,z0) and p1=(x1,y1,z1)</span><br><span>#the branch will grow.</span><br><span>with</span>(tree,{<br><span>#vector u=(x1-x0,y1-y0,z1-z0) gives the </span><br><span>#direction of the tree trunk.</span><br><span>#stub_length is the length of that trunk.</span><br>    u &lt;-<span> </span><span>c</span>(x1-x0,y1-y0,z1-z0)<br>    stub_length &lt;-<span> </span><span>sqrt</span>(<span>sum</span>(u*u))<br><span>#growth_factor is how long the branch</span><br><span>#will be with respect to stub_length</span><br>    growth_factor &lt;-<span> </span><span>runif</span>(<span>1</span>,<span>min=</span><span>0.7</span>,<span>max=</span><span>0.8</span>)<br>    where_at &lt;-<span> </span><span>runif</span>(<span>1</span>,<span>min=</span><span>0.4</span>,<span>max=</span><span>0.8</span>)<br><span>#create two perpendicular vectors to u</span><br><span>#and set their norms to 1</span><br>    i &lt;-<span> </span><span>which</span>(u!=<span>0</span>)[<span>1</span>]<br>    oi &lt;-<span> </span><span>setdiff</span>(<span>1</span>:<span>3</span>,i)<br>    v1 &lt;-<span> </span><span>rep</span>(<span>0</span>,<span>3</span>); v1[i] &lt;-<span> </span>-u[oi[<span>1</span>]]; v1[oi[<span>1</span>]] &lt;-<span> </span>u[i]<br>    v2 &lt;-<span> </span><span>rep</span>(<span>0</span>,<span>3</span>); v2[i] &lt;-<span> </span>-u[oi[<span>2</span>]]; v2[oi[<span>2</span>]] &lt;-<span> </span>u[i]<br>    v1 &lt;-<span> </span>v1/<span>sqrt</span>(<span>sum</span>(v1*v1)); v2 &lt;-<span> </span>v2/<span>sqrt</span>(<span>sum</span>(v2*v2))<br><span>#vector v is a linear combination of v1 and v2,</span><br><span>#hence it lies on the plane perpendicular to u</span><br>    v &lt;-<span> </span>v1*c1+v2*c2<br><span>#beta is the angle of separation between the</span><br><span>#branch and the trunk. The angle is larger</span><br><span>#for the &quot;deeper&quot; parts of the tree.</span><br>    beta &lt;-<span> </span><span>runif</span>(<span>1</span>,<span>min=</span><span>0.7</span>*depth,<span>max=</span><span>0.9</span>*depth)    <br><span>#calculate the scalar k for multiplying with v</span><br><span>#so that u+kv, the branch, will form a beta</span><br><span>#angle with the trunk.</span><br>    k &lt;-<span> </span><span>tan</span>(beta)*<span>sqrt</span>(<span>sum</span>(u*u))/<span>sqrt</span>(<span>sum</span>(v*v))<br><span>#new_u is the direction vector of the branch,</span><br><span>#whose length will be the length of the branch </span><br>    new_u &lt;-<span> </span>u+k*v<br>    new_u &lt;-<span> </span>new_u /<span> </span><span>sqrt</span>(<span>sum</span>(new_u*new_u))*<br><span> </span>stub_length*growth_factor<br><span>#the new (x0,y0,z0) point of the branch</span><br><span>#is where along the main trunk the branch begins.</span><br>    new_x0 &lt;-<span> </span>x0+(x1-x0)*where_at<br>    new_y0 &lt;-<span> </span>y0+(y1-y0)*where_at<br>    new_z0 &lt;-<span> </span>z0+(z1-z0)*where_at<br><span>#the new (x1,y1,z1) point of the branch is</span><br><span>#its ending point.</span><br>    new_x1 &lt;-<span> </span>new_x0+new_u[<span>1</span>]<br>    new_y1 &lt;-<span> </span>new_y0+new_u[<span>2</span>]<br>    new_z1 &lt;-<span> </span>new_z0+new_u[<span>3</span>]<br><span>list</span>(<br><span>x0=</span>new_x0,<span>y0=</span>new_y0,<span>z0=</span>new_z0,<br><span>x1=</span>new_x1,<span>y1=</span>new_y1,<span>z1=</span>new_z1,<br><span>extension=</span><span>NULL</span>,<br><span>branch1=</span><span>NULL</span>, <span>branch2=</span> <span>NULL</span>,<br><span>branch3=</span><span>NULL</span>, <span>branch4=</span> <span>NULL</span>,<br><span>depth=</span>depth*<span>0.8</span>,<br><span>lwd=</span><span>1</span>,<br><span>stub_color=</span><span>&quot;darkgreen&quot;</span>)    <br>  })<br>}<br><br>grow_tree &lt;-<span> </span>function(tree) {<br>  if (<span>is.null</span>(tree) ) <span>return</span>(<span>NULL</span>)<br>  tree$lwd &lt;-<span> </span>tree$lwd*<span>1.4</span><br>  if (tree$lwd><span>2.0</span>) tree$stub_color &lt;-<span> 'brown4'</span><br>  if (<span>is.null</span>(tree$extension)) {<br>    tree$extension &lt;-<span> </span><span>extend</span>(tree)<br>    if (tree$depth&lt;<span>0.5</span>) {<br><span>#make two branches at an angle of more than 160 degrees</span><br>      rot1 &lt;-<span> </span><span>runif</span>(<span>1</span>,<span>min=</span><span>0</span>,<span>max=</span><span>2</span>*pi)<br>      rot2 &lt;-<span> </span>(<span>runif</span>(<span>1</span>,<span>min=</span><span>8</span>*pi/<span>9</span>,<span>max=</span><span>10</span>*pi/<span>9</span>)+rot1)%%(<span>2</span>*pi)<br>      tree$branch1 &lt;-<span> </span><span>create_branch</span>(tree,<span>cos</span>(rot1),<span>sin</span>(rot1))<br>      tree$branch2 &lt;-<span> </span><span>create_branch</span>(tree,<span>cos</span>(rot2),<span>sin</span>(rot2))<br>    } else { <br><span>#make 3 branches with 120 degree angles between them and</span><br><span>#rotate the entire branch set.</span><br>      rot &lt;-<span> </span><span>runif</span>(<span>1</span>,<span>min=</span><span>0</span>,<span>max=</span><span>2</span>*pi)<br>      tree$branch1 &lt;-<span> </span><span>create_branch</span>(tree,-<span>sin</span>(rot),<span>cos</span>(rot))<br>      tree$branch2 &lt;-<span> </span><span>create_branch</span>(tree,<br><span>sqrt</span>(<span>3</span>)/<span>2</span>*<span>cos</span>(rot)+<span>sin</span>(rot)/<span>2</span>,<br><span>sqrt</span>(<span>3</span>)/<span>2</span>*<span>sin</span>(rot)-<span>cos</span>(rot)/<span>2</span>)<br>      tree$branch3 &lt;-<span> </span><span>create_branch</span>(tree,<br>                        -<span>sqrt</span>(<span>3</span>)/<span>2</span>*<span>cos</span>(rot)+<span>sin</span>(rot)/<span>2</span>,<br>                        -<span>sqrt</span>(<span>3</span>)/<span>2</span>*<span>sin</span>(rot)-<span>cos</span>(rot)/<span>2</span>)<br>    }<br>  } else {<br>    tree$extension &lt;-<span> </span><span>grow_tree</span>(tree$extension)<br>    tree$branch1 &lt;-<span> </span><span>grow_tree</span>(tree$branch1)<br>    tree$branch2 &lt;-<span> </span><span>grow_tree</span>(tree$branch2)<br>    if (tree$depth>=<span>0.5</span>) <br>      tree$branch3 &lt;-<span> </span><span>grow_tree</span>(tree$branch3)<br>  }<br><span>return</span>(tree)<br>}<br><br>create_ornaments &lt;-<span> </span>function(tree) {<br>  if (<span>is.null</span>(tree)) <span>return</span>()<br>  po &lt;-<span> </span>(<span>1</span>-tree$depth)^<span>6</span><br>  ornament &lt;-<span> </span><span>sample</span>(<span>c</span>(T,F),<span>size=</span><span>1</span>,<span>prob=</span><span>c</span>(po,<span>1</span>-po))<br>  co &lt;-<span> </span><span>sample</span>(<span>c</span>(<span>&quot;red&quot;</span>,<span>&quot;darkgoldenrod4&quot;</span>),<span>size=</span><span>1</span>,<br><span>prob=</span><span>c</span>(<span>0.6</span>,<span>0.4</span>))<br>  if (ornament)<br>    ornaments &lt;&lt;-<span> </span><span>rbind</span>(ornaments,<br><span>data.frame</span>(<span>x=</span>tree$x1,<span>y=</span>tree$y1,<span>z=</span>tree$z1,<span>color=</span>co))<br><span>create_ornaments</span>(tree$branch1)<br><span>create_ornaments</span>(tree$branch2)<br><span>create_ornaments</span>(tree$branch3)<br><span>create_ornaments</span>(tree$extension)<br>}<br><br>create_lights1 &lt;-<span> </span>function(tree) {<br>  if (<span>is.null</span>(tree)) <span>return</span>()<br>  po &lt;-<span> </span>(<span>1</span>-tree$depth)^<span>4</span><br>  light &lt;-<span> </span><span>sample</span>(<span>c</span>(T,F),<span>size=</span><span>1</span>,<span>prob=</span><span>c</span>(po,<span>1</span>-po))<br>  if (light)<br>    lights1 &lt;&lt;-<span> </span><span>rbind</span>(lights1,<br><span>data.frame</span>(<span>x=</span>tree$x1,<span>y=</span>tree$y1,<span>z=</span>tree$z1))<br><span>create_lights1</span>(tree$branch1)<br><span>create_lights1</span>(tree$branch2)<br><span>create_lights1</span>(tree$branch3)<br><span>create_lights1</span>(tree$extension)<br>}<br><br>create_lights2 &lt;-<span> </span>function(tree) {<br>  if (<span>is.null</span>(tree)) <span>return</span>()<br>  po &lt;-<span> </span>(<span>1</span>-tree$depth)^<span>4</span><br>  light &lt;-<span> </span><span>sample</span>(<span>c</span>(T,F),<span>size=</span><span>1</span>,<span>prob=</span><span>c</span>(po,<span>1</span>-po))<br>  if (light)<br>    lights2 &lt;&lt;-<span> </span><span>rbind</span>(lights2,<br><span>data.frame</span>(<span>x=</span>(tree$x1+tree$x0)/<span>2</span>,<br><span>y=</span>(tree$y1+tree$y0)/<span>2</span>,<br><span>z=</span>(tree$z1+tree$z0)/<span>2</span>))<br><span>create_lights2</span>(tree$branch1)<br><span>create_lights2</span>(tree$branch2)<br><span>create_lights2</span>(tree$branch3)<br><span>create_lights2</span>(tree$extension)<br>}<br><br>draw_ornaments &lt;-<span> </span>function() {<br><span>with</span>(ornaments,<br>    {<span>points3D</span>(<span>x=</span>x,<span>y=</span>y,<span>z=</span>z,<br><span>pch=</span><span>19</span>,<span>cex=</span><span>1.2</span>,<span>col=</span><span>as.character</span>(color),<br><span>colkey=</span><span>FALSE</span>,<span>add=</span><span>TRUE</span>)})<br>}<br><br>draw_lights1 &lt;-<span> </span>function() {<br><span>with</span>(lights1,<br>    {<span>points3D</span>(<span>x=</span>x,<span>y=</span>y,<span>z=</span>z,<span>pch=</span><span>&quot;+&quot;</span>,<span>cex=</span><span>0.8</span>,<span>col=</span><span>&quot;white&quot;</span>,<br><span>colkey=</span><span>FALSE</span>,<span>add=</span><span>TRUE</span>)})<br>}<br><br>draw_lights2 &lt;-<span> </span>function() {<br><span>with</span>(lights2,<br>    {<span>points3D</span>(<span>x=</span>x,<span>y=</span>y,<span>z=</span>z,<span>pch=</span><span>&quot;+&quot;</span>,<span>cex=</span><span>0.8</span>,<span>col=</span><span>&quot;yellow&quot;</span>,<br><span>colkey=</span><span>FALSE</span>,<span>add=</span><span>TRUE</span>)})<br>}<br><br><span>set.seed</span>(<span>20161224</span>)<br>pine_tree &lt;-<span> </span>trunk<br>for (i in <span>1</span>:<span>5</span>) pine_tree &lt;-<span> </span><span>grow_tree</span>(pine_tree)<br>ornaments &lt;-<span> </span><span>NULL</span>;<br>lights1 &lt;-<span> </span><span>NULL</span>;<br>lights2 &lt;-<span> </span><span>NULL</span>;<br><span>create_ornaments</span>(pine_tree)<br><span>create_lights1</span>(pine_tree)<br><span>create_lights2</span>(pine_tree)<br><br><span>png</span>(<span>&quot;tree%02d.png&quot;</span>)<br>for (i in <span>0</span>:<span>35</span>) {<br><span>perspbox</span>(<span>x=</span><span>c</span>(-<span>25</span>,<span>25</span>),<span>y=</span><span>c</span>(-<span>25</span>,<span>25</span>),<span>z=</span><span>c</span>(<span>0</span>,<span>40</span>),<span>bty=</span><span>&quot;n&quot;</span>,<br><span>phi=</span><span>8</span>,<span>theta=</span>i*<span>10</span>,<span>col=</span><span>&quot;white&quot;</span>,<span>alpha=</span><span>0</span>)<br><span>draw_tree</span>(pine_tree)<br><span>draw_ornaments</span>()<br>    switch((i%%<span>4</span>)+<span>1</span>,{},<br>     {<span>draw_lights1</span>()},<br>     {<span>draw_lights2</span>()},<br>     {<span>draw_lights1</span>(); <span>draw_lights2</span>()})<br>}<br><span>graphics.off</span>()</code></pre></td></tr></table></div> <div align="justify"><br><br></div><h2>Bibliography</h2><ul><li><div align="justify">Apostol, T. (1985). Calculus, Vol. II (2nd ed). (F. Veléz trans.). Caracas: Editorial Reverté.</div></li> <li><div align="justify">carlop. (April 26, 2012). Answer to &quot;How to find vector perpendicular to another vector?&quot; on math.stackexchange. [Retrieved 12/20/2016 from <a target="_blank" href="http://math.stackexchange.com/questions/137362/how-to-find-perpendicular-vector-to-another-vector">http://math.stackexchange.com/questions/137362/how-to-find-perpendicular-vector-to-another-vector</a>].</div></li> <li><div align="justify">Cooney, B. (1967). Christmas. New York: Thomas Y. Crowell Company.</div></li> <li><div align="justify">Duineveld, K. (2013, December). &quot;Merry Christmas&quot;. Blog post. [Retrieved 12/20/2016 from <a href="http://wiekvoet.blogspot.com/2013/12/merry-christmas.html">http://wiekvoet.blogspot.com/2013/12/merry-christmas.html</a>].</div></li> <li><div align="justify">R DEVELOPMENT CORE TEAM (2009). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. ISBN 3-900051-07-0. <a href="http://www.r-project.org/">http://www.R-project.org</a>.</div></li> <li><div align="justify">Soetaert, K. (2016). plot3D: Plotting multi-dimensional data. R package version 1.1. <a target="_blank" href="https://cran.r-project.org/web/packages/plot3D/index.html">https://cran.r-project.org/web/packages/plot3D/index.html</a>.</div></li></ul><div align="justify"><br><br><i><span>If you found this post interesting or useful, please share it on Google+, Facebook or Twitter so that others may find it too.</span></i></div><div align="center"><br><br><a href="https://drive.google.com/uc?export=download&id=0B_2tTMG4oaixcFMxVThtamhHU00"></a></div>