Last active
June 13, 2022 12:16
-
-
Save githubuser1983/aaea24744998f3b263519048136ee49d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle as t | |
import math | |
WIDTH, HEIGHT = 800, 800 | |
OFFSET = -WIDTH // 2, -HEIGHT // 2 | |
import numpy as np | |
class Point: | |
"""convenience for point arithmetic | |
""" | |
def __init__(self, x=0, y=0): | |
self.x, self.y = x, y | |
def __add__(self, other): | |
return Point(self.x + other.x, self.y + other.y) | |
def __mul__(self,l): | |
return Point(l*self.x,l*self.y) | |
def __iter__(self): | |
yield self.x | |
yield self.y | |
def __str__(self): | |
return "("+str(self.x)+","+str(self.y)+")" | |
def draw_triangle(triangle): | |
import numpy as np | |
w = np.exp(2*pi.n()*np.complex(0,1)/3) | |
pa,pb,pc,filled = triangle | |
pa,pb,pc = pp(pa[0]+w*pa[1]),pp(pb[0]+w*pb[1]),pp(pc[0]+w*pc[1]) | |
pa,pb,pc = Point(*pa)+OFS,Point(*pb)+OFS,Point(*pc)+OFS | |
if filled: | |
t.fillcolor("white") | |
else: | |
t.fillcolor("blue") | |
#t.begin_fill() | |
t.penup() | |
t.goto(pa) | |
t.pendown() | |
t.begin_fill() | |
t.goto(pb) | |
t.goto(pc) | |
t.goto(pa) | |
t.end_fill() | |
t.penup() | |
def pp(x): | |
num_triangles = 25 | |
X = 40 | |
return (X*x.real,X*x.imag) | |
def get_neigh(p,q,r): | |
x,y = p | |
pp = (x-1,y-1) | |
qq = (x-1,y+1) | |
rr = (x+1,y-1) | |
neigh = [(p,q,r),(p,q,rr),(r,qq,p),(pp,q,r)] | |
return neigh | |
def get_neigh2(r,p,q): | |
x,y = r | |
pp = (x,y+1) | |
qq = (x,y-1) | |
rr = (x-2,y+1) | |
neigh = [(r,p,q),(r,p,qq),(q,pp,r),(rr,p,q)] | |
return neigh | |
class Cell: | |
def __init__(self,p,q,r,is_on,neigh): | |
self.p = p | |
self.q = q | |
self.r = r | |
#neigh = get_neigh(p,q,r) | |
#self.neighbors = [conv(x,25) for x in neigh] | |
self.is_on = is_on | |
#self.points = frozenset([p,q,r]) | |
self.qq = [is_on] | |
def dist(self,other): | |
return np.abs(mp-other.mp) | |
def is_neighbor(self,other): | |
return self.dist(other)<=1.0 | |
def __hash__(self): | |
return hash((self.p,self.q,self.r)) | |
def conv(n,N=5): | |
n = list(n) | |
#if len(n)==3: | |
return frozenset([(n[0][0]%N,n[0][1]%N),(n[1][0]%N,n[1][1]%N),(n[2][0]%N,n[2][1]%N)]) | |
def create_cells(N): | |
import numpy as np | |
cells = dict([]) | |
from itertools import product | |
G = Graph(loops=True) | |
rr = range(N) | |
X = product(rr,rr) | |
for x,y in X: | |
qq = 0.00125 | |
p,q,r = (x,y),(x,y-1),(x-1,y) | |
#print(p,q,r) | |
neigh = get_neigh(p,q,r) | |
#for n in neigh: | |
is_on = np.random.choice([False,True],p=[1-qq,qq]) | |
c = Cell(p,q,r,is_on,[]) | |
cc = conv((p,q,r),N) | |
for n in neigh: | |
e = (cc,conv(n,N)) | |
if e in G.edges(): | |
continue | |
else: | |
G.add_edge(e) | |
#G.add_edge(cc,conv(n,N)) | |
cells[conv(n,N)] = Cell(*n,np.random.choice([False,True],p=[1-qq,qq]),[]) | |
#print("n = ",cc) | |
#cells[cc] = c | |
r2,p2,q2 = (x-1,y),(x-2,y),(x-2,y+1) | |
#print(p2,q2,r2) | |
neigh2 = get_neigh2(r2,p2,q2) | |
#for n in neigh: | |
is_on = np.random.choice([False,True],p=[1-qq,qq]) | |
c2 = Cell(r2,p2,q2,is_on,[]) | |
cc2 = conv((r2,p2,q2),N) | |
#print(len(neigh2)) | |
for n in neigh2: | |
e = (cc2,conv(n,N)) | |
if e in G.edges(): | |
continue | |
else: | |
G.add_edge(e) | |
cells[conv(n,N)] = Cell(*n,np.random.choice([False,True],p=[1-qq,qq]),[]) | |
#print("n = ",cc2) | |
#cells[cc2] = c2 | |
#G.add_edge((conv((p,q,r),N),conv((p2,q2,r2),N))) | |
return cells,G | |
def prepare_next_step(cells,G): | |
for k in cells.keys(): | |
cell = cells[k] | |
neigh = G[k] | |
#nn = [conv(n,25) for n in neigh] | |
nn = neigh | |
#print(nn) | |
#if sum([cells[n].is_on if n in cells.keys() else 0 for n in nn])==1: | |
if sum([cells[n].is_on for n in nn])==1: | |
cell.qq.insert(0,True) | |
else: | |
cell.qq.insert(0,False) | |
cells[k] = cell | |
def next_step(cells,G): | |
for k in cells.keys(): | |
cell = cells[k] | |
cell.is_on = cell.qq.pop(0) | |
cells[k] = cell | |
def draw_cells(cells): | |
for k in cells.keys(): | |
cell = cells[k] | |
#print(cell.points) | |
pa,pb,pc = cell.p,cell.q,cell.r | |
#print(pa,pb,pc) | |
is_on = cell.is_on | |
draw_triangle((pa,pb,pc,is_on)) | |
from time import sleep | |
from turtle import Screen, Turtle | |
t.hideturtle() | |
t.speed(0) | |
t.tracer(0,0) | |
OFS = Point(*OFFSET) | |
#allcells,triangles = connect_neighbors() | |
#print(allcells) | |
#print(([(len(G[v])) for v in G.vertices()])) | |
#print((allcells.keys())) | |
allcells,G = create_cells(23) | |
#print(allcells) | |
print(([(len(G[v])) for v in G.vertices()])) | |
print((allcells.keys())) | |
while True: | |
draw_cells(allcells) | |
import time | |
time.sleep(0.1) | |
prepare_next_step(allcells,G) | |
next_step(allcells,G) | |
t.onclick(t.update()) #exitonclick() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment