Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sinewave pattern efficiency #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions patterns/sinewave.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
import cubehelper
import random
import math

def rainbow(iter,step):
def compo(pattArray,angle):
for num in pattArray["high"]:
if (angle >= num[0]) and (angle <= num[1]):
comp = 1.0

for num in pattArray["low"]:
if (angle >= num[0]) and (angle <= num[1]):
comp = 0.0
def compo(pattArray,angle):
for num in pattArray["high"]:
if (angle >= num[0]) and (angle <= num[1]):
comp = 1.0

for num in pattArray["up"]:
if (angle >= num[0]) and (angle <= num[1]):
comp = (angle - num[0])/(num[1]-num[0]+0.0)
for num in pattArray["low"]:
if (angle >= num[0]) and (angle <= num[1]):
comp = 0.0

for num in pattArray["down"]:
if (angle >= num[0]) and (angle <= num[1]):
comp = (num[1] - angle)/(num[1]-num[0]+0.0)
for num in pattArray["up"]:
if (angle >= num[0]) and (angle <= num[1]):
comp = (angle - num[0])/(num[1]-num[0]+0.0)

return comp
for num in pattArray["down"]:
if (angle >= num[0]) and (angle <= num[1]):
comp = (num[1] - angle)/(num[1]-num[0]+0.0)

angle = (iter*step)%360
return comp

b = {
b = {
"high": [[180, 299]],
"low": [[0, 119]],
"up": [[120, 179]],
"down": [[300, 359]]
}
}

r = {
r = {
"high": [[300, 359], [0, 59]],
"low": [[120, 239]],
"up": [[240, 299]],
"down": [[60, 119]]
}
}

g = {
g = {
"high": [[60, 179]],
"low": [[240, 359]],
"up": [[0, 59]],
"down": [[180, 239]]
}
}

def rainbow(iter,step):
angle = (iter*step)%360
return (compo(r,angle), compo(g,angle), compo(b,angle))

class Pattern(object):
Expand All @@ -54,18 +52,18 @@ def init(self):
self.sinwidth = 0.3
self.color = cubehelper.random_color()
self.prevColor = cubehelper.random_color()
self.double_buffer = True

return 1.0/15

def tick(self):
self.cube.clear()
color = cubehelper.mix_color(self.prevColor, self.color, (math.sin(self.sinwidth*self.iter) + 1 )/2)
ran = random.randrange(100)

for x in range(0, self.cube.size):
color = rainbow(self.iter+x, 10)
for y in range(0, self.cube.size):
z = math.floor(math.sin(self.sinwidth*(self.iter+(x+y)))*3.2+4)
self.cube.set_pixel((x, y, int(z)), rainbow(self.iter+x, 10))
self.cube.set_pixel((x, y, int(z)), color)

self.iter += self.delta
if (math.sin(self.sinwidth*self.iter)+1)/2 < 0.1:
Expand Down