Donnerstag, 27. Mai 2010

Python 049 Laplace-algorithm for calculating voltage between different potentials numerically



The white edges have a potential of 0 V. The two bodies inside have a potential of 30 V. The voltage in the space between has been calculated numerically with the laplace-algorithm.

from visual import *

ball = []
v = [] # v = voltage
vnew = [] # new calculated voltage
unchangeableflag = [] # if 1 --> value cannot be changed
size = 35 # size of the field
scene.center = (size/2, size/2, 0)
vmax = 100.0

# initalize
for n in range(size*size+size+1):
v.append(0)
vnew.append(0)
unchangeableflag.append(0)

# draw raster
for x in range(size):
for y in range(size):
ball.append([]) # x-pos, y-pos
ball[x*size+y] = sphere(pos=(x,y), radius = 0.4)

# set unchangeable potentials
for n in range(size):
v[n] = 0.0
unchangeableflag[n] = 1

for n in range(size):
v[n*size-1] = 0.0
unchangeableflag[n*size-1] = 1

for n in range(size):
v[n*size] = 0.0
unchangeableflag[n*size] = 1

for n in range(size):
v[size*size-size+n] = 0.0
unchangeableflag[size*size-size+n] = 1

v[250] = vmax
unchangeableflag[250] = 1
v[475] = vmax
unchangeableflag[475] = 1
print "set unchangeable potentials!"

# laplace-algorithm
for a in range(1000000):
for x in range(size):
for y in range(size):
if x % size != 1:
pass
if x % size != size - 1:
pass
if y % size != 1:
pass
if y % size != size - 1:
pass
if unchangeableflag[x*size+y] == 0:
vnew[x*size+y] = (v[(x+1)*size+y] + v[(x-1)*size+y] + v[x*size+y+1] + v[x*size+y-1]) / 4.0

# vnew --> v
colorfactor = 10.0
for x in range(size):
for y in range(size):
if unchangeableflag[x*size+y] == 0:
v[x*size+y] = vnew[x*size+y]
ball[x*size+y].color = (v[x*size+y]/10,0.2,0.2)
if v[x*size+y] >= 0.2 * vmax:
ball[x*size+y].color = ((abs(v[x*size+y]/colorfactor/5.0)),0,0)
else:
ball[x*size+y].color = (0,0,(abs(v[x*size+y]/colorfactor)))

Code with identation:

http://paste.pocoo.org/show/218999/

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/

Mittwoch, 12. Mai 2010

Python 047 pyprocessing + mouseevent + color


#You can easily use mouse-events in pyprocessing. Here I combine mouse position with a RGB-color-value.

from pyprocessing import *

size(900,600)
background(1, 1, 1)
strokeWeight(6)
x1 = 0
y1 = 0
x2 = 0
y2 = 0
x = [0,0,0]
y = [0,0,0]

def draw():
if mouse.pressed:
x1 = mouse.x
y1 = mouse.y
if mouse.pressed:
x2 = pmouse.x
y2 = pmouse.y
stroke(x1,y1,1)
line(x1, y1, x2, y2)
run()

Here is the code with identation:
http://bit.ly/c4QD2o
 
eXTReMe Tracker