Monday, September 7, 2009

Vertical paths

Here is another version of the previous post, but in this case, we get a set of paths where the next point is always higher than the previous point. This uses the sort function.

import py2rhino as p2r
import random as rnd
#===============================================================================
# Parameters
#===============================================================================
num_points = 100
#===============================================================================
# Functions
#===============================================================================
def get_z(point):
return point[2]
#===============================================================================
# Main Script
#===============================================================================
#create some random points
points = []
for i in range(num_points):
x = rnd.randint(-25, 25)
y = rnd.randint(-25, 25)
z = rnd.randint(0,200)
points.append((x,y,z))
p2r.obj.Sphere.create((x,y,z), 0.5)

#sort the points, based on the x vale
points.sort()

#split the list into 4 sub lists
points1 = points[:num_points/4]
points2 = points[num_points/4:num_points/2]
points3 = points[num_points/2:100 - num_points/4]
points4 = points[100 - num_points/4:]

#sort all these lists, based on the z value
#in this case, the sort function is passed the get_z function
#this function returns the z vale (see above) of the point
points1.sort(key=get_z)
points2.sort(key=get_z)
points3.sort(key=get_z)
points4.sort(key=get_z)

#draw 4 vertical lines through these points
for pt_num in range((num_points/4) - 1):
p2r.obj.Line.create(points1[pt_num], points1[pt_num + 1])
p2r.obj.Line.create(points2[pt_num], points2[pt_num + 1])
p2r.obj.Line.create(points3[pt_num], points3[pt_num + 1])
p2r.obj.Line.create(points4[pt_num], points4[pt_num + 1])

print "done"

No comments:

Post a Comment