Playing with fire: Transforms and variations
Now that we've learned about the chaos game, it's time to spice things up. Variations create the shapes and patterns that fractal flames are known for.
This post uses reference parameters to demonstrate the fractal flame algorithm. If you're interested in tweaking the parameters, or creating your own, Apophysis can load that file.
Variations
This post covers section 3 of the Fractal Flame Algorithm paper
We previously introduced transforms as the "functions" of an "iterated function system," and showed how 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?
This leads us to the first big innovation of the fractal flame algorithm: adding non-linear functions after the affine transform. These functions are called "variations":
export type Variation = (
x: number,
y: number
) => [number, number];
Just like transforms, variations () are functions that take in coordinates
and give back new coordinates.
However, the sky is the limit for what happens between input and output.
The Fractal Flame paper lists 49 variation functions,
and the official flam3
implementation supports 98 different variations.
To draw our reference image, we'll focus on just four:
Linear (variation 0)
This variation is dead simple: return the and coordinates as-is.
import {Variation} from "./variation"
export const linear: Variation =
(x, y) => [x, y];
In a way, we've already been using this variation! The transforms that define Sierpinski's Gasket apply the affine coefficients to the input point and use that as the output.
Julia (variation 13)
This variation is a good example of a non-linear function. It uses both trigonometry and probability to produce interesting shapes:
import { Variation } from "./variation";
const omega =
() => Math.random() > 0.5 ? 0 : Math.PI;
export const julia: Variation =
(x, y) => {
const x2 = Math.pow(x, 2);
const y2 = Math.pow(y, 2);
const r = Math.sqrt(x2 + y2);
const theta = Math.atan2(x, y);
const sqrtR = Math.sqrt(r);
const thetaVal = theta / 2 + omega();
return [
sqrtR * Math.cos(thetaVal),
sqrtR * Math.sin(thetaVal)
];
};
Popcorn (variation 17)
Some variations rely on knowing the transform's affine coefficients; they're called "dependent variations." For this variation, we use and :
import { Coefs } from "./transform";
import { Variation } from "./variation";
export const popcorn =
({ c, f }: Coefs): Variation =>
(x, y) => [
x + c * Math.sin(Math.tan(3 * y)),
y + f * Math.sin(Math.tan(3 * x))
];
PDJ (variation 24)
Some variations have extra parameters we can choose; they're called "parametric variations." For the PDJ variation, there are four extra parameters:
import { Variation } from './variation'
export type PdjParams = {
a: number,
b: number,
c: number,
d: number
};
export const pdj =
({a, b, c, d}: PdjParams): Variation =>
(x, y) => [
Math.sin(a * y) - Math.cos(b * x),
Math.sin(c * x) - Math.cos(d * y)
]
Blending
Now, one variation is fun, but we can also combine variations in a process called "blending." Each variation receives the same and inputs, and we add together each variation's and outputs. We'll also give each variation a weight () that changes how much it contributes to the result:
The formula looks intimidating, but it's not hard to implement:
import { Variation } from "./variation";
export type Blend = [number, Variation][];
export function blend(
x: number,
y: number,
varFns: Blend
): [number, number] {
let [outX, outY] = [0, 0];
for (const [weight, varFn] of varFns) {
const [varX, varY] = varFn(x, y);
outX += weight * varX;
outY += weight * varY;
}
return [outX, outY];
}
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:
Try using the variation weights to figure out which parts of the image each transform controls.
Post transforms
Next, we'll introduce a second affine transform applied after variation blending. This is called a "post transform."
We'll use some new variables, but the post transform should look familiar:
import { applyCoefs, Coefs, Transform } from "../src/transform";
export const transformPost = (
transform: Transform,
coefs: Coefs
): Transform =>
(x, y) => {
[x, y] = transform(x, y);
return applyCoefs(x, y, coefs);
}
The image below uses the same transforms/variations as the previous fractal flame, but allows changing the post-transform coefficients:
If you want to test your understanding...
- What post-transform coefficients will give us the previous image?
- What post-transform coefficients will give us a mirrored image?
Final transforms
The last step is to introduce a "final transform" () that is applied regardless of which regular transform () 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.
After adding the final transform, our chaos game algorithm looks like this:
import { randomBiUnit } from "../src/randomBiUnit";
import { randomChoice } from "../src/randomChoice";
import { plotBinary as plot } from "../src/plotBinary";
import { Transform } from "../src/transform";
import { Props as WeightedProps } from "../1-introduction/chaosGameWeighted";
const quality = 0.5;
const step = 1000;
export type Props = WeightedProps & {
final: Transform,
}
export function* chaosGameFinal(
{
width,
height,
transforms,
final
}: Props
) {
let img =
new ImageData(width, height);
let [x, y] = [
randomBiUnit(),
randomBiUnit()
];
const pixels = width * height;
const iterations = quality * pixels;
for (let i = 0; i < iterations; i++) {
const [_, transform] =
randomChoice(transforms);
[x, y] = transform(x, y);
const [finalX, finalY] = final(x, y);
if (i > 20)
plot(finalX, finalY, img);
if (i % step === 0)
yield img;
}
yield img;
}
This image uses the same normal/post transforms as above, but allows modifying the coefficients and variations of the final transform:
Summary
Variations are the fractal flame algorithm's first major innovation. By blending variation functions and post/final transforms, we generate unique images.
However, these images are grainy and unappealing. In the next post, we'll clean up the image quality and add some color.