Showing posts with label subclass. Show all posts
Showing posts with label subclass. Show all posts

Thursday, 29 November 2012

Generative Design Sketch (How to subclass)

Here is an excellent example of sub-classing a java class from a vanilla processing library, and running it with processing.py. Currently you need to put the generativedesign library in the processing.py/libraries folder (but that might change to pick up the library from the sketchbook/libraries folder).
   1 import generativedesign.Mesh as Mesh
   2 
   3 """
   4 part of the example files of the generativedesign library.
   5 shows how to use the mesh class, if you want to define your own forms.
   6 Modified to run with processing.py
   7 """
   8 
   9 def setup():
  10     """
  11     Setup the drawing style
  12     """
  13     size(1000, 1000, P3D)    
  14     colorMode(HSB, 360, 100, 100, 100)
  15     noStroke()
  16     global myMesh
  17     myMesh = MyOwnMesh(this)
  18     myMesh.setUCount(100)
  19     myMesh.setVCount(100)
  20     myMesh.setColorRange(193, 193, 30, 30, 85, 85, 100)
  21     myMesh.update()
  22 
  23 def draw():
  24     """
  25     The processing draw loop
  26     """
  27     background(255)    
  28     # setup lights
  29     colorMode(RGB, 255, 255, 255, 100)
  30     lightSpecular(255, 255, 255) 
  31     directionalLight(255, 255, 255, 1, 1, -1) 
  32     shininess(5.0)
  33     # setup view    
  34     translate(width/2, height/2)
  35     scale(180)
  36     myMesh.draw()    
  37 
  38 
  39 class MyOwnMesh(Mesh):
  40     """
  41     A custom python class that extend the original Mesh class
  42     """
  43     
  44     def __init__(self, theParent):
  45         Mesh.__init__(self, theParent)
  46         
  47         
  48     # just override this function and put your own formulas inside
  49     def calculatePoints(self, u,  v):
  50         A = 2/3.0
  51         B = sqrt(2)      
  52         x = A *  (cos(u) * cos(2 * v) + B * sin(u) * cos(v)) * cos(u) / (B - sin(2 * u) * sin(3 * v))
  53         y = A * (cos(u) * sin(2 * v) - B * sin(u) * sin(v)) * cos(u) / (B - sin(2 * u) * sin(3 * v))
  54         z = B * cos(u) * cos(u) / (B - sin(2 * u) * sin(3 * v))
  55         return PVector(x, y, z)