{"id":385585,"date":"2024-06-30T08:23:48","date_gmt":"2024-06-30T14:23:48","guid":{"rendered":"http:\/\/tomaztsql.wordpress.com\/?p=10010"},"modified":"2024-06-30T08:23:48","modified_gmt":"2024-06-30T14:23:48","slug":"little-useless-useful-r-functions-dragon-curve","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2024\/06\/little-useless-useful-r-functions-dragon-curve\/","title":{"rendered":"Little useless-useful R functions \u2013 Dragon curve"},"content":{"rendered":"<!-- \r\n<div style=\"min-height: 30px;\">\r\n[social4i size=\"small\" align=\"align-left\"]\r\n<\/div>\r\n-->\r\n\r\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\">\r\n[This article was first published on  <strong><a href=\"https:\/\/tomaztsql.wordpress.com\/2024\/06\/30\/little-useless-useful-r-functions-dragon-curve\/\"> R \u2013 TomazTsql<\/a><\/strong>, and kindly contributed to <a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers<\/a>].  (You can report issue about the content on this page <a href=\"https:\/\/www.r-bloggers.com\/contact-us\/\">here<\/a>)\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>\n\n<p>Let\u2019s play with some dragons. Dragons from the Jurassic park or the board game  dungeon and dragons.<\/p>\n\n\n\n<p>The algorithm  is a fractal curve of Hausdorff dimension 2. One starts with one segment. In each iteration the number of segments is doubled by taking each segment as the diagonal of a square and replacing it by half the square (90 degrees). Alternating and doing the left  and right function \/ direction to complement in order to get the shape.<\/p>\n\n\n\n<p>Assume that we start with   just a horizontal line  (called Dn) as a  segment. Since we created two dragons, we will introduce another line (called Cn, which is a derivate of Dn+1). The Dn+1 is obtained from Dn as follows:<br>1) Translate Dn, moving its end point to the origin.<br>2) Multiply the translated copy by \u221a1\/2.<br>3) Rotate the result of previous step by \u221245\u25e6 degrees and call the result Cn.<br>3) Rotate Cn by \u221290\u25e6 degrees and join this rotated copy to the end of Cn to get Dn+1.<\/p>\n\n\n\n<p>Translate this to the function, we get the following IFS (iterated function system).<\/p>\n\n\n<pre>\n# Function to generate points using IFS\ngenerate_dragon_curve &lt;- function(iterations) {\n  # Initial point\n  points &lt;- complex(real = 0, imaginary = 0)\n  colors &lt;- c() # Vector to store colors\n\n  # two iterated functions; each for own direction\n  f1 &lt;- function(z) {      (1 + 1i) * z \/ 2 }\n  f2 &lt;- function(z) {  1 - (1 - 1i) * z \/ 2 }\n  \n  # iterate to generate points\n  for (i in 1:iterations) {\n    new_points &lt;- vector(&quot;complex&quot;, length = length(points) * 2)\n    new_colors &lt;- vector(&quot;character&quot;, length = length(points) * 2)\n    for (j in 1:length(points)) {\n      new_points[2 * j - 1] &lt;- f1(points[j])\n      new_colors[2 * j - 1] &lt;- ifelse(i %% 2 == 1, &quot;blue&quot;, &quot;red&quot;) # Alternating colors\n      new_points[2 * j] &lt;- f2(points[j])\n      new_colors[2 * j] &lt;- ifelse(i %% 2 == 1, &quot;red&quot;, &quot;blue&quot;) # Alternating colors\n    }\n    points &lt;- new_points\n    colors &lt;- c(colors, new_colors)\n  }\n  \n  return(list(points = points, colors = colors))\n}\n<\/pre>\n\n\n<p>Once we have the points, we need to plot the points with corresponding numbers.<\/p>\n\n\n<pre>\nplot_dragon_curve &lt;- function(iterations) {\n  result &lt;- generate_dragon_curve(iterations)\n  points &lt;- result$points\n  colors &lt;- result$colors\n  plot(Re(points), Im(points), type = &quot;p&quot;, pch = &quot;.&quot;, col = colors, asp = 1, labels=FALSE, yaxt=&quot;n&quot;, xaxt=&quot;n&quot;,\n       xlab = &quot;&quot;, ylab = &quot;&quot;, main = paste(&quot;Heighway Dragon Curve with&quot;, iterations, &quot;iterations&quot;))\n}\n<\/pre>\n\n\n<p>And lastly, execute the function (in this case with 15 iterations):<\/p>\n\n\n<pre>\nplot_dragon_curve(15)\n<\/pre>\n\n\n<p>This plots the dragon curve.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/i2.wp.com\/tomaztsql.wordpress.com\/wp-content\/uploads\/2024\/06\/image-7.png?ssl=1\" rel=\"nofollow\" target=\"_blank\"><img loading=\"lazy\" data-attachment-id=\"10019\" data-permalink=\"https:\/\/tomaztsql.wordpress.com\/2024\/06\/30\/little-useless-useful-r-functions-dragon-curve\/image-129\/\" data-orig-file=\"https:\/\/tomaztsql.wordpress.com\/wp-content\/uploads\/2024\/06\/image-7.png\" data-orig-size=\"1086,1070\" data-comments-opened=\"1\" data-image-meta=\"{\"aperture\":\"0\",\"credit\":\"\",\"camera\":\"\",\"caption\":\"\",\"created_timestamp\":\"0\",\"copyright\":\"\",\"focal_length\":\"0\",\"iso\":\"0\",\"shutter_speed\":\"0\",\"title\":\"\",\"orientation\":\"0\"}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/tomaztsql.wordpress.com\/wp-content\/uploads\/2024\/06\/image-7.png?w=300\" data-large-file=\"https:\/\/tomaztsql.wordpress.com\/wp-content\/uploads\/2024\/06\/image-7.png?w=605\" tabindex=\"0\" role=\"button\" src=\"https:\/\/i2.wp.com\/tomaztsql.wordpress.com\/wp-content\/uploads\/2024\/06\/image-7.png?w=450&#038;ssl=1\" alt=\"\" class=\"wp-image-10019\" style=\"width:741px;height:auto\" data-recalc-dims=\"1\" \/><\/a><\/figure><\/div>\n\n\n<p>As always, the complete code is available on GitHub in \u00a0<a href=\"https:\/\/github.com\/tomaztk\/Useless_R_functions\" rel=\"nofollow\" target=\"_blank\">Useless_R_function repository<\/a>. The sample file in this repository is\u00a0here\u00a0(filename:\u00a0<em><a href=\"https:\/\/github.com\/tomaztk\/Useless_R_functions\/blob\/main\/functions\/twin_dragon_fractal.R\" rel=\"nofollow\" target=\"_blank\">Two_dragon_fractal.R<\/a><\/em>). Check the repository for future updates.<\/p>\n\n\n\n<p>Happy R-coding and stay healthy!<\/p>\n\n\n\n<p><\/p>\n\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\">\r\n<div style=\"text-align: center;\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"https:\/\/tomaztsql.wordpress.com\/2024\/06\/30\/little-useless-useful-r-functions-dragon-curve\/\"> R \u2013 TomazTsql<\/a><\/strong>.<\/div>\r\n<hr \/>\r\n<a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers.com<\/a> offers <strong><a href=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=RBloggers\" rel=\"nofollow\">daily e-mail updates<\/a><\/strong> about <a title=\"The R Project for Statistical Computing\" href=\"https:\/\/www.r-project.org\/\" rel=\"nofollow\">R<\/a> news and tutorials about <a title=\"R tutorials\" href=\"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/\" rel=\"nofollow\">learning R<\/a> and many other topics. <a title=\"Data science jobs\" href=\"https:\/\/www.r-users.com\/\" rel=\"nofollow\">Click here if you're looking to post or find an R\/data-science job<\/a>.\r\n\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<div style = \"width:60%; display: inline-block; float:left; \"> Let\u2019s play with some dragons. Dragons from the Jurassic park or the board game dungeon and dragons. The algorithm is a fractal curve of Hausdorff dimension 2. One starts with one segment. In each iteration the number of segments is\u2026Read more \u203a<\/div>\n<div style = \"width: 40%; display: inline-block; float:right;\"><\/div>\n<div style=\"clear: both;\"><\/div>\n","protected":false},"author":1281,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"aioseo_notices":[],"jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/385585"}],"collection":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/users\/1281"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=385585"}],"version-history":[{"count":4,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/385585\/revisions"}],"predecessor-version":[{"id":388508,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/385585\/revisions\/388508"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=385585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=385585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=385585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}