Donnerstag, 20. Mai 2010

Python 048 Objects with a minimal distance and a path between them



# date: 20100520
# object that have a minimal distance between them
# using faster algorithm with the filter-function
# building a path between close objects

import random
from visual import *

Ncell = 4000
cell = [[10,10]] # first cell is given
ball = sphere(pos=(10,10))
size = Ncell/15 # size of the field
scene.center = (size/2, size/2, 0)
min_dist = 5 # minimal distance between two bodies
path_dist = min_dist*1.5 # draw a path if length is path-dist or shorter

for i in range(Ncell):
x = random.randint(1,size)
y = random.randint(1,size)
far = 1
# coarse filter
cell2 = filter(lambda k: abs(k[0] - x) < min_dist and abs(k[1] - y) < min_dist , cell) # excellent :)
# fine filter
for i in range(len(cell2)):
r = ((x-cell2[i][0])**2 + (y-cell2[i][1])**2)**0.5
if r < min_dist:
far = 0
pass
if far == 1:
cell.append([x,y]) # x-pos, y-pos
ball = sphere(pos=(x,y))

# building the path between two objects
for i in range(len(cell)):
# coarse filter
cell2 = filter(lambda k: abs(k[0] - cell[i][0]) < path_dist and abs(k[1] - cell[i][1]) < path_dist , cell)
# fine filter
for j in range(len(cell2)):
r = ((cell[i][0]-cell2[j][0])**2 + (cell[i][1]-cell2[j][1])**2)**0.5
if r < path_dist:
curve(pos=([cell[i][0],cell[i][1]],[cell2[j][0],cell2[j][1]]))


Here is the code with indentation
http://paste.pocoo.org/show/216285/

Keine Kommentare:

 
eXTReMe Tracker