Perlin Noise Flower

Perlin Noise Flower

Generative Art is a lot of fun and I learned some stuff today from watching a workshop from pycon 2018 given by Emily Xie, particularly about the use of Perlin Noise to add natural looking randomisation to some code.

first

Here I've pretty much followed the example in the latter part of the workshop to draw a flower as a modified circle whose radius collapses over time but uses perlin noise to deform the circle on each frame. The undulation caused by the perlin noise generator gives the impression of leaves or petals and it really can some generate some pretty patterns.

second

Where I have modified the code Emily presented, it was to keep going after the drawing collapses into the centre, and reverses the direction of the radius so that it starts growing outwards again. Once it reaches the outside of the canvas, it re-contracts and starts again.


# From Emily Xie at PyCon 2018 https://www.youtube.com/watch?v=u3d-n41Tobw
# @emilyxxie on twitter

TOTAL_DEGREES = 360
radius = 0
inc_direction = -1
 
def setup():
  global displayWidth, displayHeight, radius
  size(displayWidth, displayHeight)
  background(0)
  noFill()
  stroke(255, 25)
  radius = height / 2


def draw():

  centre_x = width / 2
  centre_y = height / 2
    
  global TOTAL_DEGREES, radius, inc_direction
    
  # debug
  # background(0)
    
  beginShape()
  
  for degree in range(TOTAL_DEGREES):
    noiseFactor = noise(degree * 0.02, float(frameCount) / 150)
    # debug 
    # print(noiseFactor)

    # first guess - use normal random
#      x = centre_x + radius * cos(radians(degree)) + random(100)
#      y = centre_y + radius * sin(radians(degree)) + random(100)

    x = centre_x + radius * cos(radians(degree)) * noiseFactor
    y = centre_y + radius * sin(radians(degree)) * noiseFactor

    curveVertex(x, y)
      
  endShape(CLOSE)
    
  radius += inc_direction
    
  if radius == 0 or radius > (width / 2):
    inc_direction = -inc_direction
    saveFrame("flower-###.png")
#   noLoop()
    

third

Each time the flower reverses direction, I take a screenshot so you can see how the flower develops and creates ghostly layers that are reinforced the longer the sketch runs.