I’m trying to create a circle/ellipse/loop whose radius varies with the built-in noise function. I’m using Quil, a set of Clojure bindings to the Processing framework, so I’ll paste my code below and explain it since I suspect few if any here are Clojure people.
Problem
First, here is the output. I’ve limited it to a single loop and illustrated each vertex with a little dot.
The spike happens between the first, last, and second to last points. The first point is at a radius of 186.96024, the last point is at a radius of 202.97652, and the second to last point is at a radius of 180.59953.
The fact that the second to last point is inwards of the mean, the last point is outside of the mean leads me to believe there’s some kind of instability near that point in the noise function.
Code
Here is the code for how I generate this (explanation below):
(defn noise-path
[]
(let [divs 1000
nscale 0.008
mean-rad (* 0.75 win-rad)
]
(reduce
(fn
[acc div]
(let [{:keys [last-x last-y]} acc
noise (q/noise
(* nscale last-x)
(* nscale last-y))
max-rad (* 1.25 mean-rad)
min-rad (* 0.75 mean-rad)
new-rad (q/map-range noise 0 1 min-rad max-rad)
angle (* q/TWO-PI (/ div divs))
new-x (* new-rad (q/cos angle))
new-y (* new-rad (q/sin angle))]
(-> acc
(assoc :last-x new-x)
(assoc :last-y new-y)
(update :vertices
(fn [vs] (cons [new-x new-y] vs))))
))
{:last-x (* nscale (q/random 0 d))
:last-y (* nscale (q/random 0 d))
:vertices nil}
(range divs))))
I divide the ellipse into some number of angle increments (divs
, 1000 in this case), then iterate over them to generate a random radius at each angle.
To generate a random radius I first set a mean radius (mean-radius
, randomly selected in the real code, set to 0.75x the window radius for debugging), then from that mean radius I set an upper bound (max-rad
) and lower bound (min-rad
) within which the randomly selected radius will be selected. I use the (scaled) x- and y-coordinates of the previous point as the inputs to the noise function (q/noise
, which is just Processing’s noise
function).
When I’m “rendering” the path I just connect the vertices with lines and let end-shape
close the shape.
Thoughts
I haven’t been able to figure out why the last point freaks out. The last point isn’t always on the inside or outside of the mean, and it’s not pegged at the minimum or maximum radius.