Breathing Circles: Driver
#PythonProcessingTemplate_9_3_19
'''
File: breathingCircles_s4
Site:
Path:
Date: 9/16/19
Author:
Comments:
TODO:
'''
import random
from SpecialClasses import *
#wide-scoped variables go here
#adjust your canvas size if desired, here
canvas_width = 600
canvas_height = 600
#adjust for background refresh and grid here
shouldRefreshBackground = True
t = 0
deg = 0
def setup():
global shouldRefreshBackground
global white, my_red, my_blue, my_green, yellow, black, lightGray
global bgColr, myRate
global tinc
global cir1, cir2, cir3, cir4
size(canvas_width, canvas_height)
#color mode and basic colors
colorMode(HSB, 360, 100, 100, 100) #hue, saturation, brightness,
alpha
white = color(0, 0, 100)
#red, green and blue are reserved words in Processing
my_red = color(0, 100, 100)
my_blue = color(240, 80, 60)
my_green = color(120, 60, 40)
yellow = color(60, 100, 100)
black = color(0, 0, 0)
lightGray = color(0, 0, 95)
#sketch settings
myRate = 30 # default is 60 frames/second
bgColr = lightGray
frameRate(myRate)
background(bgColr)
#additional setup variables/functions here
msg = "===Breathing Circle!==="
print(msg)
tinc = .25
stroke(white)
#circle_1
h = random.randint(.10*width, .9*width)
k = random.randint(.10*height, .9*height)
position = Position(h, k)
initVelocityWave = Sinusoid("s", -1, 1, 120, 0)
v1x = initVelocityWave.getY(random.randint(0, 120))
v1y = initVelocityWave.getY(random.randint(0, 120))
velocity = Velocity(v1x,v1y)
diamX = Sinusoid("s", 50, 250, 30, 0)
diamY = Sinusoid("s", 50, 250, 30, 0)
hueCycle = Sinusoid("s", 30, 50, 120, 0)
opacityCycle = Sinusoid("s", 20, 80, 30, 0)
velXCycle1 = Sinusoid("s", -5, 5, 30, 0)
velYCycle1 = Sinusoid("c", -1, 1, 30, 0)
jitterCycleX = Sinusoid("s", -5, 5, 120, 0)
jitterCycleY = Sinusoid("c", -3, 8, 30, 0)
cir1 = BreathingCircle(diamX, diamY, hueCycle, opacityCycle,
velXCycle1, velYCycle1, position, velocity, 1,
jitterCycleX, jitterCycleY)
#circle_2
h = random.randint(.10*width, .9*width)
k = random.randint(.10*height, .9*height)
position2 = Position(h, k)
v2x = initVelocityWave.getY(random.randint(0, 120))
v2y = initVelocityWave.getY(random.randint(0, 120))
velocity2 = Velocity(v2x,v2y)
hueCycle2 = Sinusoid("s", 0, 40, 120, 0)
opacityCycle2 = Sinusoid("c", 20, 80, 60, 0)
velXCycle2 = Sinusoid("s", -2, 5, 30, 0)
velYCycle2 = Sinusoid("c", -3, 1, 360, 0)
diamX2 = Sinusoid("s", 10, 400, 180, 0)
diamY2 = Sinusoid("s", 10, 400, 180, 0)
jitterCycleX2 = Sinusoid("s", -.2, .2, 60, 0)
jitterCycleY2 = Sinusoid("c", -.2, .2, 30, 0)
cir2 = BreathingCircle(diamX2, diamY2, hueCycle2, opacityCycle2,
velXCycle2, velYCycle2, position2, velocity2, 40,
jitterCycleX2, jitterCycleY2)
#circle_3
h = random.randint(.10*width, .9*width)
k = random.randint(.10*height, .9*height)
position3 = Position(h, k)
v3x = initVelocityWave.getY(random.randint(0, 120))
v3y = initVelocityWave.getY(random.randint(0, 120))
velocity3 = Velocity(v3x,v3y)
hueCycle3 = Sinusoid("s", 200, 300, 120, 0)
opacityCycle3 = Sinusoid("c", 20, 80, 80, 0)
velXCycle3 = Sinusoid("s", -.5, .5, 30, 0)
velYCycle3 = Sinusoid("s", -.5, .5, 360, 0)
diamX3 = Sinusoid("c", 100, 250, 180, 0)
diamY3 = Sinusoid("c", 100, 250, 180, 0)
jitterCycleX3 = Sinusoid("s", -.4, .4, 60, 0)
jitterCycleY3 = Sinusoid("c", -.4, .4, 30, 0)
cir3 = BreathingCircle(diamX3, diamY3, hueCycle3, opacityCycle3,
velXCycle3, velYCycle3, position3, velocity3, 80,
jitterCycleX3, jitterCycleY3)
#circle_4
h = random.randint(.10*width, .9*width)
k = random.randint(.10*height, .9*height)
position4 = Position(h, k)
v4x = initVelocityWave.getY(random.randint(0, 120))
v4y = initVelocityWave.getY(random.randint(0, 120))
velocity4 = Velocity(v4x,v4y)
hueCycle4 = Sinusoid("s", 85, 143, 360, 0)
opacityCycle4 = Sinusoid("c", 20, 80, 80, 0)
velXCycle4 = Sinusoid("s", -1, 1, 30, 0)
velYCycle4 = Sinusoid("c", -1, 1, 360, 0)
diamX4 = Sinusoid("s", 150, 350, 20, 0)
diamY4 = Sinusoid("c", 150, 350, 20, 0)
jitterCycleX4 = Sinusoid("s", -1, 1, 60, 0)
jitterCycleY4 = Sinusoid("c", -1, 1, 30, 0)
cir4 = BreathingCircle(diamX4, diamY4, hueCycle4, opacityCycle4,
velXCycle4, velYCycle4, position4, velocity4, 80,
jitterCycleX4, jitterCycleY4)
#end function setup
def draw():
global t, deg
#adjust background color if desired
if shouldRefreshBackground:
background(lightGray);
t += tinc
cir1.update(t, deg)
cir1.display(t)
cir1.move(tinc, t)
cir2.update(t, deg)
cir2.display(t)
cir2.move(tinc, t)
cir3.update(t, deg)
cir3.display(t)
cir3.move(tinc, t)
cir4.update(t, deg)
cir4.display(t)
cir4.move(tinc, t)
#end function draw
Last update: 09/20/19
Breathing Circles: Special Classes
class Position:
def __init__(self, xc, yc):
self.xc = xc
self.yc = yc
#end __init__ Position
def update(self, myCirc, tinc):
self.xc += myCirc.velocity.vx*tinc
self.yc += myCirc.velocity.vy*tinc
#end function update
def boundaryCheck(self, myCirc, t):
#horizontal boundaries
if myCirc.posn.xc + myCirc.xDiamCycle.getY(t)/2.0 > .99*width:
if myCirc.velocity.vx > 0:
myCirc.velocity.vx *= -1
if myCirc.posn.xc - myCirc.xDiamCycle.getY(t)/2.0< .01*width:
if myCirc.velocity.vx <= 0:
myCirc.velocity.vx *= -1
#vertical boundaries
if myCirc.posn.yc + myCirc.yDiamCycle.getY(t)/2.0 > .99*height:
if myCirc.velocity.vy > 0:
myCirc.velocity.vy *= -1
if myCirc.posn.yc - myCirc.yDiamCycle.getY(t)/2.0< .01*height:
if myCirc.velocity.vy <= 0:
myCirc.velocity.vy *= -1
#end function boundaryCheck2
def addJitter(self, jitterCycleX, jitterCycleY, t):
self.xc += jitterCycleX.getY(t)
self.yc += jitterCycleY.getY(t)
#end function addJitter
#end class Position
class Velocity:
def __init__(self, vx, vy):
self.vx = vx
self.vy = vy
#end __init__ velocity
def update(self, vxCycle, vyCycle, t):
self.vx += vxCycle.getY(t)
self.vy += vyCycle.getY(t)
#end function update
#end class Velocity
class Sinusoid:
def __init__(self, type, minVal, maxVal, period, hShift):
self.type = type;
self.minVal = minVal
self.maxVal = maxVal
self.period = period
self.hShift = hShift
self.axis = (minVal + maxVal)/2.0
self.amplitude = maxVal - self.axis
self.b = 2*PI/period
#end __init__ Sinusoid
def getY(self, x):
if type == "s":
return self.amplitude*sin(self.b*(x - self.hShift)) +
self.axis
else:
return self.amplitude*cos(self.b*(x - self.hShift)) +
self.axis
#end function getY
#end class Sinusoid
class BreathingCircle:
def __init__(self, xDiamCycle, yDiamCycle, hueCycle, opacityCycle,
velXCycle, velYCycle, posn, velocity,
deltaDeg, jxCycle, jyCycle):
self.xDiamCycle = xDiamCycle
self.yDiamCycle = yDiamCycle
self.hueCycle = hueCycle
self.opacityCycle = opacityCycle
self.velXCycle = velXCycle
self.velYCycle = velYCycle
self.velocity = velocity
self.deltaDeg = deltaDeg
self.posn = posn
self.jxCycle = jxCycle
self.jyCycle = jyCycle
#end __init__ BreathingCircle
def update(self, t, deg):
pushMatrix()
translate(self.posn.xc, self.posn.yc)
deg += self.deltaDeg
rotate(radians(deg))
self.velocity.update(self.velXCycle, self.velYCycle, t)
#end function update
def display(self, t):
#update color
myColr = color(round(self.hueCycle.getY(t)), 100, 100)
myAlpha = self.opacityCycle.getY(t)
fill(myColr, myAlpha)
ellipse(0, 0, self.xDiamCycle.getY(t), self.yDiamCycle.getY(t))
popMatrix()
#end function display
def move(self, tinc, t):
self.posn.update(self, tinc)
self.posn.addJitter(self.jxCycle, self.jyCycle, t)
self.posn.boundaryCheck(self, t)
#end class BreathingCircle
Last update: 09/20/19