How to resolve the algorithm Fibonacci word/fractal step by step in the R programming language

Published on 12 May 2024 09:40 PM
#R

How to resolve the algorithm Fibonacci word/fractal step by step in the R programming language

Table of Contents

Problem Statement

The Fibonacci word may be represented as a fractal as described here: (Clicking on the above website   (hal.archives-ouvertes.fr)   will leave a cookie.)

Create and display a fractal similar to Fig 1. (Clicking on the above website   (hal.archives-ouvertes.fr)   will leave a cookie.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fibonacci word/fractal step by step in the R programming language

Source code in the r programming language

## Fibonacci word/fractal  2/20/17 aev
## Create Fibonacci word order n
fibow <- function(n) {
  t2="0"; t1="01"; t="";
  if(n<2) {n=2}
  for (i in 2:n) {t=paste0(t1,t2); t2=t1; t1=t}
  return(t)
}
## Plot Fibonacci word/fractal:
## n - word order, w - width, h - height, d - segment size, clr - color.
pfibofractal <- function(n, w, h, d, clr) {
  dx=d; x=y=x2=y2=tx=dy=nr=0;
  if(n<2) {n=2}
  fw=fibow(n); nf=nchar(fw);
  pf = paste0("FiboFractR", n, ".png");
  ttl=paste0("Fibonacci word/fractal, n=",n);
  cat(ttl,"nf=", nf, "pf=", pf,"\n");
  plot(NA, xlim=c(0,w), ylim=c(-h,0), xlab="", ylab="", main=ttl)
  for (i in 1:nf) {
    fwi=substr(fw, i, i);
    x2=x+dx; y2=y+dy;
    segments(x, y, x2, y2, col=clr); x=x2; y=y2;
    if(fwi=="0") {tx=dx; nr=i%%2;
      if(nr==0) {dx=-dy;dy=tx} else {dx=dy;dy=-tx}}
  }
  dev.copy(png, filename=pf, width=w, height=h); # plot to png-file
  dev.off(); graphics.off();  # Cleaning
}

## Executing:
pfibofractal(23, 1000, 1000, 1, "navy")
pfibofractal(25, 2300, 1000, 1, "red")


  

You may also check:How to resolve the algorithm Parallel brute force step by step in the Ada programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Klingphix programming language
You may also check:How to resolve the algorithm Hostname step by step in the LFE programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Elixir programming language
You may also check:How to resolve the algorithm Quoting constructs step by step in the Quackery programming language