Thursday 29 November 2012

Yet another Generative Design Sketch in Processing.py

This is a sketch you definetly need to run to fully appreciate it.
   1 import generativedesign.Node as Node
   2 import generativedesign.Spring as Spring
   3 import generativedesign.Attractor as Attractor
   4 
   5 """
   6 part of the example files of the generativedesign library. 
   7 shows how to use the classes Node, Spring and Attractor in 3d space.
   8 """
   9 
  10 def setup():
  11     """
  12     Setup Sketch smooth should be called here
  13     """
  14     size(512, 512, P3D)
  15     lights()
  16     global nodeA, nodeB, attractor, spring
  17     smooth(8)
  18     fill(0)
  19     nodeA = Node(random(width), random(height), random(-200, 200))
  20     nodeB = Node(random(width), random(height), random(-200, 200))
  21     nodeA.setStrength(-2)
  22     nodeB.setStrength(-2)
  23     nodeA.setDamping(0.1)
  24     nodeB.setDamping(0.1)
  25     nodeA.setBoundary(0, 0, -300, width, height, 300)
  26     nodeB.setBoundary(0, 0, -300, width, height, 300)
  27     
  28     spring = Spring(nodeA, nodeB)
  29     spring.setStiffness(0.7)
  30     spring.setDamping(0.9)
  31     spring.setLength(100)
  32     
  33     attractor = Attractor(width/2, height/2, 0)
  34     attractor.setMode(Attractor.SMOOTH)
  35     attractor.setRadius(200)
  36     attractor.setStrength(5)
  37 
  38 
  39 def draw():
  40     """
  41     The processing draw loop
  42     """
  43     background(50, 50, 200)
  44     lights()
  45     ambientLight(100, 100, 100)
  46     ambient(30)
  47     specular(30)
  48     if (mousePressed == True):
  49         nodeA.x = mouseX
  50         nodeA.y = mouseY
  51         nodeA.z = mouseY - 256
  52     # attraction between nodes
  53     nodeA.attract(nodeB)
  54     nodeB.attract(nodeA)
  55     # update spring
  56     spring.update()
  57     # attract
  58     attractor.attract(nodeA)
  59     attractor.attract(nodeB)
  60     # update node positions
  61     nodeA.update()
  62     nodeB.update()
  63     # draw attractor
  64     stroke(0, 50)
  65     strokeWeight(1)
  66     noFill()
  67     line(attractor.x-10, attractor.y, attractor.x+10, attractor.y)
  68     line(attractor.x, attractor.y-10, attractor.x, attractor.y+10)
  69     ellipse(attractor.x, attractor.y, attractor.radius*2, attractor.radius*2)
  70     # draw spring
  71     stroke(255, 0, 0, 255)
  72     strokeWeight(4)
  73     line(nodeA.x, nodeA.y, nodeA.z, nodeB.x, nodeB.y, nodeB.z)
  74     # draw nodes
  75     noStroke()
  76     fill(212)
  77     pushMatrix()
  78     translate(nodeA.x, nodeA.y, nodeA.z)
  79     sphere(20)
  80     popMatrix()
  81     pushMatrix()
  82     translate(nodeB.x, nodeB.y, nodeB.z)
  83     sphere(20)
  84     popMatrix()

No comments:

Post a Comment