/
Metaball.py
52 lines (41 loc) · 1.33 KB
/
Metaball.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
"""
Metaball Demo Effect
by luis2048. (Adapted to Python by Jonathan Feinberg)
Organic-looking n-dimensional objects. The technique for rendering
metaballs was invented by Jim Blinn in the early 1980s. Each metaball
is defined as a function in n-dimensions.
"""
numBlobs = 3
# Position vector for each blob
blogPx = [0, 90, 90]
blogPy = [0, 120, 45]
# Movement vector for each blob
blogDx = [1, 1, 1]
blogDy = [1, 1, 1]
pg = createGraphics(160, 90, P2D)
def setup():
size(640, 360, OPENGL)
def draw():
vx, vy = [], []
for i in range(numBlobs):
blogPx[i] += blogDx[i]
blogPy[i] += blogDy[i]
# bounce across screen
if blogPx[i] < 0: blogDx[i] = 1
if blogPx[i] > pg.width: blogDx[i] = -1
if blogPy[i] < 0: blogDy[i] = 1
if blogPy[i] > pg.height: blogDy[i] = -1
vx.append(tuple(sq(blogPx[i] - x) for x in xrange(pg.width)))
vy.append(tuple(sq(blogPy[i] - y) for y in xrange(pg.height)))
# Output into a buffered image for reuse
pg.beginDraw()
for y in range(pg.height):
for x in range(pg.width):
m = 1
for i in range(numBlobs):
# Increase this number to make your blobs bigger
m += 60000 / (vy[i][y] + vx[i][x] + 1)
pg.set(x, y, color(0, m + x, (x + m + y) / 2))
pg.endDraw()
# Display the results
image(pg, 0, 0, width, height)