Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/andrie/fractaltree

Create fractal trees using R
https://github.com/andrie/fractaltree

Last synced: about 1 month ago
JSON representation

Create fractal trees using R

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "60%"
)
```
# fractaltree

The goal of fractaltree is to plot trees as fractals. Using the shape of a single "leaf", every subsequent growht takes the same shape.

```{r, message=FALSE, echo=FALSE}
library(fractaltree)
library(magrittr)
library(dplyr)

leaf <- local({
matrix(
c(0, 0, 0, 1,
0, 0.45, 0.2, 1,
0, 0.45, -0.2, 1),
byrow = TRUE, ncol = 4
)
})

tree <- fractal_tree(leaf, depth = 3, growth_fraction = 1)

tree %>%
filter(i <= 1) %>%
bind_rows(
tree %>%
filter(i <= 2) %>%
translate(c(1.5, 0))
) %>%
bind_rows(
tree %>%
filter(i <= 4) %>%
translate(c(4, 0))
) %>%
plot_tree()

```

## Installation

```r
devtools::install_github("andrie/fractaltree")
```

## Example

You can use the `depth` argument to change the number of growth cycles, and the `shrinkage_factor` to control how much growth with every generation. If the growth is high, the resulting image may resemble a shrub, and if the growth is low, the resulting image may resemble a mature tree.

```{r, fig.keep='all'}
fractal_tree(leaf, depth = 5, growth_fraction = 1) %>%
plot_tree()

fractal_tree(leaf, depth = 5, growth_fraction = 0.75) %>%
plot_tree()

fractal_tree(leaf, depth = 5, growth_fraction = 0.5) %>%
plot_tree()

```

You can make interesting tree shapes if the leaf shape is asymmetric:

```{r}
leaf <- local({
matrix(
c(0, 0, 0, 1,
0, 0.45, 0.12, 0.80,
0, 0.85, -0.04, 0.95),
byrow = TRUE, ncol = 4
)
})

leaf %>%
fractal_tree(depth = 1) %>%
plot_tree()

leaf %>%
fractal_tree(depth = 7, growth_fraction = 0.6) %>%
plot_tree()

```

## Translate, rotate and grow

You can use the functions `translate()`, `rotate()` and `shrink()` to perform spatial transformation on a tree.

## Kaleidoscope

In addition you can use the `kaleidoscope()` function to turn a tree into a symmetric diagram:

```{r}
leaf %>%
fractal_tree(depth = 3, growth_fraction = 0.8) %>%
kaleidoscope() %>%
plot_tree(colors = c( "red", "blue"))

```

By first translating a tree in 2-dimensional space, using the `translate()` function, you can create very interesting `kaleidoscope()` images:

```{r}
leaf %>%
fractal_tree(depth = 3, growth_fraction = 0.8) %>%
kaleidoscope() %>%
translate(c(0, 5)) %>%
kaleidoscope() %>%
plot_tree(colors = c( "red", "blue"))

```