Skip to content

Commit ef19c94

Browse files
author
monkstone
committed
add some examples that I created for processing.py by feinberg
1 parent ecde569 commit ef19c94

File tree

107 files changed

+1469
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1469
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
Adapted from
3+
4+
/**
5+
* Synthesis 3: Motion and Arrays
6+
* Centipede by Ariel Malka (www.chronotext.org)
7+
* p. 372
8+
*/
9+
"""
10+
11+
12+
node_length = 30
13+
node_size = node_length-1
14+
n_nodes = 70
15+
nodes = []
16+
delay = 20.0
17+
col_head = color(255, 0, 0)
18+
col_body = color(0)
19+
20+
def setup():
21+
size(600, 600)
22+
smooth()
23+
noStroke()
24+
25+
global x,y
26+
x = width/2
27+
y = height/2
28+
29+
r1 = 10
30+
r2 = 100
31+
dr = r2-r1
32+
D = 0.0
33+
34+
for i in range(n_nodes):
35+
r = sqrt(r1 * r1 + 2.0 * dr * D)
36+
d = (r - r1) / dr
37+
38+
nodes.append((x - sin(d) * r, y + cos(d) * r))
39+
40+
D += node_length
41+
42+
43+
def draw():
44+
background(204)
45+
46+
# Set the position of the head
47+
setTarget(mouseX, mouseY)
48+
49+
# Draw the head
50+
fill(col_head)
51+
ellipse(nodes[0][0], nodes[0][1], node_size, node_size)
52+
53+
# Draw the body
54+
fill(col_body)
55+
for x,y in nodes[1:]:
56+
ellipse(x,y, node_size, node_size)
57+
58+
59+
def setTarget(tx, ty):
60+
# Motion interpolation for the head
61+
global x,y
62+
x += (tx - x) / delay
63+
y += (ty - y) / delay
64+
nodes[0] = (x,y)
65+
66+
# Constrained motion for the other nodes
67+
for i in range(1,n_nodes):
68+
dx = nodes[i - 1][0] - nodes[i][0]
69+
dy = nodes[i - 1][1] - nodes[i][1]
70+
length = sqrt(sq(dx) + sq(dy))
71+
nodes[i] = (nodes[i - 1][0] - (dx/length * node_length),
72+
nodes[i - 1][1] - (dy/length * node_length))
73+
74+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Draw a cylinder centered on the y-axis, going down from y=0 to y=height.
2+
# The radius at the top can be different from the radius at the bottom,
3+
# and the number of sides drawn is variable.
4+
5+
6+
7+
def setup() :
8+
size(400, 400, P3D)
9+
global my_shape
10+
my_shape = drawCylinder(10, 180, 200, 16) # Draw a mix between a cylinder and a cone
11+
#my_shape = drawCylinder(70, 70, 120, 16) # Draw a cylinder
12+
#my_shape = drawCylinder(0, 180, 200, 4) # Draw a pyramid
13+
14+
15+
def draw():
16+
background(0)
17+
lights()
18+
translate(width / 2, height / 2)
19+
rotateY(mouseX * PI / width)
20+
rotateZ(mouseY * -PI / height)
21+
22+
fill(255, 255, 255)
23+
translate(0, -40, 0)
24+
shape(my_shape)
25+
26+
def drawCylinder(topRadius, bottomRadius, tall, sides):
27+
noStroke()
28+
cone = createShape(PShape.GROUP)
29+
angle = 0
30+
angleIncrement = TWO_PI / sides
31+
cylinder = createShape(PShape.QUAD_STRIP)
32+
for i in range(sides+1):
33+
#normal(cos(angle),sin(angle),0)
34+
cylinder.vertex(topRadius*cos(angle), 0, topRadius*sin(angle))
35+
cylinder.vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle))
36+
angle += angleIncrement
37+
cylinder.end()
38+
cone.addChild(cylinder)
39+
# If it is not a cone, draw the circular top cap
40+
if (topRadius != 0):
41+
angle = 0
42+
top = createShape(PShape.TRIANGLE_FAN)
43+
44+
# Center point
45+
top.vertex(0, 0, 0)
46+
for i in range(sides+1):
47+
top.vertex(topRadius * cos(angle), 0, topRadius * sin(angle))
48+
angle += angleIncrement
49+
top.end()
50+
cone.addChild(top)
51+
# If it is not a cone, draw the circular bottom cap
52+
if (bottomRadius != 0):
53+
angle = 0
54+
bottom = createShape(PShape.TRIANGLE_FAN)
55+
56+
# Center point
57+
bottom.vertex(0, tall, 0)
58+
for i in range(sides+1):
59+
bottom.vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle))
60+
angle += angleIncrement
61+
62+
bottom.end()
63+
return cone
64+
65+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
size(100, 100)
2+
background(0)
3+
noStroke()
4+
smooth()
5+
fill(242, 204, 47, 160)
6+
ellipse(47, 36, 64, 64)
7+
fill(174, 221, 60, 160)
8+
ellipse(90, 47, 64, 64)
9+
fill(116, 193, 206, 160)
10+
ellipse(57, 79, 64, 64)
11+
12+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
# Change the saturation and brightness, hue constant
3+
size(100, 100)
4+
colorMode(HSB)
5+
for i in range(100):
6+
for j in range(100):
7+
stroke(132, j*2.5, i*2.5)
8+
point(i, j)
9+
10+

examples/handbook/lines/lines.pde

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
size(100, 100)
2+
background(56, 90, 94)
3+
smooth(4)
4+
x = 0
5+
strokeWeight(12)
6+
for i in range(51,256,51):
7+
stroke(242, 204, 47, i)
8+
line(x, 20, x+20, 80)
9+
x += 20
10+
11+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Draw a sphere on top of a box and moves the coordinates with the mouse
2+
# Press a mouse button to turn on the lights
3+
def setup():
4+
size(400, 400, P3D)
5+
6+
def draw():
7+
background(0);
8+
if mousePressed == True:
9+
# If the mouse is pressed,
10+
lights(); # turn on lights
11+
noStroke()
12+
pushMatrix()
13+
translate(mouseX, mouseY, -500)
14+
rotateY(PI / 6) # Rotate around y-axis
15+
box(400, 100, 400) # Draw box
16+
pushMatrix()
17+
popMatrix()
18+
translate(0, -200, 0) # Position the sphere
19+
sphere(150) # Draw sphere on top of box
20+
popMatrix()
21+
22+

examples/handbook/waves/waves.pde

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Adapted from
3+
/**
4+
* Synthesis 1: Form and Code
5+
* Riley Waves by Casey Reas (www.processing.org)
6+
* p. 151
7+
*
8+
* Step 3, values are modified to create a new pattern.
9+
*/
10+
"""
11+
12+
size(1200, 280)
13+
background(255)
14+
smooth()
15+
noStroke()
16+
#fill(0)
17+
angle = PI
18+
angle2 = PI
19+
magnitude = 3
20+
21+
for i in range(-magnitude,height+magnitude,12):
22+
angle2 = angle
23+
fill(0)
24+
beginShape(TRIANGLE_STRIP)
25+
for x in range(0,width+1,8):
26+
y = i + (sin(angle)* magnitude)
27+
angle += PI/24.0
28+
y2 = i+4 + (sin(angle+PI/12)* magnitude)
29+
vertex(x, y)
30+
vertex(x, y2)
31+
endShape()
32+
33+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def setup():
2+
size(200,200, P3D)
3+
noLoop()
4+
5+
def draw():
6+
# clear the whole screen
7+
background(200)
8+
lights()
9+
noStroke()
10+
background(200)
11+
pushMatrix()
12+
fill(255,255,0)
13+
translate(130,130)
14+
rotate(PI/6,1,1,0)
15+
box(50)
16+
popMatrix()
17+
fill(255,0,255)
18+
translate(60, 50)
19+
sphere(50)
20+
21+

