speice.io/blog/2024-11-15-playing-with-fire/2-transforms/index.mdx

216 lines
7.0 KiB
Plaintext
Raw Normal View History

2024-11-18 22:01:31 -05:00
---
slug: 2024/11/playing-with-fire-transforms
title: "Playing with fire: Transforms and variations"
2024-12-16 21:30:05 -05:00
date: 2024-12-16 21:31:00
2024-11-18 22:01:31 -05:00
authors: [bspeice]
tags: []
---
2024-12-16 21:29:03 -05:00
Now that we've learned about the chaos game, it's time to spice things up. Variations create the
2024-12-09 22:18:13 -05:00
shapes and patterns that fractal flames are known for.
2024-11-18 22:01:31 -05:00
<!-- truncate -->
2024-12-13 20:03:53 -05:00
:::info
2024-12-15 15:55:27 -05:00
This post uses [reference parameters](../params.flame) to demonstrate the fractal flame algorithm.
2024-12-16 21:29:03 -05:00
If you're interested in tweaking the parameters, or creating your own, [Apophysis](https://sourceforge.net/projects/apophysis/)
can load that file.
2024-12-09 22:18:13 -05:00
:::
2024-11-29 19:25:29 -05:00
2024-12-13 20:03:53 -05:00
## Variations
:::note
2024-12-16 21:29:03 -05:00
This post covers section 3 of the Fractal Flame Algorithm paper
2024-12-13 20:03:53 -05:00
:::
2024-11-18 22:01:31 -05:00
2024-12-09 22:18:13 -05:00
import CodeBlock from '@theme/CodeBlock'
2024-11-19 21:42:03 -05:00
2024-12-09 22:18:13 -05:00
We previously introduced transforms as the "functions" of an "iterated function system," and showed how
2024-12-16 21:29:03 -05:00
playing the chaos game gives us an image of Sierpinski's Gasket. Even though we used simple functions,
the image it generates is intriguing. But what would happen if we used something more complex?
2024-11-19 21:42:03 -05:00
2024-12-16 21:29:03 -05:00
This leads us to the first big innovation of the fractal flame algorithm: adding non-linear functions
2024-12-15 15:55:27 -05:00
after the affine transform. These functions are called "variations":
2024-11-18 22:01:31 -05:00
$$
F_i(x, y) = V_j(a_i x + b_i y + c_i, d_i x + e_i y + f_i)
2024-11-18 22:01:31 -05:00
$$
2024-12-09 22:18:13 -05:00
import variationSource from '!!raw-loader!../src/variation'
2024-11-30 17:35:42 -05:00
2024-12-09 22:18:13 -05:00
<CodeBlock language="typescript">{variationSource}</CodeBlock>
2024-11-18 22:01:31 -05:00
2024-12-15 15:55:27 -05:00
Just like transforms, variations ($V_j$) are functions that take in $(x, y)$ coordinates
and give back new $(x, y)$ coordinates.
However, the sky is the limit for what happens between input and output.
2024-12-16 21:29:03 -05:00
The Fractal Flame paper lists 49 variation functions,
2024-12-13 20:03:53 -05:00
and the official `flam3` implementation supports [98 different variations](https://github.com/scottdraves/flam3/blob/7fb50c82e90e051f00efcc3123d0e06de26594b2/variations.c).
2024-11-18 22:01:31 -05:00
2024-12-16 21:29:03 -05:00
To draw our reference image, we'll focus on just four:
2024-11-18 22:01:31 -05:00
### Linear (variation 0)
2024-12-16 21:29:03 -05:00
This variation is dead simple: return the $x$ and $y$ coordinates as-is.
2024-11-18 22:01:31 -05:00
$$
V_0(x,y) = (x,y)
$$
2024-11-19 21:42:03 -05:00
import linearSrc from '!!raw-loader!../src/linear'
<CodeBlock language={'typescript'}>{linearSrc}</CodeBlock>
2024-12-09 22:18:13 -05:00
:::tip
2024-12-13 20:03:53 -05:00
In a way, we've already been using this variation! The transforms that define Sierpinski's Gasket
2024-12-16 21:29:03 -05:00
apply the affine coefficients to the input point and use that as the output.
2024-12-09 22:18:13 -05:00
:::
### Julia (variation 13)
2024-12-16 21:29:03 -05:00
This variation is a good example of a non-linear function. It uses both trigonometry
2024-12-13 20:03:53 -05:00
and probability to produce interesting shapes:
2024-11-18 22:01:31 -05:00
$$
\begin{align*}
r &= \sqrt{x^2 + y^2} \\
\theta &= \text{arctan}(x / y) \\
\Omega &= \left\{
2024-11-30 17:35:42 -05:00
\begin{array}{lr}
0 \hspace{0.4cm} \text{w.p. } 0.5 \\
\pi \hspace{0.4cm} \text{w.p. } 0.5 \\
\end{array}
2024-11-18 22:01:31 -05:00
\right\} \\
V_{13}(x, y) &= \sqrt{r} \cdot (\text{cos} ( \theta / 2 + \Omega ), \text{sin} ( \theta / 2 + \Omega ))
\end{align*}
$$
2024-11-19 21:42:03 -05:00
import juliaSrc from '!!raw-loader!../src/julia'
2024-11-18 22:01:31 -05:00
2024-11-19 21:42:03 -05:00
<CodeBlock language={'typescript'}>{juliaSrc}</CodeBlock>
2024-11-18 22:01:31 -05:00
### Popcorn (variation 17)
2024-12-15 15:55:27 -05:00
Some variations rely on knowing the transform's affine coefficients; they're called "dependent variations."
2024-12-16 21:29:03 -05:00
For this variation, we use $c$ and $f$:
2024-11-18 22:01:31 -05:00
$$
V_{17}(x,y) = (x + c\ \text{sin}(\text{tan }3y), y + f\ \text{sin}(\text{tan }3x))
2024-11-18 22:01:31 -05:00
$$
2024-11-19 21:42:03 -05:00
import popcornSrc from '!!raw-loader!../src/popcorn'
<CodeBlock language={'typescript'}>{popcornSrc}</CodeBlock>
2024-11-18 22:01:31 -05:00
### PDJ (variation 24)
2024-12-16 21:29:03 -05:00
Some variations have extra parameters we can choose; they're called "parametric variations."
For the PDJ variation, there are four extra parameters:
2024-11-18 22:01:31 -05:00
$$
p_1 = \text{pdj.a} \hspace{0.1cm} p_2 = \text{pdj.b} \hspace{0.1cm} p_3 = \text{pdj.c} \hspace{0.1cm} p_4 = \text{pdj.d} \\
V_{24} = (\text{sin}(p_1 y) - \text{cos}(p_2 x), \text{sin}(p_3 x) - \text{cos}(p_4 y))
2024-11-18 22:01:31 -05:00
$$
2024-11-19 21:42:03 -05:00
import pdjSrc from '!!raw-loader!../src/pdj'
<CodeBlock language={'typescript'}>{pdjSrc}</CodeBlock>
## Blending
2024-11-19 21:42:03 -05:00
2024-12-09 22:18:13 -05:00
Now, one variation is fun, but we can also combine variations in a process called "blending."
2024-11-29 19:25:29 -05:00
Each variation receives the same $x$ and $y$ inputs, and we add together each variation's $x$ and $y$ outputs.
2024-12-15 15:55:27 -05:00
We'll also give each variation a weight ($v_{ij}$) that changes how much it contributes to the result:
2024-11-19 21:42:03 -05:00
$$
F_i(x,y) = \sum_{j} v_{ij} V_j(x, y)
2024-11-19 21:42:03 -05:00
$$
The formula looks intimidating, but it's not hard to implement:
2024-12-01 21:57:10 -05:00
import blendSource from "!!raw-loader!../src/blend";
<CodeBlock language={'typescript'}>{blendSource}</CodeBlock>
2024-11-19 21:42:03 -05:00
2024-12-16 21:29:03 -05:00
With that in place, we have enough to render a fractal flame. We'll use the same
chaos game as before, but the new transforms and variations produce a dramatically different image:
2024-12-09 22:18:13 -05:00
:::tip
2024-12-16 21:29:03 -05:00
Try using the variation weights to figure out which parts of the image each transform controls.
2024-12-09 22:18:13 -05:00
:::
2024-11-29 19:25:29 -05:00
2024-12-08 22:50:46 -05:00
import {SquareCanvas} from "../src/Canvas";
2024-11-29 19:25:29 -05:00
import FlameBlend from "./FlameBlend";
2024-12-15 15:55:27 -05:00
<SquareCanvas name={"flame_blend"}><FlameBlend/></SquareCanvas>
2024-11-30 17:35:42 -05:00
## Post transforms
2024-12-16 21:29:03 -05:00
Next, we'll introduce a second affine transform applied _after_ variation blending. This is called a "post transform."
2024-12-15 15:55:27 -05:00
2024-12-16 21:29:03 -05:00
We'll use some new variables, but the post transform should look familiar:
2024-11-30 17:35:42 -05:00
$$
2024-12-09 22:18:13 -05:00
\begin{align*}
P_i(x, y) &= (\alpha_i x + \beta_i y + \gamma_i, \delta_i x + \epsilon_i y + \zeta_i) \\
F_i(x, y) &= P_i\left(\sum_{j} v_{ij} V_j(x, y)\right)
\end{align*}
2024-11-30 17:35:42 -05:00
$$
2024-12-09 22:18:13 -05:00
import postSource from '!!raw-loader!./post'
<CodeBlock language="typescript">{postSource}</CodeBlock>
2024-12-13 20:03:53 -05:00
The image below uses the same transforms/variations as the previous fractal flame,
2024-12-16 21:29:03 -05:00
but allows changing the post-transform coefficients:
2024-12-09 22:18:13 -05:00
<details>
2024-12-13 20:03:53 -05:00
<summary>If you want to test your understanding...</summary>
2024-12-09 22:18:13 -05:00
2024-12-15 15:55:27 -05:00
- What post-transform coefficients will give us the previous image?
- What post-transform coefficients will give us a _mirrored_ image?
2024-12-09 22:18:13 -05:00
</details>
2024-11-30 17:35:42 -05:00
import FlamePost from "./FlamePost";
2024-12-15 15:55:27 -05:00
<SquareCanvas name={"flame_post"}><FlamePost/></SquareCanvas>
2024-11-30 17:35:42 -05:00
2024-12-09 22:18:13 -05:00
## Final transforms
2024-12-16 21:29:03 -05:00
The last step is to introduce a "final transform" ($F_{final}$) that is applied
regardless of which regular transform ($F_i$) the chaos game selects.
It's just like a normal transform (composition of affine transform, variation blend, and post transform),
but it doesn't affect the chaos game state.
2024-12-14 16:55:54 -05:00
2024-12-16 21:29:03 -05:00
After adding the final transform, our chaos game algorithm looks like this:
2024-12-14 16:55:54 -05:00
$$
\begin{align*}
&(x, y) = \text{random point in the bi-unit square} \\
&\text{iterate } \{ \\
&\hspace{1cm} i = \text{random integer from 0 to } n - 1 \\
&\hspace{1cm} (x,y) = F_i(x,y) \\
&\hspace{1cm} (x_f,y_f) = F_{final}(x,y) \\
&\hspace{1cm} \text{plot}(x_f,y_f) \text{ if iterations} > 20 \\
\}
\end{align*}
$$
2024-12-13 20:03:53 -05:00
2024-12-09 22:18:13 -05:00
import chaosGameFinalSource from "!!raw-loader!./chaosGameFinal"
<CodeBlock language="typescript">{chaosGameFinalSource}</CodeBlock>
2024-11-30 17:35:42 -05:00
2024-12-16 21:29:03 -05:00
This image uses the same normal/post transforms as above, but allows modifying
the coefficients and variations of the final transform:
2024-11-30 17:35:42 -05:00
import FlameFinal from "./FlameFinal";
2024-12-15 15:55:27 -05:00
<SquareCanvas name={"flame_final"}><FlameFinal/></SquareCanvas>
2024-12-13 20:03:53 -05:00
## Summary
2024-12-15 15:55:27 -05:00
Variations are the fractal flame algorithm's first major innovation.
2024-12-16 21:29:03 -05:00
By blending variation functions and post/final transforms, we generate unique images.
2024-12-13 20:03:53 -05:00
2024-12-16 21:29:03 -05:00
However, these images are grainy and unappealing. In the next post, we'll clean up
2024-12-14 16:55:54 -05:00
the image quality and add some color.