-
Notifications
You must be signed in to change notification settings - Fork 0
/
sine1.py
executable file
·209 lines (164 loc) · 5.62 KB
/
sine1.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
from time import sleep
import ui
import curses
import drawille
import locale
import math
import sys
locale.setlocale(locale.LC_ALL,"")
stdscr = curses.initscr()
stdscr.refresh()
size = drawille.getTerminalSize()
width = (size[0]-1) * ui.HORIZONTAL_PIXELS_PER_CHAR
height = (size[1]-1) * ui.VERTICAL_PIXELS_PER_CHAR
amplitude = ui.Control("amp", 1, 0, 2)
frequency = ui.Control("freq", 1.0, 0.1, 8)
phase = ui.Control("phase", 0.0, -1, 1)
resolution = ui.Control("res", 5, 1, 10)
speed = ui.Control("speed", 1, -2, 2)
time = 0
controls = [
amplitude,
frequency,
phase,
#resolution,
speed
]
class Plot:
def __init__(self, plot_centerx, plot_centery, plot_width, plot_height):
self.centerx = plot_centerx
self.centery = plot_centery
self.width = plot_width
self.height = plot_height
self.update_alignment()
#
# Recalculate alignment after moving/resizing plot.
#
def update_alignment(self):
self.left = self.centerx - self.width/2
self.top = self.centery - self.height/2
self.right = self.centerx + self.width/2
self.bottom = self.centery + self.height/2
def draw_centered_axes(self, c):
ui.draw_line(c, self.left, self.centery, self.right, self.centery)
ui.draw_line(c, self.centerx, self.top, self.centerx, self.bottom)
def draw_left_aligned_axes(self, c):
ui.draw_line(c, self.left, self.centery, self.right, self.centery)
ui.draw_line(c, self.left, self.top, self.left, self.bottom)
class SinePlot(Plot):
def render(self, c):
self.draw_left_aligned_axes(c)
count = resolution.value * 20
for n in range(int(count)):
i = n / count
x = self.left + i * self.width
y = self.centery - self.height/4. * amplitude.value * math.sin(2 * math.pi * (frequency.value * i + phase.value))
if n != 0 and not ((x < 0 or x > width) and (y < 0 or y > width) and (prevx < 0 or prevx > height) and (prevy < 0 or prevy > height)):
ui.draw_line(c, prevx, prevy, x, y)
prevx = x
prevy = y
if mode != 0:
x = self.left + time * self.width
y = int(self.centery - self.height/4. * amplitude.value * math.sin(2 * math.pi * (frequency.value * time + phase.value)))
ui.draw_ellipse(c, x, y, 5, 5, 16)
class PhaseSpace1DPlot(Plot):
def render(self, c):
self.draw_centered_axes(c)
x = self.centerx
y = int(self.centery - self.height/4. * amplitude.value * math.sin(2 * math.pi * (frequency.value * time + phase.value)))
ui.draw_ellipse(c, x, y, 5, 5, 16)
class PhaseSpace2DPlot(Plot):
def render(self, c):
self.draw_centered_axes(c)
ui.draw_ellipse(c, self.centerx, self.centery, self.width/4. * amplitude.value, self.height/4. * amplitude.value, 100)
x = int(self.centerx + self.width/4. * amplitude.value * math.cos(2 * math.pi * (frequency.value * time + phase.value)))
y = int(self.centery - self.height/4. * amplitude.value * math.sin(2 * math.pi * (frequency.value * time + phase.value)))
ui.draw_ellipse(c, x, y, 5, 5, 16)
ui_width = width / 5
ui_height = ui.VERTICAL_PIXELS_PER_CHAR * len(controls) + 10
ui_x = width - 4 - ui_width
ui_y = 4
ui_inst = ui.UI(ui_x, ui_y, ui_width, ui_height, controls)
sine_plot = SinePlot(width/2, height/2, width*2/3, height*2/3)
phaseSpace1DPlot = PhaseSpace1DPlot(width*1/3, height*3/4, height*1/2, height*1/2)
phaseSpace2DPlot = PhaseSpace2DPlot(width*2/3, height*3/4, height*1/2, height*1/2)
def render(c):
sine_plot .render(c)
if mode >= 2:
phaseSpace1DPlot.render(c)
if mode >= 3:
phaseSpace2DPlot.render(c)
ui_inst.render(c)
def update(c, deltaTime):
global time
time += deltaTime
if time > 1:
time = 0
elif time < 0:
time = 1
mode = 0
max_mode = 4
def on_set_mode():
global speed
if mode == 0:
speed.visible = False
else:
speed.visible = True
if mode < 2:
sine_plot.centerx = width/2
sine_plot.centery = height/2
sine_plot.width = width*2/3
sine_plot.height = height*2/3
else:
sine_plot.centerx = width/2
sine_plot.centery = height/4
#sine_plot.width = width*1/3
sine_plot.height = height*2/5
sine_plot.update_alignment()
on_set_mode()
def next_mode():
global mode
mode += 1
if mode >= max_mode:
mode = max_mode - 1
on_set_mode()
def prev_mode():
global mode
mode -= 1
if mode < 0:
mode = 0
on_set_mode()
def __main__(stdscr):
try:
ui.init_input()
c = drawille.Canvas()
while 1:
render(c)
f = c.frame(0, 0, width, height)
stdscr.addstr(0, 0, '{0}\n'.format(f))
stdscr.refresh()
if ui.poll_input():
key = sys.stdin.read(1)
if key == 'q':
break
elif key == 'k':
ui_inst.prev_control()
elif key == 'j':
ui_inst.next_control()
elif key == 'h':
ui_inst.decrease_control()
elif key == 'l':
ui_inst.increase_control()
elif key == 'n':
next_mode()
elif key == 'p':
prev_mode()
sleep(1.0/20)
update(c, 1.0/20 * speed.value) # FIXME: use clock.
c.clear()
finally:
ui.cleanup_input()
if __name__ == '__main__':
from sys import argv
curses.wrapper(__main__)