examples/misc/circles/circles.pde

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def setup():
2+
size(300,300, P3D)
3+
hint(DISABLE_DEPTH_TEST)
4+
smooth()
5+
global l
6+
l = []
7+
8+
def mousePressed():
9+
l.append ([mouseX, mouseY, 1])
10+
11+
def mouseDragged():
12+
l.append ([mouseX, mouseY,1])
13+
14+
def draw():
15+
fill(0,10)
16+
noStroke()
17+
rect(0,0,width,height)
18+
stroke(255)
19+
nl = l[:]
20+
l[:]=[]
21+
for x,y,w in nl:
22+
gs = 255-w
23+
if gs<0: continue
24+
stroke(gs)
25+
ellipse(x,y,w,w)
26+
l.append([x,y,w+2])
27+
28+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
size(200,200)
2+
rectMode(CENTER)
3+
rect(100,100,20,100)
4+
ellipse(100,70,60,60)
5+
ellipse(81,70,16,32)
6+
ellipse(119,70,16,32)
7+
line(90,150,80,160)
8+
line(110,150,120,160)
9+

examples/misc/falloff/falloff.pde

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
def setup():
3+
size(200, 200, P3D)
4+
noStroke()
5+
6+
def draw():
7+
background(200)
8+
if mousePressed: lightFalloff(1,0,1e-4)
9+
pointLight(255,255,255,100,100,50)
10+
nx = 6
11+
ny = 6
12+
for i in range(nx):
13+
for j in range(ny):
14+
pushMatrix()
15+
translate((i+0.5)*width/nx,(j+0.5)*height/ny)
16+
sphere(10)
17+
popMatrix()
18+
19+

