Donnerstag, 24. Dezember 2009
Python 042 Ball springt durchs Gelände
from visual import *
from math import *
import numpy as np
import time
scene.center=(10,0,5)
l = 2.5
#coordinate system
curve(pos=[(-l/2,0,0),(2*l,0,0)],radius=0.05,color=color.green)
curve(pos=[(0,-l/2,0),(0,2*l,0)],radius=0.05,color=color.blue)
curve(pos=[(0,0,-l/2),(0,0,2*l)],radius=0.05,color=color.red)
xwidth=40
zwidth=40
ebene = []
normal = []
#ground surface
for x in range(xwidth):
ebene.append([])
normal.append([])
for z in range(zwidth):
ebene[x].append(sin(x/4.0)+sin(z/4.0)) #FUNCTION
normal[x].append(0)
#draw ground surface
for x in range(xwidth-1):
for z in range(zwidth-1):
curve(pos=[(x,ebene[x][z],z),(x+1,ebene[x+1][z],z)],radius=.1,color=color.green)
curve(pos=[(x,ebene[x][z],z),(x,ebene[x][z+1],z+1)],radius=.1,color=color.green)
#ball
ball = sphere(pos=(8,4,11),radius=.3)
vektor = [-0,-0.02,-0]
ball.trail = curve(color=ball.color, radius=.1)
#calculate surface normal
def surfacenormal(pointx,pointz):
pointy = sin(pointx/4.0)+sin(pointz/4.0) #FUNCTION
start = (pointx,pointy,pointz)
#derivative and cross product of fw and fu
fu = np.array([1,0.25*cos(pointx/4.0),0]) #FUNCTION
fw = np.array([0,0.25*cos(pointz/4.0),1]) #FUNCTION
fv = np.cross(fw,fu)
end = (fv[0]+pointx,fv[1]+pointy,fv[2]+pointz)
normal = curve(pos=[start, end])
return normal.pos
t=0
veca = [0,0,0]
vecc = [0,0,0]
#movement
while 1:
t += 1
ball.pos = ball.pos + vektor
ball.trail.append(pos=ball.pos)
if (ball.pos.y <= (sin(int(ball.pos.x/4.0))+sin(int(ball.pos.z/4.0)))): #FUNCTION
veca[0] = -vektor[0] #incoming vector
veca[1] = -vektor[1]
veca[2] = -vektor[2]
vecb = surfacenormal(int(ball.pos.x),int(ball.pos.z)) #surface normal
vecc[0] = vecb[1][0]-vecb[0][0]
vecc[1] = vecb[1][1]-vecb[0][1]
vecc[2] = vecb[1][2]-vecb[0][2]
vecref = 2*norm(vecc)-norm(veca)
vektor = vecref * mag(veca)
vektor[1]=(vektor[1]-0.0001)*0.999 #Daempfung (vereinfacht)
time.sleep(.001)
Python 041 Winkel zwischen landender Kugel und Oberflächennormale
Das Programm gibt den Winkel an der zwischen dem Vektor einer auf den Boden fallenden Kugel und der Oberflächennormale liegt.
from visual import curve, scene, sphere, color, array
from math import *
import numpy as np
import time
scene.center=(10,0,5)
l = 2.5
#coordinate system
curve(pos=[(-l/2,0,0),(2*l,0,0)],radius=0.05,color=color.green)
curve(pos=[(0,-l/2,0),(0,2*l,0)],radius=0.05,color=color.blue)
curve(pos=[(0,0,-l/2),(0,0,2*l)],radius=0.05,color=color.red)
xwidth=22
zwidth=11
ebene = []
normal = []
#ground surface
for x in range(xwidth):
ebene.append([])
normal.append([])
for z in range(zwidth):
ebene[x].append(sin(x/4.0)+sin(z/4.0)) #FUNCTION
normal[x].append(0)
#draw ground surface
for x in range(xwidth-1):
for z in range(zwidth-1):
curve(pos=[(x,ebene[x][z],z),(x+1,ebene[x+1][z],z)],radius=.1,color=color.green)
curve(pos=[(x,ebene[x][z],z),(x,ebene[x][z+1],z+1)],radius=.1,color=color.green)
#ball
ball = sphere(pos=(10,5,5),radius=.3)
vektor = [-0,-0.02,-0]
#calculate surface normal
def surfacenormal(pointx,pointz):
pointy = sin(pointx/4.0)+sin(pointz/4.0) #FUNCTION
start = (pointx,pointy,pointz)
#derivative and cross product of fw and fu
fu = np.array([1,0.25*cos(pointx/4.0),0]) #FUNCTION
fw = np.array([0,0.25*cos(pointz/4.0),1]) #FUNCTION
fv = np.cross(fw,fu)
end = (fv[0]+pointx,fv[1]+pointy,fv[2]+pointz)
normal = curve(pos=[start, end])
#print(normal.pos)
return normal.pos
t=0
veca = [0,0,0]
vecc = [0,0,0]
#movement
while 1:
t += 1
ball.pos = ball.pos + vektor
if (ball.pos.y <= (sin(int(ball.pos.x/4.0))+sin(int(ball.pos.z/4.0)))): #FUNCTION #maybe round()
veca[0] = -vektor[0] #incoming vector
veca[1] = -vektor[1]
veca[2] = -vektor[2]
vecb = surfacenormal(int(ball.pos.x),int(ball.pos.z)) #surface normal
vecc[0] = vecb[1][0]-vecb[0][0]
vecc[1] = vecb[1][1]-vecb[0][1]
vecc[2] = vecb[1][2]-vecb[0][2]
zaehler = veca[0]*vecc[0]+veca[1]*vecc[1]+veca[2]*vecc[2]
nenner = (np.sqrt((veca[0])**2+(veca[1])**2+(veca[2])**2)*np.sqrt((vecc[0])**2+(vecc[1])**2+(vecc[2])**2))
cosalpha = zaehler/nenner
alpha = np.arccos(cosalpha)
winkel = np.degrees(alpha)
print(winkel)
time.sleep(.01)
from visual import curve, scene, sphere, color, array
from math import *
import numpy as np
import time
scene.center=(10,0,5)
l = 2.5
#coordinate system
curve(pos=[(-l/2,0,0),(2*l,0,0)],radius=0.05,color=color.green)
curve(pos=[(0,-l/2,0),(0,2*l,0)],radius=0.05,color=color.blue)
curve(pos=[(0,0,-l/2),(0,0,2*l)],radius=0.05,color=color.red)
xwidth=22
zwidth=11
ebene = []
normal = []
#ground surface
for x in range(xwidth):
ebene.append([])
normal.append([])
for z in range(zwidth):
ebene[x].append(sin(x/4.0)+sin(z/4.0)) #FUNCTION
normal[x].append(0)
#draw ground surface
for x in range(xwidth-1):
for z in range(zwidth-1):
curve(pos=[(x,ebene[x][z],z),(x+1,ebene[x+1][z],z)],radius=.1,color=color.green)
curve(pos=[(x,ebene[x][z],z),(x,ebene[x][z+1],z+1)],radius=.1,color=color.green)
#ball
ball = sphere(pos=(10,5,5),radius=.3)
vektor = [-0,-0.02,-0]
#calculate surface normal
def surfacenormal(pointx,pointz):
pointy = sin(pointx/4.0)+sin(pointz/4.0) #FUNCTION
start = (pointx,pointy,pointz)
#derivative and cross product of fw and fu
fu = np.array([1,0.25*cos(pointx/4.0),0]) #FUNCTION
fw = np.array([0,0.25*cos(pointz/4.0),1]) #FUNCTION
fv = np.cross(fw,fu)
end = (fv[0]+pointx,fv[1]+pointy,fv[2]+pointz)
normal = curve(pos=[start, end])
#print(normal.pos)
return normal.pos
t=0
veca = [0,0,0]
vecc = [0,0,0]
#movement
while 1:
t += 1
ball.pos = ball.pos + vektor
if (ball.pos.y <= (sin(int(ball.pos.x/4.0))+sin(int(ball.pos.z/4.0)))): #FUNCTION #maybe round()
veca[0] = -vektor[0] #incoming vector
veca[1] = -vektor[1]
veca[2] = -vektor[2]
vecb = surfacenormal(int(ball.pos.x),int(ball.pos.z)) #surface normal
vecc[0] = vecb[1][0]-vecb[0][0]
vecc[1] = vecb[1][1]-vecb[0][1]
vecc[2] = vecb[1][2]-vecb[0][2]
zaehler = veca[0]*vecc[0]+veca[1]*vecc[1]+veca[2]*vecc[2]
nenner = (np.sqrt((veca[0])**2+(veca[1])**2+(veca[2])**2)*np.sqrt((vecc[0])**2+(vecc[1])**2+(vecc[2])**2))
cosalpha = zaehler/nenner
alpha = np.arccos(cosalpha)
winkel = np.degrees(alpha)
print(winkel)
time.sleep(.01)
Mittwoch, 23. Dezember 2009
Python 040 Normalenvektoren einer Fläche / surface normals
from visual import curve, scene, sphere, color, array
from math import *
xwidth=22
zwidth=11
ebene = []
for x in range(xwidth):
ebene.append([])
for z in range(zwidth):
ebene[x].append(sin(x/4.0)+sin(z/4.0)) #FUNCTION
for x in range(xwidth-1):
for z in range(zwidth-1):
curve(pos=[(x,ebene[x][z],z),(x+1,ebene[x+1][z],z)],radius=.1,color=color.green)
curve(pos=[(x,ebene[x][z],z),(x,ebene[x][z+1],z+1)],radius=.1,color=color.green)
for i in range(xwidth):
pointx = i
for j in range(zwidth):
pointz = j
pointy = sin(pointx/4.0)+sin(pointz/4.0) #FUNCTION
start = (pointx,pointy,pointz)
#derivative
fu = [1,0.25*cos(pointx/4.0),0] #FUNCTION
fw = [0,0.25*cos(pointz/4.0),1] #FUNCTION
fv = [0,0,0] #will change next line
#cross product of fw and fu
fv[0] = fw[1]*fu[2]-fu[1]*fw[2]
fv[1] = fw[2]*fu[0]-fu[2]*fw[0]
fv[2] = fw[0]*fu[1]-fu[0]*fw[1]
end = (fv[0]+pointx,fv[1]+pointy,fv[2]+pointz)
normal = curve(pos=[start, end])
Montag, 21. Dezember 2009
Python 039: Partielle Ableitung bzw. Gradient
from sympy import *
import numpy
x = Symbol('x')
y = Symbol('y')
gleichung = x**2+y**2
xx = diff(gleichung, x)
yy = diff(gleichung, y)
vektor = numpy.array((xx, yy))
print vektor
x = 1
y = 1
print(xx.evalf)
import numpy
x = Symbol('x')
y = Symbol('y')
gleichung = x**2+y**2
xx = diff(gleichung, x)
yy = diff(gleichung, y)
vektor = numpy.array((xx, yy))
print vektor
x = 1
y = 1
print(xx.evalf)
Python 038: Faltung einer Dreiecksfunktion mit einer Rechteckfunktion
from numpy import *
from pylab import *
#print convolve.__doc__
#a = arange(12)
a = [0,0,0,1,2,3,4,5,6,7,8,7,6,5,4,3,2,1,0,0,0] #Dreieck
b = [0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0]
h = convolve(a,b,mode=0)
i = convolve(a,b,mode=1)
#j = convolve(a,b,mode=2)
print(h)
print(i)
#print(j)
plot(i)
hold(True)
#plot(j)
grid(True)
show()
from pylab import *
#print convolve.__doc__
#a = arange(12)
a = [0,0,0,1,2,3,4,5,6,7,8,7,6,5,4,3,2,1,0,0,0] #Dreieck
b = [0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0]
h = convolve(a,b,mode=0)
i = convolve(a,b,mode=1)
#j = convolve(a,b,mode=2)
print(h)
print(i)
#print(j)
plot(i)
hold(True)
#plot(j)
grid(True)
show()
Abonnieren
Posts (Atom)