Hallo,
ich habe gestern in Python ein paar Funktionen geschrieben, die mir ein interessantes Bild ausrechnen sollen. Ich kriege es jedoch nicht hin, dass das Programm einigermaßen schnell läuft und am Ende muss auch die Auflösung darunter leiden
Wenn da mal jemand drüberschauen könnte, wäre das wirklich hilfreich.
import numpy as np
import matplotlib.pyplot as plt
from math import pi, sqrt, atan
#Punkt definieren
class point:
def __init__(self,x=0,y=0,z=0):
self.x = x
self.y = y
#Operationen
def __str__(self):
return "({0},{1})".format(self.x, self.y)
def __add__(self, other):
return point(self.x+other.x,self.y+other.y)
def __sub__(self, other):
return point(self.x - other.x, self.y - other.y)
def __abs__(self):
return sqrt(self.x**2 + self.y**2)
def __mul__(self, other):
if type(other) == point:
return point(self.x * other.x, self.y * other.y)
return point(self.x * other, self.y * other)
def __truediv__(self, other):
if type(other) == point:
return point(self.x / other.x, self.y / other.y)
return point(self.x / other, self.y / other)
def __neg__(self):
return point(-self.x,-self.y)
# weitere Funktionen für Punkte
def dis(PointA,PointB):
return abs(PointB-PointA)
def dir(PointA,PointB):
return (PointB-PointA)/dis(PointA,PointB)
#Funktion für den Wert
def grav_v(x,y,time=5,frames=60):
p = point(-1,0)
sp = point(x+1,y)
c = point(0,0)
sc = point(0,0)
#Vielleicht wegen des for-loops so langsam?
for k in range(frames*time):
try:
plus = dir(p,c) * 1/dis(p,c)**2 / frames
sp += plus
p += sp/frames
sc -= plus
c += sc/frames
except:
pass
return p
#Funktion für das Bild
def grav_c(a=-4, b=4, smoothness=1):
ROWS = []
p = 0
print(0)
#zwei for-loops...
for Y in np.linspace(a,b,int(200*smoothness)):
row = []
for X in np.linspace(a,b,int(200*smoothness)):
g = grav_v(X, Y)
row.append( (abs(atan(g.x)*2/pi), abs(atan(g.y)*2/pi), abs(atan(abs(g))*2/pi)) )
ROWS.append(row)
p += 1
print(p / (200 * smoothness))
plt.imshow(ROWS, extent=(a, b, a, b))
plt.show()
grav_c()