# Random Art

Suppose we take an expression in x and y, built by nesting simple primitives: product, average, sin(pi*_), and cos(pi*_). These four operations map arguments in the range -1 to 1 to results in this same range. Thus, at any point with -1 <= x,y <= 1, our expression built from these primitives will return a value between -1 and 1. By scaling the answer to a grayscale value 0-255, we can plot the function in this 2-by-2 square. (From three such expressions, we can get red, green, and blue values for each point.)

What you get is some very nifty art!

(via PLT)

Noel posted some PLT Scheme code for implementation here:

#lang scheme/gui

(define (make-plotter drawing-fn size)
(lambda (canvas dc)
(for* ((x (in-range size))
(y (in-range size)))
(let* ([intensity (inexact->exact (round (* (/ (add1
(drawing-fn x y)) 2) 255)))]
[colour (make-object color% intensity intensity intensity)])
;;(printf "~a ~a ~a\n" intensity x y)
(send dc set-pen colour 1 'solid)
(send dc draw-point x y)))))

(define (draw-grayscale size drawing-fn)
(define frame (new frame%
[label "Random Art"]
[width size]
[height size]))
(define canvas (new canvas%
[parent frame]
[paint-callback (make-plotter drawing-fn size)]))
(send frame show #t))

(define (example1 x y)
;; some random stuff
(sin (* (sin (* (cos x) y))
(cos (*
(sin (* (sin y) (cos x)))
(cos (*  (cos (* y x)))))))))

(define (example2 x y)
(if (> x 200)
1
-1))

;;(draw-grayscale 400 example1)


## 7 thoughts on “Random Art”

1. Grant says:

Thanks Zach, nice.

2. Yavuz says:

I think most of the patterns in the samples are aliasing ‘artifacts’ of the high frequency components of the functions, in other words, Moiré patterns.

Essentially, any pixel plot of a continuous function is a discrete sampling, and frequency components of 2 x sampling frequency are folded into the < 2 x sampling frequency area.

I remember doing these in the mid 80’s on my first computer in BASIC. Of course, the idea didn’t originate with me; I got it from some computer magazine. I wonder how many times it has been rediscovered since then.

3. Some beautiful pics! Reminds me of the demo scene – very transcendental :]

In the image gallery it would be nice to have a link to the expression for that pic.

This almost demands a dedicated website and a javascript implementation to create these patterns in the browser then upload them and vote up the best ones. I can see it getting kids interested in math.

[ The math expressions/formulae tend to be long, but look like they have several recurring motifs – so maybe some kind of factoring into reusable components? lisp is great for building these up obviously (probably one can write some heuristics to reduce the existing ones, too). Perhaps working in higher level motifs might give intuition.. so the scheme scripts would give you a palette from which to compose interacting patterns. Just a random thought ]

very cool, thanks.

4. X y z says:

That ain’t random.