-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractal.py
180 lines (159 loc) · 5.44 KB
/
fractal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import pygame
from pygame.locals import *
import pygame.freetype
from math import *
import numpy as np
import time
import socket
import sys
import json
import threading
BG_COLOR = [0,0,0]
ip = "0.0.0.0"
port = 5005
dim = (2*770,770)
med = 0
at = 0
sig = 0
go = True
start = False
Tserver = 0
Tmonitor = 0
Tmain = 0
def monitor():
global go
print('monitor')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print('Close')
go = False
Tserver.join()
Tmain.join()
pygame.quit()
exit()
time.sleep(0.01)
def startServer():
global at, sig, med, go
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(2)
# Bind the socket to the port
server_address = (ip, port)
s.bind(server_address)
while go:
try:
data, address = s.recvfrom(4096)
start = True
neural = json.loads(data.decode('utf-8'))
med = neural['meditation']
at = neural['attention']
sig = neural['quality']
print(neural)
except socket.timeout as e:
start = False
print(e)
def tree(dim, iterations):
s = ['X']
deg = 22.5
size = 1.2
pos = dim[0]/2.,dim[1]
for i in range(iterations):
X_make = ['F', '-', '[', '[', 'X', ']', '+', 'X', ']', '+', 'F', '[', '+', 'F', 'X', ']', '-', 'X']
F_make = ['F','F']
j = 0
while j<len(s):
if s[j] == 'X':
s.pop(j)
for item in X_make[::-1]: s.insert(j,item)
j+=len(X_make)-1
elif s[j] == 'F':
s.pop(j)
for item in F_make[::-1]: s.insert(j,item)
j+=len(F_make)-1
j+=1
return s, deg, size, pos
def tree2(dim, iterations):
s = ['F']
pos = dim[0]/2.,dim[1]
for i in range(iterations):
#X -> F-[[X]+X]+F[+FX]-X
#F- > FF
deg = 22.5
size = 3.3
X_make = ['F', '-', '[', '[', 'X', ']', '+', 'X', ']', '+', 'F', '[', '+', 'F', 'X', ']', '-', 'X']
F_make = ['F','F','+','[','+','F','-','F','-','F',']','-','[','-','F','+','F','+','F',']']
j = 0
while j<len(s):
if s[j] == 'X':
s.pop(j)#replace X with our X_make list
for item in X_make[::-1]: s.insert(j,item)
j+=len(X_make)-1#bump j up to skip over added items
elif s[j] == 'F':
s.pop(j)
for item in F_make[::-1]: s.insert(j,item)
j+=len(F_make)-1
j+=1
return s, deg, size, pos
def main():
global go
infoObject = pygame.display.Info()
screen = pygame.display.set_mode((infoObject.current_w, infoObject.current_h))
s,deg,size,pos_init = tree2((infoObject.current_w, infoObject.current_h), 5)
print("main")
while(go):
if(sig < 100):
font = pygame.freetype.Font('./OpenSans-Regular.ttf', 30)
pygame.draw.rect(screen, (0,0,0), pygame.Rect(10, 10, 250, 250))
font.render_to(screen, (10, 10), 'Low Signal: ' + str(sig) +'%', (255, 0, 0))
pygame.display.update()
time.sleep(1)
elif(start and sig >=100):
screen.fill(BG_COLOR)
pos = pos_init
angle = -120
saved_angles = []
saved_poses = []
tam = len(s)
font = pygame.freetype.Font('./OpenSans-Regular.ttf', 15)
i = 0
for letter in s:
if (not go):
break
if (letter == 'F'):
unit = 2
dx = cos(radians(angle))*unit*size
dy = sin(radians(angle))*unit*size
f = (at/100.)
da = deg + np.random.uniform(-deg*f,deg*f)
if(sig < 100):
color = pygame.Color(0, 128, 128, 0)
else:
color = pygame.Color(int(180.*(1.-(med/100.)))+40, 0, int(180. * (med/100.) )+40, 0)
pygame.draw.line(screen, color, pos, (pos[0]+dx,pos[1]+dy), 1+int(2*(1.0-i/tam)))
pygame.draw.rect(screen, (0,0,0), pygame.Rect(10, 10, 150, 80))
font.render_to(screen, (10, 10), 'Signal: ' + str(sig) +'%', (255, 0, 0))
font.render_to(screen, (10, 30), 'Attention: ' + str(at) + '%', (255, 0, 0))
font.render_to(screen, (10, 50), 'Meditation: ' + str(med) + '%', (255, 0, 0))
pygame.display.update()
time.sleep(0.01)
pos = (pos[0]+dx, pos[1]+dy)
elif letter == '+':
angle += da
elif letter == '-':
angle -= da
elif letter == '[':
saved_poses.append(pos)
saved_angles.append(angle)
elif letter == ']':
#pygame.draw.circle(screen,(50,205,50),(int(pos[0]),int(pos[1])),2)
pos = saved_poses.pop()
angle = saved_angles.pop()
i += 1
if __name__ == '__main__':
pygame.init()
Tserver = threading.Thread(target=startServer)
Tmonitor = threading.Thread(target=monitor)
Tmain = threading.Thread(target=main)
Tserver.start()
Tmonitor.start()
Tmain.start()