attractor-0
2012. 06. 12.

The good ol' Peter de Jong attractor.

This is a port of my old sketches written in both Processing and Flash back in 2004. I must mention, however, that they were not my original work, but just another re-implementation of what already had been done by others. Check out the links below.

Anyway, my contribution here, if I may say so, is this simple yet effective colorization method.

Click on the screen to randomize the four parameters of the attractor. (Not all combinations generate visible image. If you don't see anything coming up, just click again.) Press space bar to pause and resume drawing. Press enter key to toggle background color.

» launch attractor-0

» launch attractor-0-curve
p5 = processing
a = b = c = d = undefined
x = y = s = undefined
cr = cg = cb = undefined
cmm = [undefined, undefined]
l = true
bg = 255
fg = 0

font = '/fonts/rough_typewriter.otf'
preload = fonts: [font]

setup = ->
  textFont createFont(font, 32), 16
  frameRate 30
  mousePressed()

mousePressed = ->
  randomize()
  restart()

randomize = ->
  a = random(-3, 3)
  b = random(-3, 3)
  c = random(-3, 3)
  d = random(-3, 3)
  cmm[0] = random(0, 70)
  cmm[1] = random(cmm[0], 200)
  cr = random(cmm[0], cmm[1])
  cg = random(cmm[0], cmm[1])
  cb = random(cmm[0], cmm[1])

restart = ->
  size $(window).width(), $(window).height()

  x = y = 0
  s = min(p5.width, p5.height) / 5
  l = true

  background bg

  fill fg
  text "a = #{a}\nb = #{b}\nc = #{c}\nd = #{d}", 20, 30

  noFill()
  p5.loop()

draw = ->
  translate p5.width / 2, p5.height / 2
  for k in [0...300]
    beginShape()
    for i in [0...4]
      points = []
      dists = []
      for j in [0...3]
        [x2, y2] = [sin(a * y) - cos(b * x), sin(c * x) - cos(d * y)]
        dists.push dist(x2, y2, x, y)
        [x, y] = [x2, y2]
        points.push [x, y] if j == 0

      stroke(
        min(cr * dists[0], 255),
        min(cg * dists[1], 255),
        min(cb * dists[2], 255), 5)

      for p in points
        curveVertex p[0] * s, p[1] * s
    endShape()

keyPressed = ->
  if keyCode() == 32
    l = !l
    if l
      p5.loop()
    else
      noLoop()
  else if keyCode() == ENTER || keyCode() == RETURN
    if bg == 0
      bg = 255
      fg = 0
    else
      bg = 0
      fg = 255
    restart()
» capture | close