Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung | |||
ws1920:elfter_gruppentermin [2020/02/13 16:24] hannariegel |
ws1920:elfter_gruppentermin [2020/02/13 16:25] (aktuell) hannariegel |
||
---|---|---|---|
Zeile 101: | Zeile 101: | ||
# fuer mint gruen cmap=YlGn | # fuer mint gruen cmap=YlGn | ||
plt.show() | plt.show() | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | # -*- coding: utf-8 -*- | ||
+ | """ | ||
+ | Created on Thu Jan 30 15:06:55 2020 | ||
+ | |||
+ | @author: HP | ||
+ | """ | ||
+ | |||
+ | import random | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | from PIL import Image | ||
+ | from math import floor, ceil | ||
+ | import time | ||
+ | |||
+ | # Größe der Matrix | ||
+ | # Die Matrix wird zufällig mit Nullen und Einsen gefüllt | ||
+ | |||
+ | |||
+ | def Tripleeinheitsmatrix(zeilen,spalten): | ||
+ | tem = np.zeros ((zeilen,spalten)) | ||
+ | for i in range(0, zeilen): | ||
+ | for j in range(0, spalten): | ||
+ | if i==j: | ||
+ | tem[(i,j)] = 1 | ||
+ | else: | ||
+ | tem[(i,j)] = 0 | ||
+ | | ||
+ | for i in range(0, zeilen): | ||
+ | for j in range(0, spalten): | ||
+ | if i==(j+1): | ||
+ | tem[(i,j)] = 1 | ||
+ | for i in range(0, zeilen): | ||
+ | for j in range(0, spalten): | ||
+ | if j==(i+1): | ||
+ | tem[(i,j)] = 1 | ||
+ | | ||
+ | tem[0,tem.shape[1]-1] = 1 | ||
+ | tem[tem.shape[1]-1,0] = 1 | ||
+ | |||
+ | return tem | ||
+ | | ||
+ | |||
+ | def BerechneAnzahlNachbarn(zustand): | ||
+ | z = np.dot(tem, zustand) | ||
+ | k = np.dot(z, tem) | ||
+ | anzahlNachbarn = k - zustand | ||
+ | | ||
+ | return anzahlNachbarn | ||
+ | |||
+ | |||
+ | def BerechneNeuenZustand(zustand, anzahlNachbarn): | ||
+ | #Ursprungszelle Tod + genau 3 lebende Nachbarn = lebend | ||
+ | #Ursprungszelle Lebend + genau 1 lebenden Nachbarn = tot | ||
+ | #Ursprungszelle Lebend + 2 o. 3 Nachbarn = lebend | ||
+ | #Ursprungszelle lebend + 4 oder mehr Nachbarn = tot | ||
+ | | ||
+ | for i in range(0, zeilen): | ||
+ | for j in range(0, spalten): | ||
+ | if zustand[(i,j)] == 1 and (anzahlNachbarn[(i,j)] in range(2) or anzahlNachbarn[(i,j)] in range(4,9)): | ||
+ | zustand[(i,j)]=0 | ||
+ | elif zustand[(i,j)] == 0 and anzahlNachbarn[(i,j)]==3: | ||
+ | zustand[(i,j)]=1 | ||
+ | | ||
+ | return zustand | ||
+ | |||
+ | def Schritt(): | ||
+ | global zustand | ||
+ | |||
+ | #start_time = time.time() | ||
+ | anzahlNachbarn = BerechneAnzahlNachbarn(zustand) | ||
+ | #print('Berechne Nachbarn:', time.time() - start_time) | ||
+ | | ||
+ | #start_time = time.time() | ||
+ | ergebnis = BerechneNeuenZustand(zustand, anzahlNachbarn) | ||
+ | #print('Berechne neuen Zustand:', time.time() - start_time) | ||
+ | #cmap = cmap * zustand + zustand | ||
+ | |||
+ | return ergebnis | ||
+ | | ||
+ | def Wiederhole(t): | ||
+ | for i in range(t): | ||
+ | Schritt() | ||
+ | |||
+ | def MachQuadrat(pixel): | ||
+ | breite = pixel.shape[0] | ||
+ | hoehe = pixel.shape[1] | ||
+ | size = max(breite, hoehe) | ||
+ | |||
+ | links = floor((size - breite) / 2) | ||
+ | rechts = ceil((size - breite) / 2) | ||
+ | oben = floor((size - hoehe) / 2) | ||
+ | unten = ceil((size - hoehe) / 2) | ||
+ | |||
+ | pad_width = ((links, rechts), (oben, unten)) | ||
+ | pixel = np.pad(pixel, pad_width, mode='constant', constant_values=0) | ||
+ | return pixel | ||
+ | |||
+ | #t= int(input('Gib die Zeitschritte ein: ')) | ||
+ | def Initialisiere(): | ||
+ | global zustand, tem, zeilen, spalten, cmap | ||
+ | |||
+ | modus = int(input('Gib den Modus 1 (Zufall), 2 (alternierend), 3 (Datei), 4 (Bild) oder 5 (Leer) ein: ')) | ||
+ | |||
+ | if modus==1 or modus==2 or modus==5: | ||
+ | spalten = zeilen = int(input('Gib die Anzahl der Zeilen/Spalten ein: ')) | ||
+ | |||
+ | #zustand = np.zeros | ||
+ | if modus==1: | ||
+ | zustand = np.round(np.random.random((zeilen,spalten))).astype(int) | ||
+ | | ||
+ | elif modus==2: | ||
+ | zustand=np.zeros((zeilen,spalten)) | ||
+ | for i in range(0,zeilen): | ||
+ | for j in range (0,spalten): | ||
+ | if j%2==0: | ||
+ | zustand[(i,j)]=0 | ||
+ | else: | ||
+ | zustand[(i,j)]=1 | ||
+ | | ||
+ | if zustand[(int(zeilen/2),int(spalten/2))]==0: | ||
+ | zustand[(int(zeilen/2),int(spalten/2))]=1 | ||
+ | zustand[(int((zeilen/2)+1),int(spalten/2))]=1 | ||
+ | | ||
+ | else: | ||
+ | zustand[(int(zeilen/2),int(spalten/2))]=0 | ||
+ | zustand[(int((zeilen/2)+1),int(spalten/2))]=0 | ||
+ | | ||
+ | elif modus==3: | ||
+ | DateiName = input('was willste oeffnen? ') | ||
+ | Datei = open(DateiName) | ||
+ | | ||
+ | DateiAlsListe = [] | ||
+ | for line in Datei: | ||
+ | DateiAlsListe.append([int(c) for c in list(line.rstrip())]) | ||
+ | DateiAlsMatrix = np.array(DateiAlsListe) | ||
+ | | ||
+ | zustand = MachQuadrat(DateiAlsMatrix) | ||
+ | zeilen = spalten = zustand.shape[0] | ||
+ | | ||
+ | elif modus==4: | ||
+ | dateiname = input('Gib den Bildpfad ein: ') | ||
+ | bild = Image.open(dateiname) | ||
+ | bild = bild.convert('1') | ||
+ | bild.thumbnail((500,500)) | ||
+ | |||
+ | pixel = np.array(bild) | ||
+ | |||
+ | zustand = MachQuadrat(pixel) | ||
+ | zeilen = spalten = zustand.shape[0] | ||
+ | | ||
+ | elif modus==5: | ||
+ | zustand = np.zeros((zeilen,spalten)).astype(int) | ||
+ | |||
+ | tem = Tripleeinheitsmatrix(zeilen, spalten) | ||
+ | #Wiederholungen(t) | ||
</code> | </code> |