Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
41 changes: 41 additions & 0 deletions crates/processing_pyo3/examples/Basics/Arrays/array_2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Array 2D.
#
# Demonstrates the syntax for creating a two-dimensional (2D) array.
# Values in a 2D array are accessed through two index values.
# 2D arrays are useful for storing images. In this example, each dot
# is colored in relation to its distance from the center of the image.
from mewnala import *

distances = []
max_distance = 0.0
spacer = 0


def setup():
global max_distance, spacer
size(640, 360)
max_distance = dist(width / 2, height / 2, width, height)
for x in range(width):
distances.append([0.0] * height)
for y in range(height):
for x in range(width):
distance = dist(width / 2, height / 2, x, y)
distances[x][y] = distance / max_distance
spacer = 10
stroke_weight(6)
no_loop() # Run once and stop


def draw():
background(0.0)
# This embedded loop skips over values in the arrays based on
# the spacer variable, so there are more values in the array
# than are drawn here. Change the value of the spacer variable
# to change the density of the points
for y in range(0, height, spacer):
for x in range(0, width, spacer):
stroke(distances[x][y])
point(x + spacer // 2, y + spacer // 2)


run()
59 changes: 59 additions & 0 deletions crates/processing_pyo3/examples/Basics/Arrays/array_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Array Objects.
#
# Demonstrates the syntax for creating a list of custom objects.
from mewnala import *

unit = 40
count = 0
mods = []


def setup():
global count
size(640, 360)
no_stroke()
wide_count = width // unit
high_count = height // unit
count = wide_count * high_count

for y in range(high_count):
for x in range(wide_count):
mods.append(Module(x * unit, y * unit, unit / 2, unit / 2, random(0.05, 0.8), unit))


def draw():
background(0.0)
for mod in mods:
mod.update()
mod.display()


class Module:
def __init__(self, x_offset, y_offset, x, y, speed, unit):
self.x_offset = x_offset
self.y_offset = y_offset
self.x = x
self.y = y
self.speed = speed
self.unit = unit
self.x_direction = 1
self.y_direction = 1

# Custom method for updating the variables
def update(self):
self.x = self.x + (self.speed * self.x_direction)
if self.x >= self.unit or self.x <= 0:
self.x_direction *= -1
self.x = self.x + (1 * self.x_direction)
self.y = self.y + (1 * self.y_direction)
if self.y >= self.unit or self.y <= 0:
self.y_direction *= -1
self.y = self.y + (1 * self.y_direction)

# Custom method for drawing the object
def display(self):
fill(1.0)
ellipse(self.x_offset + self.x, self.y_offset + self.y, 6, 6)


run()
44 changes: 44 additions & 0 deletions crates/processing_pyo3/examples/Basics/Arrays/array_sketch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Array.
#
# An array is a list of data. Each piece of data in an array
# is identified by an index number representing its position in
# the array. Arrays are zero based, which means that the first
# element in the array is [0], the second element is [1], and so on.
# In this example, an array named "coswave" is created and
# filled with the cosine values. This data is displayed three
# separate ways on the screen.
from mewnala import *

coswave = []


def setup():
size(640, 360)
for i in range(width):
amount = remap(i, 0, width, 0, PI)
coswave.append(abs(cos(amount)))
background(1.0)
no_loop()


def draw():
y1 = 0
y2 = height // 3
for i in range(width):
stroke(coswave[i])
line(i, y1, i, y2)

y1 = y2
y2 = y1 + y1
for i in range(width):
stroke(coswave[i] / 4)
line(i, y1, i, y2)

y1 = y2
y2 = height
for i in range(width):
stroke(1.0 - coswave[i])
line(i, y1, i, y2)


run()
35 changes: 35 additions & 0 deletions crates/processing_pyo3/examples/Basics/Camera/move_eye.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Move Eye.
# by Simon Greenwold.
#
# The camera lifts up (controlled by mouseY) while looking at the same point.
from mewnala import *

light = None


def setup():
global light
size(640, 360)
mode_3d()
fill(0.8)
light = directional_light((1.0, 1.0, 1.0), 4000.0, position=(0, 0, 1), look_at=(0, 0, 0))
roughness(0.6)


def draw():
background(0.0)

# Change height of the camera with mouseY (the world is y-up)
camera(30.0, -mouse_y, 220.0, # eyeX, eyeY, eyeZ
0.0, 0.0, 0.0, # centerX, centerY, centerZ
0.0, 1.0, 0.0) # upX, upY, upZ

no_stroke()
box(90)
stroke(1.0)
line(-100, 0, 0, 100, 0, 0) # GAP: 3D line(x1, y1, z1, x2, y2, z2)
line(0, -100, 0, 0, 100, 0) # GAP: 3D line(x1, y1, z1, x2, y2, z2)
line(0, 0, -100, 0, 0, 100) # GAP: 3D line(x1, y1, z1, x2, y2, z2)


run()
44 changes: 44 additions & 0 deletions crates/processing_pyo3/examples/Basics/Camera/orthographic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Perspective vs. Ortho
#
# Move the mouse left to right to change the "far"
# parameter for the perspective() and ortho() functions.
# This parameter sets the maximum distance from the
# origin away from the viewer and will clip the geometry.
# Click a mouse button to switch between the perspective and
# orthographic projections.
from mewnala import *

show_perspective = False
light = None


def setup():
global light
size(600, 360)
mode_3d()
no_fill()
fill(1.0)
no_stroke()
light = directional_light((1.0, 1.0, 1.0), 4000.0, position=(0, 0, 1), look_at=(0, 0, 0))
roughness(0.6)


def draw():
background(0.0)
far = remap(mouse_x, 0, width, 120, 400)
if show_perspective:
perspective(PI / 3.0, width / height, 10, far)
else:
ortho(-width / 2.0, width / 2.0, -height / 2.0, height / 2.0, 10, far)
# Rotations about x run the other way in the y-up world
rotate_x(PI / 6)
rotate_y(PI / 3)
box(180)


def mouse_pressed():
global show_perspective
show_perspective = not show_perspective


run()
48 changes: 48 additions & 0 deletions crates/processing_pyo3/examples/Basics/Camera/perspective.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Perspective.
#
# Move the mouse left and right to change the field of view (fov).
# Click to modify the aspect ratio. The perspective() function
# sets a perspective projection applying foreshortening, making
# distant objects appear smaller than closer ones. The parameters
# define a viewing volume with the shape of truncated pyramid.
# Objects near to the front of the volume appear their actual size,
# while farther objects appear smaller. This projection simulates
# the perspective of the world more accurately than orthographic projection.
# The version of perspective without parameters sets the default
# perspective and the version with four parameters allows the programmer
# to set the area precisely.
from mewnala import *

light = None


def setup():
global light
size(640, 360)
mode_3d()
no_stroke()
light = directional_light((1.0, 1.0, 1.0), 4000.0, position=(0, 0, 1), look_at=(0, 0, 0))
roughness(0.6)


def draw():
background(0.0)
camera_y = height / 2.0
# A fov of 0 would divide by zero below
fov = max(mouse_x, 1) / width * PI / 2
camera_z = camera_y / tan(fov / 2.0)
aspect = width / height
if mouse_is_pressed:
aspect = aspect / 2.0
perspective(fov, aspect, camera_z / 10.0, camera_z * 10.0)

translate(30, 0, 0)
# Rotations about x run the other way in the y-up world
rotate_x(PI / 6)
rotate_y(PI / 3 + mouse_y / height * PI)
box(45)
translate(0, 0, -50)
box(30)


run()
28 changes: 28 additions & 0 deletions crates/processing_pyo3/examples/Basics/Color/brightness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Brightness
# by Rusty Robison.
#
# Brightness is the relative lightness or darkness of a color.
# Move the cursor vertically over each bar to alter its brightness.
from mewnala import *

bar_width = 20
last_bar = -1


def setup():
size(640, 360)
no_stroke()
background(0.0)


def draw():
global last_bar
which_bar = int(mouse_x) // bar_width
if which_bar != last_bar:
bar_x = which_bar * bar_width
fill(hsva(bar_x / width * 360, 1.0, mouse_y / height))
rect(bar_x, 0, bar_width, height)
last_bar = which_bar


run()
53 changes: 53 additions & 0 deletions crates/processing_pyo3/examples/Basics/Color/color_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Color Variables (Homage to Albers).
#
# This example creates variables for colors that may be referred to
# in the program by a name, rather than a number.
from mewnala import *

inside = None
middle = None
outside = None


def setup():
global inside, middle, outside
size(640, 360)
no_stroke()
no_loop()

inside = color(0.8, 0.4, 0.0)
middle = color(0.8, 0.6, 0.0)
outside = color(0.6, 0.2, 0.0)

# These statements are equivalent to the statements above.
# Programmers may use the format they prefer.
# inside = color("#CC6600")
# middle = color("#CC9900")
# outside = color("#993300")


def draw():
background(0.2, 0.0, 0.0)

push_matrix()
translate(80, 80)
fill(outside)
rect(0, 0, 200, 200)
fill(middle)
rect(40, 60, 120, 120)
fill(inside)
rect(60, 90, 80, 80)
pop_matrix()

push_matrix()
translate(360, 80)
fill(inside)
rect(0, 0, 200, 200)
fill(outside)
rect(40, 60, 120, 120)
fill(middle)
rect(60, 90, 80, 80)
pop_matrix()


run()
29 changes: 29 additions & 0 deletions crates/processing_pyo3/examples/Basics/Color/hue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Hue.
#
# Hue is the color reflected from or transmitted through an object
# and is typically referred to as the name of the color such as
# red, blue, or yellow. In this example, move the cursor vertically
# over each bar to alter its hue.
from mewnala import *

bar_width = 20
last_bar = -1


def setup():
size(640, 360)
no_stroke()
background(0.0)


def draw():
global last_bar
which_bar = int(mouse_x) // bar_width
if which_bar != last_bar:
bar_x = which_bar * bar_width
fill(hsva(mouse_y / height * 360, 1.0, 1.0))
rect(bar_x, 0, bar_width, height)
last_bar = which_bar


run()
Loading
Loading