examples/misc/fonttest/fonttest.pde

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
size(200,200)
4+
font = createFont("Times New Roman", 20)
5+
textFont(font)
6+
textAlign(CENTER,CENTER)
7+
text("The quick brown\nfox jumps over\nthe lazy dogs", width/2, height/2)
8+
9+

examples/misc/fpstest/fpstest.pde

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from random import randint
2+
3+
# Choose the total number of bouncing balls
4+
nballs = 150
5+
balls = [()]*nballs
6+
7+
def setup():
8+
size(400,400)
9+
noStroke()
10+
ellipseMode(CENTER)
11+
# generate n balls with random positions, sizes, speeds and colors
12+
for i in range(nballs):
13+
# position
14+
x, y = randint(20, 380), randint(20, 380)
15+
# speed
16+
dx, dy = randint(1, 4), randint(-4, -1)
17+
# radius
18+
r = randint(10, 20)
19+
# color
20+
c = color(randint(10, 255),randint(10, 255),randint(10, 255))
21+
balls[i] = (x,y,dx,dy,r,c)
22+
23+
24+
def draw():
25+
# fade the last frame by drawing background in transparent color
26+
fill(255,50)
27+
rect(0,0,width,height)
28+
# draw/bounce balls
29+
for i, ball in enumerate(balls):
30+
x,y,dx,dy,r,c = ball
31+
fill(c)
32+
x += dx
33+
y += dy
34+
diam = r/2
35+
if constrain(x, diam, 400 - diam) != x:
36+
dx *= -1
37+
if constrain(y, diam, 400 - diam) != y:
38+
dy *= -1
39+
balls[i] = x,y,dx,dy,r,c
40+
ellipse(x,y,r,r)
41+
fill(0)
42+
text("fps: %3d"%frameRate,0,20)
43+
44+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
size(300,300)
2+
textFont(createFont("Times", 36))
3+
textAlign(CENTER)
4+
text("Hello world", width/2, height/2)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def setup():
2+
size(300,300, P3D)
3+
textFont(createFont("Times New Roman", 36))
4+
textAlign(CENTER,CENTER)
5+
global ang
6+
ang = 0
7+
8+
def draw():
9+
global ang
10+
background(0,0,0)
11+
fill (255)
12+
translate (width/2, height/2)
13+
rotateZ (ang)
14+
ang += 0.01
15+
text("Hello world", 0, 0)
16+
17+

0 commit comments

Comments
 (0)