{"id":697,"date":"2021-12-20T08:41:22","date_gmt":"2021-12-20T07:41:22","guid":{"rendered":"https:\/\/lorentzen.ch\/?p=697"},"modified":"2021-12-20T08:41:24","modified_gmt":"2021-12-20T07:41:24","slug":"x-mas-tree-with-10-lines-of-r-code","status":"publish","type":"post","link":"https:\/\/lorentzen.ch\/index.php\/2021\/12\/20\/x-mas-tree-with-10-lines-of-r-code\/","title":{"rendered":"X-Mas Tree with 10 Lines of R Code"},"content":{"rendered":"\n<p>Besides the many negative aspects of going through a pandemic, there are also certain positive ones like having time to write short blog posts like this.<\/p>\n\n\n\n<p>This one picks up a topic that was intensively discussed a couple of years ago on <a href=\"https:\/\/community.wolfram.com\/groups\/-\/m\/t\/175891\">Wolfram&#8217;s page<\/a>: Namely that the damped sine wave <\/p>\n\n\n\n<p>f(t) = t sin(t) <\/p>\n\n\n\n<p>can be used to draw a Christmas tree. Throw in some 3D animation using the R package <a href=\"https:\/\/CRAN.R-project.org\/package=rgl\">rgl<\/a> and the tree begins to become virtual reality&#8230;<\/p>\n\n\n\n<p>Here is our version using just ten lines of R code:<\/p>\n\n\n<div class=\"wp-block-ub-tabbed-content wp-block-ub-tabbed-content-holder wp-block-ub-tabbed-content-horizontal-holder-mobile wp-block-ub-tabbed-content-horizontal-holder-tablet\" id=\"ub-tabbed-content-2a39212f-a829-49ec-a355-d855a4f50240\" style=\"\">\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-holder horizontal-tab-width-mobile horizontal-tab-width-tablet\">\n\t\t\t\t<div role=\"tablist\" class=\"wp-block-ub-tabbed-content-tabs-title wp-block-ub-tabbed-content-tabs-title-mobile-horizontal-tab wp-block-ub-tabbed-content-tabs-title-tablet-horizontal-tab\" style=\"justify-content: flex-start; \"><div role=\"tab\" id=\"ub-tabbed-content-2a39212f-a829-49ec-a355-d855a4f50240-tab-0\" aria-controls=\"ub-tabbed-content-2a39212f-a829-49ec-a355-d855a4f50240-panel-0\" aria-selected=\"true\" class=\"wp-block-ub-tabbed-content-tab-title-wrap active\" style=\"--ub-tabbed-title-background-color: #6d6d6d; --ub-tabbed-active-title-color: inherit; --ub-tabbed-active-title-background-color: #6d6d6d; text-align: center; \" tabindex=\"-1\">\n\t\t\t\t<div class=\"wp-block-ub-tabbed-content-tab-title\">R<\/div>\n\t\t\t<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<div class=\"wp-block-ub-tabbed-content-tabs-content\" style=\"\"><div role=\"tabpanel\" class=\"wp-block-ub-tabbed-content-tab-content-wrap active\" id=\"ub-tabbed-content-2a39212f-a829-49ec-a355-d855a4f50240-panel-0\" aria-labelledby=\"ub-tabbed-content-2a39212f-a829-49ec-a355-d855a4f50240-tab-0\" tabindex=\"0\">\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting='{\"showPanel\":true,\"languageLabel\":\"language\",\"fullScreenButton\":true,\"copyButton\":true,\"mode\":\"r\",\"mime\":\"text\/x-rsrc\",\"theme\":\"material\",\"lineNumbers\":false,\"styleActiveLine\":false,\"lineWrapping\":false,\"readOnly\":true,\"fileName\":\"\",\"language\":\"R\",\"maxHeight\":\"400px\",\"modeName\":\"r\"}'>library(rgl)\n\nt &lt;- seq(0, 100, by = 0.7)^0.6\nx &lt;- t * c(sin(t), sin(t + pi))\ny &lt;- t * c(cos(t), cos(t + pi))\nz &lt;- -2 * c(t, t)\ncolor &lt;- rep(c(\"darkgreen\", \"gold\"), each = length(t))\n\nopen3d(windowRect = c(100, 100, 600, 600), zoom = 0.9)\nbg3d(\"black\")\nspheres3d(x, y, z, radius = 0.3, color = color)\n\n# On screen (skip if export)\nplay3d(spin3d(axis = c(0, 0, 1), rpm = 4))\n\n# Export (requires 3rd party tool \"ImageMagick\" resp. magick-package)\n# movie3d(spin3d(axis = c(0, 0, 1), rpm = 4), duration = 30, dir = getwd())<\/pre><\/div>\n\n<\/div><\/div>\n\t\t<\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/lorentzen.ch\/wp-content\/uploads\/2021\/12\/xmas_movie.gif\" alt=\"\" class=\"wp-image-698\"\/><figcaption>Exported as gif using magick<\/figcaption><\/figure>\n\n\n\n<p>Christian and me wish you a relaxing time over Christmas. Take care of the people you love and stay healthy and safe.<\/p>\n\n\n\n<p>Code and animation can be found on <a href=\"https:\/\/github.com\/mayer79\/xmas_tree_r\">github<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Besides the many negative aspects of going through a pandemic, there are also certain positive ones like having time to write short blog posts like this. This one picks up a topic that was intensively discussed a couple of years ago on Wolfram&#8217;s page: Namely that the damped sine wave f(t) = t sin(t) can [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[5],"class_list":["post-697","post","type-post","status-publish","format-standard","hentry","category-statistics","tag-r"],"featured_image_src":null,"author_info":{"display_name":"Michael Mayer","author_link":"https:\/\/lorentzen.ch\/index.php\/author\/michael\/"},"_links":{"self":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts\/697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/comments?post=697"}],"version-history":[{"count":6,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":706,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/posts\/697\/revisions\/706"}],"wp:attachment":[{"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/media?parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/categories?post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lorentzen.ch\/index.php\/wp-json\/wp\/v2\/tags?post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}