Skip to content

Commit aaa0215

Browse files
authored
Port processing4 examples to Python. (#260)
1 parent 0b1a55d commit aaa0215

439 files changed

Lines changed: 56758 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Array 2D.
2+
#
3+
# Demonstrates the syntax for creating a two-dimensional (2D) array.
4+
# Values in a 2D array are accessed through two index values.
5+
# 2D arrays are useful for storing images. In this example, each dot
6+
# is colored in relation to its distance from the center of the image.
7+
from mewnala import *
8+
9+
distances = []
10+
max_distance = 0.0
11+
spacer = 0
12+
13+
14+
def setup():
15+
global max_distance, spacer
16+
size(640, 360)
17+
max_distance = dist(width / 2, height / 2, width, height)
18+
for x in range(width):
19+
distances.append([0.0] * height)
20+
for y in range(height):
21+
for x in range(width):
22+
distance = dist(width / 2, height / 2, x, y)
23+
distances[x][y] = distance / max_distance
24+
spacer = 10
25+
stroke_weight(6)
26+
no_loop() # Run once and stop
27+
28+
29+
def draw():
30+
background(0.0)
31+
# This embedded loop skips over values in the arrays based on
32+
# the spacer variable, so there are more values in the array
33+
# than are drawn here. Change the value of the spacer variable
34+
# to change the density of the points
35+
for y in range(0, height, spacer):
36+
for x in range(0, width, spacer):
37+
stroke(distances[x][y])
38+
point(x + spacer // 2, y + spacer // 2)
39+
40+
41+
run()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Array Objects.
2+
#
3+
# Demonstrates the syntax for creating a list of custom objects.
4+
from mewnala import *
5+
6+
unit = 40
7+
count = 0
8+
mods = []
9+
10+
11+
def setup():
12+
global count
13+
size(640, 360)
14+
no_stroke()
15+
wide_count = width // unit
16+
high_count = height // unit
17+
count = wide_count * high_count
18+
19+
for y in range(high_count):
20+
for x in range(wide_count):
21+
mods.append(Module(x * unit, y * unit, unit / 2, unit / 2, random(0.05, 0.8), unit))
22+
23+
24+
def draw():
25+
background(0.0)
26+
for mod in mods:
27+
mod.update()
28+
mod.display()
29+
30+
31+
class Module:
32+
def __init__(self, x_offset, y_offset, x, y, speed, unit):
33+
self.x_offset = x_offset
34+
self.y_offset = y_offset
35+
self.x = x
36+
self.y = y
37+
self.speed = speed
38+
self.unit = unit
39+
self.x_direction = 1
40+
self.y_direction = 1
41+
42+
# Custom method for updating the variables
43+
def update(self):
44+
self.x = self.x + (self.speed * self.x_direction)
45+
if self.x >= self.unit or self.x <= 0:
46+
self.x_direction *= -1
47+
self.x = self.x + (1 * self.x_direction)
48+
self.y = self.y + (1 * self.y_direction)
49+
if self.y >= self.unit or self.y <= 0:
50+
self.y_direction *= -1
51+
self.y = self.y + (1 * self.y_direction)
52+
53+
# Custom method for drawing the object
54+
def display(self):
55+
fill(1.0)
56+
ellipse(self.x_offset + self.x, self.y_offset + self.y, 6, 6)
57+
58+
59+
run()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Array.
2+
#
3+
# An array is a list of data. Each piece of data in an array
4+
# is identified by an index number representing its position in
5+
# the array. Arrays are zero based, which means that the first
6+
# element in the array is [0], the second element is [1], and so on.
7+
# In this example, an array named "coswave" is created and
8+
# filled with the cosine values. This data is displayed three
9+
# separate ways on the screen.
10+
from mewnala import *
11+
12+
coswave = []
13+
14+
15+
def setup():
16+
size(640, 360)
17+
for i in range(width):
18+
amount = remap(i, 0, width, 0, PI)
19+
coswave.append(abs(cos(amount)))
20+
background(1.0)
21+
no_loop()
22+
23+
24+
def draw():
25+
y1 = 0
26+
y2 = height // 3
27+
for i in range(width):
28+
stroke(coswave[i])
29+
line(i, y1, i, y2)
30+
31+
y1 = y2
32+
y2 = y1 + y1
33+
for i in range(width):
34+
stroke(coswave[i] / 4)
35+
line(i, y1, i, y2)
36+
37+
y1 = y2
38+
y2 = height
39+
for i in range(width):
40+
stroke(1.0 - coswave[i])
41+
line(i, y1, i, y2)
42+
43+
44+
run()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Move Eye.
2+
# by Simon Greenwold.
3+
#
4+
# The camera lifts up (controlled by mouseY) while looking at the same point.
5+
from mewnala import *
6+
7+
light = None
8+
9+
10+
def setup():
11+
global light
12+
size(640, 360)
13+
mode_3d()
14+
fill(0.8)
15+
light = directional_light((1.0, 1.0, 1.0), 4000.0, position=(0, 0, 1), look_at=(0, 0, 0))
16+
roughness(0.6)
17+
18+
19+
def draw():
20+
background(0.0)
21+
22+
# Change height of the camera with mouseY (the world is y-up)
23+
camera(30.0, -mouse_y, 220.0, # eyeX, eyeY, eyeZ
24+
0.0, 0.0, 0.0, # centerX, centerY, centerZ
25+
0.0, 1.0, 0.0) # upX, upY, upZ
26+
27+
no_stroke()
28+
box(90)
29+
stroke(1.0)
30+
line(-100, 0, 0, 100, 0, 0) # GAP: 3D line(x1, y1, z1, x2, y2, z2)
31+
line(0, -100, 0, 0, 100, 0) # GAP: 3D line(x1, y1, z1, x2, y2, z2)
32+
line(0, 0, -100, 0, 0, 100) # GAP: 3D line(x1, y1, z1, x2, y2, z2)
33+
34+
35+
run()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Perspective vs. Ortho
2+
#
3+
# Move the mouse left to right to change the "far"
4+
# parameter for the perspective() and ortho() functions.
5+
# This parameter sets the maximum distance from the
6+
# origin away from the viewer and will clip the geometry.
7+
# Click a mouse button to switch between the perspective and
8+
# orthographic projections.
9+
from mewnala import *
10+
11+
show_perspective = False
12+
light = None
13+
14+
15+
def setup():
16+
global light
17+
size(600, 360)
18+
mode_3d()
19+
no_fill()
20+
fill(1.0)
21+
no_stroke()
22+
light = directional_light((1.0, 1.0, 1.0), 4000.0, position=(0, 0, 1), look_at=(0, 0, 0))
23+
roughness(0.6)
24+
25+
26+
def draw():
27+
background(0.0)
28+
far = remap(mouse_x, 0, width, 120, 400)
29+
if show_perspective:
30+
perspective(PI / 3.0, width / height, 10, far)
31+
else:
32+
ortho(-width / 2.0, width / 2.0, -height / 2.0, height / 2.0, 10, far)
33+
# Rotations about x run the other way in the y-up world
34+
rotate_x(PI / 6)
35+
rotate_y(PI / 3)
36+
box(180)
37+
38+
39+
def mouse_pressed():
40+
global show_perspective
41+
show_perspective = not show_perspective
42+
43+
44+
run()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Perspective.
2+
#
3+
# Move the mouse left and right to change the field of view (fov).
4+
# Click to modify the aspect ratio. The perspective() function
5+
# sets a perspective projection applying foreshortening, making
6+
# distant objects appear smaller than closer ones. The parameters
7+
# define a viewing volume with the shape of truncated pyramid.
8+
# Objects near to the front of the volume appear their actual size,
9+
# while farther objects appear smaller. This projection simulates
10+
# the perspective of the world more accurately than orthographic projection.
11+
# The version of perspective without parameters sets the default
12+
# perspective and the version with four parameters allows the programmer
13+
# to set the area precisely.
14+
from mewnala import *
15+
16+
light = None
17+
18+
19+
def setup():
20+
global light
21+
size(640, 360)
22+
mode_3d()
23+
no_stroke()
24+
light = directional_light((1.0, 1.0, 1.0), 4000.0, position=(0, 0, 1), look_at=(0, 0, 0))
25+
roughness(0.6)
26+
27+
28+
def draw():
29+
background(0.0)
30+
camera_y = height / 2.0
31+
# A fov of 0 would divide by zero below
32+
fov = max(mouse_x, 1) / width * PI / 2
33+
camera_z = camera_y / tan(fov / 2.0)
34+
aspect = width / height
35+
if mouse_is_pressed:
36+
aspect = aspect / 2.0
37+
perspective(fov, aspect, camera_z / 10.0, camera_z * 10.0)
38+
39+
translate(30, 0, 0)
40+
# Rotations about x run the other way in the y-up world
41+
rotate_x(PI / 6)
42+
rotate_y(PI / 3 + mouse_y / height * PI)
43+
box(45)
44+
translate(0, 0, -50)
45+
box(30)
46+
47+
48+
run()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Brightness
2+
# by Rusty Robison.
3+
#
4+
# Brightness is the relative lightness or darkness of a color.
5+
# Move the cursor vertically over each bar to alter its brightness.
6+
from mewnala import *
7+
8+
bar_width = 20
9+
last_bar = -1
10+
11+
12+
def setup():
13+
size(640, 360)
14+
no_stroke()
15+
background(0.0)
16+
17+
18+
def draw():
19+
global last_bar
20+
which_bar = int(mouse_x) // bar_width
21+
if which_bar != last_bar:
22+
bar_x = which_bar * bar_width
23+
fill(hsva(bar_x / width * 360, 1.0, mouse_y / height))
24+
rect(bar_x, 0, bar_width, height)
25+
last_bar = which_bar
26+
27+
28+
run()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Color Variables (Homage to Albers).
2+
#
3+
# This example creates variables for colors that may be referred to
4+
# in the program by a name, rather than a number.
5+
from mewnala import *
6+
7+
inside = None
8+
middle = None
9+
outside = None
10+
11+
12+
def setup():
13+
global inside, middle, outside
14+
size(640, 360)
15+
no_stroke()
16+
no_loop()
17+
18+
inside = color(0.8, 0.4, 0.0)
19+
middle = color(0.8, 0.6, 0.0)
20+
outside = color(0.6, 0.2, 0.0)
21+
22+
# These statements are equivalent to the statements above.
23+
# Programmers may use the format they prefer.
24+
# inside = color("#CC6600")
25+
# middle = color("#CC9900")
26+
# outside = color("#993300")
27+
28+
29+
def draw():
30+
background(0.2, 0.0, 0.0)
31+
32+
push_matrix()
33+
translate(80, 80)
34+
fill(outside)
35+
rect(0, 0, 200, 200)
36+
fill(middle)
37+
rect(40, 60, 120, 120)
38+
fill(inside)
39+
rect(60, 90, 80, 80)
40+
pop_matrix()
41+
42+
push_matrix()
43+
translate(360, 80)
44+
fill(inside)
45+
rect(0, 0, 200, 200)
46+
fill(outside)
47+
rect(40, 60, 120, 120)
48+
fill(middle)
49+
rect(60, 90, 80, 80)
50+
pop_matrix()
51+
52+
53+
run()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Hue.
2+
#
3+
# Hue is the color reflected from or transmitted through an object
4+
# and is typically referred to as the name of the color such as
5+
# red, blue, or yellow. In this example, move the cursor vertically
6+
# over each bar to alter its hue.
7+
from mewnala import *
8+
9+
bar_width = 20
10+
last_bar = -1
11+
12+
13+
def setup():
14+
size(640, 360)
15+
no_stroke()
16+
background(0.0)
17+
18+
19+
def draw():
20+
global last_bar
21+
which_bar = int(mouse_x) // bar_width
22+
if which_bar != last_bar:
23+
bar_x = which_bar * bar_width
24+
fill(hsva(mouse_y / height * 360, 1.0, 1.0))
25+
rect(bar_x, 0, bar_width, height)
26+
last_bar = which_bar
27+
28+
29+
run()

0 commit comments

Comments
 (0)