# Fractal

Paradigm(s) Declarative User:Hakerh400 2022 Not applicable Implementation in JavaScript `.txt`

Fractal is an esoteric fractal rendering engine invented by User:Hakerh400 in 2022.

## Overview

Each program defines a 2D fractal pattern. The interpreter displays the fractal. Currently, programs cannot perform I/O operations or other sort of computation.

## Syntax

Source code consists of two parts. The first part defines lines that are drawn in the initial state. The second part describes how the drawn pattern repeats in smaller variants recursively. The two parts are separated by a blank line.

In the first part, lines are represented by a sequence of points separated by spaces. Each point is represented by its `x` and `y` coordinates (floating-point numbers), separated by a space.

In the second part, each recursive pattern is represented by three points (six floating-point numbers). These three points represent two vectors. The first vector goes from the first to the second point. The second vector goes from the first to the third point. These two vectors define a new viewport.

## Viewport

Initially, the viewport is represented by points `0 0 1 0 0 1`. The corresponding vectors represent the square viewport starting at $(0,0)$ whose width and height are both $1$ .

Each of the new viewports defined in the second part of the source code is drawn recursively by copying and transforming the original viewport. If you define multiple viewports, the number of recursive calls grows exponentially. Viewports are drawn until their area (the shape defined by the two vectors) is less than one pixel.

If the area of a new viewport is larger than the previous one, the program crashes.

## Examples

### Ternary Cantor set

```0 0 1 0 1 .05 0 .05 0 0

0 .1 .333 .1 0 .9
.667 .1 1 .1 .667 .9
```

The first row represents the line path that is drawn initially. It represents a line going from $(0,0)$ to $(1,0)$ , to $(1,0.05)$ , to $(0,0.05)$ , and finally it goes back to $(0,0)$ . It draws a rectangle whose top left corner is at $(0,0)$ and whose width and height are $1$ and $0.05$ , respectively.

The second part defines two new viewports. The first viewport is represented by a rectangle whose top left corner is at $(0,0.1)$ and whose with and height are $0.333$ and $0.8$ , respectively. The second rectangle is the same, except its top left corner is $(0.667,0.1)$ .

### Sierpiński carpet

```.333 .333 .667 .333 .667 .667 .333 .667 .333 .333

0 0 .333 0 0 .333
.333 0 .667 0 .333 .333
.667 0 1 0 .667 .333
0 .333 .333 .333 0 .667
.667 .333 1 .333 .667 .667
0 .667 .333 .667 0 1
.333 .667 .667 .667 .333 1
.667 .667 1 .667 .667 1
```

Like in the previous example, all viewports are rectangles (in this case squares) whose sides are parallel to the sides of original viewport (the rotation is zero).

### Simple tree

```.5 1 .5 .8

-0.041667 0.2375 0.625 0.0291667 0.1667 0.9041667
0.375 0.0291667 1.041667 0.2375 0.1667 0.6958333
```

In this example, we draw just a single vertical line from the middle bottom part of the initial viewport. Then we create two new viewports.

The new viewports are also squares, but they are rotated. The first one is rotated to the left and the second one is rotated to the right.

### Barnsley fern

```0.50667 1 0.50667 0.84

0.16 -0.0222 1 0 0.08 0.82889
-0.11555 0.67555 0.0889 0.56444 0.4 0.89333
1.13778 0.778 0.9778 0.64889 0.58667 1.0222
```