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)

Addendum: 03/11/09

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. 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.

  2. 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.

  3. I wrote it in CL, and generated a nice wall paper in some 10 or 20 minutes. That’s very expensive calculations we have here, and memoizing doesn’t help because of the precision. Also most pictures I build are smooth. No abrupt gradient…
    Finally, I save pictures in pgm, and embedd the formula as a comment in it. Nice (but not space friendly).

  4. Note that my implementation doesn’t quite meet the spec. — it doesn’t iterate over the square (-1,1) but rather (0, size). The differences aren’t substantial in my tests.

Leave a Reply

Your email address will not be published. Required fields are marked *