1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

Pebbles

Posted by Enrique P on Aug 03, 2008



I'm new to NodeBox and after playing around with the Ovals.py demo file, I ended up with this nice pebble generator.

If you have any ideas to improve this, please do. For example, how could we avoid the pebbles from overlapping and/or stay in the format?

# based on NodeBox demo file Ovals.py
# "pebblized" with the Supershape library
 
size(800,800)
 
# Set the background color to light gray
background(0.90)
 
# Load supershape library to "pebblize" the shapes
# # http://nodebox.net/code/index.php/Supershape
supershape = ximport("supershape")
 
# Load colors library
# http://nodebox.net/code/index.php/Colors
colors = ximport("colors")
 
# Set color to something brownish
clr = colors.rgb(80, 52, 32, a=50, range=255, name="brown")
 
# How many shapes
for i in range(15):
 
    # Set the stroke color to black
    stroke(0)
 
    # Set the stroke width
    strokewidth(0)
    
    # Set the fill to change slightly
    fill(clr.analog(angle=2, d=0.2))
 
    # Draw the oval onscreen. The width/height are both set to the
    # radius * 2 to get the diameter. The radius is subtracted from the
    # x and y coordinates to place the oval in the middle.
    # oval(random(WIDTH)-radius, random(HEIGHT)-radius, radius*2, radius*2)
    
    x = random(WIDTH)
    y = random(HEIGHT)
    
    w = h = random(20,150)
    
    m = random(2,3)     # circle is m = 0
    n1 = random(3,4)
    n2 = random(2,5)
    n3 = random(4,5)
 
    # path(x, y, w, h, m, n1, n2, n3, points=1000, percentage=1.0, range=pi*2)
 
    p = supershape.path(x, y, w, h, m, n1, n2, n3, points=100)
    rotate(random(90))
    drawpath(p)
Thanks //e

 


 
Posted by Theodore Test on Aug 04, 2008

A simple way to prevent overlap would be to keep a list of the bounding boxes of all pebbles added so far. Each time a new pebble is about to be made, test whether or not its area would intersect with any of the existing bounds. If there's an overlap, don't draw the pebble and move on to another possible pebble.

You can get the bounding box parameters of any Bezier path this way:

a = any_function_that_makes_a_path(arg_x,arg_y,arg_width,arg_height,etc)
b = a.bounds
o = b.origin
s = b.size

x = o.x
y = o.y
width = s.width
height = s.height

# More compactly / uglier...
x,y,width,height = a.bounds.origin.x,a.bounds.origin.y,a.bounds.size.width, a.bounds.size.height

The limitation of this method is that it limits you to rectangular exclusion areas.



Posted by Theodore Test on Aug 04, 2008

Whoops, I completely forgot about a much simpler way of testing for intersection -- the "intersects" method that comes with each Nodebox bezier path.

If you have two paths, path_a and path_b , you can just try:

path_a.intersects(path_b)
This will return a boolean saying whether or not they overlap. Whew, much more elegant. :)

 



Posted by Giorgio O. on Aug 07, 2008

you could start form the code that I posted here
http://www.nodebox.net/code/index.php/shared_2008-08-07-12-55-33
I'm also planning to implement a versione with svg paths instead of plain circles.

ciao
Giorgio

 


Posted by Enrique P on Sep 08, 2008

Giorgio: This page is currently empty. :-(



Posted by Tom De Smedt on Sep 08, 2008

I've fixed the link. My mistake related to an update of the forum software.