Skip to content

Commit 797918d

Browse files
committed
New sketch Starz
1 parent d99aebb commit 797918d

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

Starz_HYPE/Starz_HYPE.pyde

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# add_library('hype')
2+
'''
3+
1. One hundred `HRect`
4+
2. Of randomly-selected size
5+
3. Each having semi-tranparent fill and stroke
6+
4. Each colored according to an underlying `HColorField`
7+
5. Each rotating around its own center with a randomly-selected speed and
8+
direction
9+
6. Randomly distributed around the circumference of
10+
7. One of several concentric circles
11+
8. All squares rotating at a randomly-selected speed and direction around
12+
a common center point
13+
14+
Implementation by Ben Alkov 7-12 August 2014
15+
16+
'''
17+
from hype.core.util import H
18+
from hype.extended.colorist import HColorField
19+
from hype.extended.drawable import HGroup
20+
from hype.extended.drawable import HRect
21+
22+
import util
23+
24+
from parentcallback import ParentCallback
25+
from tricallback import TriCallback
26+
27+
28+
def setup():
29+
size(512, 512)
30+
util.centerX = width / 2
31+
util.centerY = height / 2
32+
frameRate(30)
33+
H.init(this).background(0) # #000000
34+
smooth()
35+
# No `requestAll()` for the visible HRects. They will be requested inside
36+
# the parent's callback.
37+
util.visiblePool.add(HRect())\
38+
.onCreate(TriCallback())
39+
util.parentPool.autoAddToStage()\
40+
.add(HGroup())\
41+
.onCreate(ParentCallback())\
42+
.requestAll()
43+
44+
45+
def draw():
46+
H.drawStage()
47+
# saveFrame('C:/Users/IBM_ADMIN/Documents/frames/####.tif')
48+
# if frameCount == 900:
49+
# exit()

Starz_HYPE/parentcallback.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
`draw()` loop callback for the invisible parent `HGroup`s.
3+
'''
4+
from hype.core.util import H
5+
from hype.core.interfaces import HCallback
6+
7+
import util
8+
9+
10+
class ParentCallback(HCallback):
11+
@staticmethod
12+
def run(parent):
13+
# Pull one HRect from the pool of visible HRects.
14+
child = util.visiblePool.request()
15+
parent.noStroke()\
16+
.noFill()\
17+
.size(3)\
18+
.rotation(random(360))
19+
# In order for the object to rotate *on* the orbit, around the
20+
# *orbit's* center, we have to set its center to the center of the
21+
# orbit, and then calculate its position on the orbit based on the
22+
# child's position.
23+
parent.loc(util.centerX, util.centerY)\
24+
.anchor(abs(child.x() - util.centerX),
25+
abs(child.y() - util.centerY))
26+
parent.add(child)
27+
# Move the child to the parent's center.
28+
child.locAt(H.CENTER)
29+
util.applyRotation(parent, 0.5, 0.01)

Starz_HYPE/tricallback.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
`draw()` loop callback for the visible tris.
3+
'''
4+
from hype.core.util import H
5+
from hype.core.interfaces import HCallback
6+
7+
import util
8+
9+
10+
class TriCallback(HCallback):
11+
@staticmethod
12+
def run(drawable):
13+
# Position this HTri around its parent's center, at an angle based on
14+
# its index.
15+
# fill(random(288, 360), random(100, 255), random(51, 153), random(5, 15))
16+
drawable.noStroke()\
17+
.fill(0xff000000, 100)\
18+
.rotation(TAU / self.index)\
19+
.loc(0, 0)\
20+
.anchorAt(H.CENTER_BOTTOM)

Starz_HYPE/util.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
Globals and helper methods.
3+
'''
4+
from hype.extended.behavior import HRotate
5+
from hype.extended.util import HDrawablePool
6+
7+
from random import randint
8+
9+
10+
colorfield = None
11+
centerX = None
12+
centerY = None
13+
visiblePool = HDrawablePool(100)
14+
parentPool = HDrawablePool(100)
15+
16+
17+
def positionOnOrbit():
18+
'''
19+
Generate a random position on the circumference of the orbit chosen for
20+
this item.
21+
'''
22+
angle = random(TAU)
23+
# `randint` slightly offsets the position so we don't end up with the
24+
# visible HRects orbiting on *exact* circles.
25+
radius = chooseOrbit() + randint(0, 22)
26+
createX = centerX + (cos(angle) * radius) # `angle` *must* be radians.
27+
createY = centerY + (sin(angle) * radius) #
28+
return createX, createY
29+
30+
31+
def chooseOrbit():
32+
'''
33+
Randomly choose an orbit, based on a set of weights.
34+
'''
35+
chance = random(1)
36+
if chance < 0.18:
37+
return 64
38+
elif chance < 0.50:
39+
return 128
40+
elif chance < 0.78:
41+
return 208
42+
elif chance < 1.0:
43+
return 288
44+
45+
46+
def applyRotation(obj, speed, tolerance):
47+
'''
48+
Attach an HRotate to the given object, with speed `speed`.
49+
'''
50+
HRotate(obj, avoidZero(speed, tolerance))
51+
52+
53+
# This is specifically used to avoid zero or synchronous rotation. We want all
54+
# visible HRects to *appear* to rotate in place.
55+
def avoidZero(limit, tolerance):
56+
'''
57+
Return a random value in the range from `-limit` to `limit - 1`, excluding
58+
the inner range from `-tolerance` to `tolerance - 1` (and, logically, zero
59+
as well).
60+
'''
61+
value = random(-limit, limit)
62+
while -tolerance < value < tolerance:
63+
value = random(-limit, limit)
64+
continue
65+
return value

0 commit comments

Comments
 (0)