Skip to content

Instantly share code, notes, and snippets.

@villares
Last active Jun 8, 2022
Embed
What would you like to do?
There are several files on this Gist... the ones ending in `.pyde` are for Processing Python Mode, the others `.py` are for use with the `py5.ixora.io` library on Python 3.8+. One has a "manual" tuples option.
# for Processing Python Mode
# https://abav.lugaralgum.com/como-instalar-o-processing-modo-python/index-EN.html
add_library('pdf') # equivale a importar a lib de PDF no Processing
mode_3D = True # modo 3D ou 2D (muda com tecla '3')
save_pdf = False # Avisa que vai exportar, tecla 's'
seed = 5465 # "semente" que trava os sorteios dos números aleatórios e do noise
s = 0.005 # noise scale
def setup():
size(600, 600, P3D) # tamanho da tela do Processing
print('seed: {}'.format(seed))
def draw():
global save_pdf
randomSeed(seed)
noiseSeed(seed)
background(10)
elements = [] # ((x, w, a, b), )
margin = 50
x = margin
while x < 580 - margin:
w = max(20, min(random(20, 60), 600 - margin - x)) # largura dos elementos
a = 300 * noise(1000 + x * s) # lado a
b = 300 * noise(x * s) # lado b (a e b são altura e profundidade)
elements.append((x, w, a, b))
x += w
if mode_3D and not save_pdf:
fill(255)
lights()
push()
translate(width / 2, height / 4, -height)
rotateX(QUARTER_PI)
rotateZ(QUARTER_PI / 2)
translate(-width / 4, height / 2, 0) # -height / 2, 0)
rect_base(0, 0, 600, 400, *[(x, 0, w, b) for x, w, a, b in elements])
for x, w, a, b in elements:
rect_h(x, 0, w, b, z=a)
rotateX(HALF_PI)
rect_base(0, 0, 600, 400, *[(x, 0, w, a) for x, w, a, b in elements])
for x, w, a, b in elements:
rect_h(x, 0, w, a, z=-b)
pop()
else:
background(255)
noFill()
if save_pdf:
beginRecord(PDF, 'output###.pdf')
rect(0, 0, 600, 600)
stroke(0, 0, 200)
line(0, height / 2, margin, height / 2)
for x, w, a, b in elements:
y = 300
stroke(200, 0, 0)
line(x, y - a, x, y + b)
line(x + w, y - a, x + w, y + b)
stroke(0, 0, 200)
line(x, y - a, x + w, y - a)
line(x, y - a + b, x + w, y - a + b)
line(x, y + b, x + w, y + b)
last_x = x + w
stroke(0, 0, 200)
line(last_x, height / 2, width, height / 2)
if save_pdf:
endRecord()
save_pdf = False
def rect_h(x, y, w, h, z):
with pushMatrix():
translate(0, 0, z)
rect(x, y, w, h)
def rect_base(x, y, w, h, *furos):
if furos:
beginShape()
vertex(x, y)
vertex(x + w, y)
vertex(x + w, y + h)
vertex(x, y + h)
for furo in furos:
rect_base(*furo)
endShape(CLOSE)
else:
beginContour()
vertex(x, y)
vertex(x, y + h)
vertex(x + w, y + h)
vertex(x + w, y)
endContour()
def keyPressed():
global mode_3D, save_pdf, seed
if key == ' ':
seed = int(random(10000))
noiseSeed(seed)
print('seed: {}'.format(seed))
elif key == 's':
save_pdf = True
print('Salvando PDF')
elif key == '3':
mode_3D = not mode_3D
# Para atividade com corte laser no Sesc Av. Paulista
# Usar com Processing IDE e modo Python
# https://abav.lugaralgum.com/como-instalar-o-processing-modo-python/
add_library('pdf') # equivale a importar a lib de PDF no Processing
mode_3D = True # modo 3D ou 2D (muda com tecla '3')
save_pdf = False # Avisa que vai exportar, tecla 's'
s = 0.005 # 'escala' do Perlin noise
def setup():
size(600, 600, P3D) # tamanho da tela do Processing
def draw():
global save_pdf
background(10)
# versão "manual edite as tuplas
elementos = [ # [(x, largura, altura, profundidade), ... ]
(50, 60, 200, 30),
(110, 60, 50, 100),
(220, 160, 80, 60),
(380, 60, 180, 40),
]
margem = elementos[0][0] # margem é o x do primeiro elemento
# Versão "automática" com Perlin noise - use noiseSeed(nnn) para travar (escolha o número)
# # noiseSeed(123)
# elementos = []
# margem = x = 50
# while x < 580 - margem:
# w = 60 * noise(2000 + x * s * 10) # largura do elemento
# a = 300 * noise(1000 + x * s) # altura do elemento
# b = 300 * noise(x * s) # profundidade do elemento
# elementos.append((x, w, a, b))
# x += w
if mode_3D and not save_pdf:
fill(255)
stroke(0)
push()
translate(width / 2, height / 4, -height)
rotateX(QUARTER_PI)
rotateZ(QUARTER_PI / 2)
translate(-width / 4, height / 2, 0) # -height / 2, 0)
rect_base(0, 0, 600, 400, *[(x, 0, w, b) for x, w, a, b in elementos])
for x, w, a, b in elementos:
rect_h(x, 0, w, b, z=a)
rotateX(HALF_PI)
rect_base(0, 0, 600, 400, *[(x, 0, w, a) for x, w, a, b in elementos])
for x, w, a, b in elementos:
rect_h(x, 0, w, a, z=-b)
pop()
else:
background(255)
noFill()
if save_pdf:
beginRecord(PDF, 'output###.pdf')
rect(0, 0, 600, 600)
last_x = 0
for x, w, a, b in elementos:
if x != last_x:
stroke(0, 0, 200)
line(last_x, height / 2, x, height / 2)
y = 300
stroke(200, 0, 0)
line(x, y - a, x, y + b)
line(x + w, y - a, x + w, y + b)
stroke(0, 0, 200)
line(x, y - a, x + w, y - a)
line(x, y - a + b, x + w, y - a + b)
line(x, y + b, x + w, y + b)
last_x = x + w
stroke(0, 0, 200)
line(last_x, height / 2, width, height / 2)
if save_pdf:
endRecord()
save_pdf = False
def rect_h(x, y, w, h, z):
with pushMatrix():
translate(0, 0, z)
rect(x, y, w, h)
def rect_base(x, y, w, h, *furos):
if furos:
beginShape()
vertex(x, y)
vertex(x + w, y)
vertex(x + w, y + h)
vertex(x, y + h)
for furo in furos:
rect_base(*furo)
endShape(CLOSE)
else:
beginContour()
vertex(x, y)
vertex(x, y + h)
vertex(x + w, y + h)
vertex(x + w, y)
endContour()
def keyPressed():
global mode_3D, save_pdf
if key == 's':
save_pdf = True
print('Salvando PDF')
elif key == '3':
mode_3D = not mode_3D
# You'll need to install py5 - https://py5.ixora.io
# Try this: https://github.com/tabreturn/thonny-py5mode
import py5
modo_3D = True # modo 3D ou 2D (muda com tecla '3')
save_pdf = False # Avisa que vai exportar, tecla 's'
seed = 5465 # "semente" que trava os sorteios dos números aleatórios e do noise
s = 0.005 # noise scale
def setup():
py5.size(600, 600, py5.P3D) # tamanho da tela do Processing
print('seed: {}'.format(seed))
def draw():
global save_pdf
py5.random_seed(seed)
py5.noise_seed(seed)
py5.background(10)
elements = [] # ((x, w, a, b), )
margin = 50
x = margin
while x < 580 - margin:
# largura dos elementos
w = max(20, min(py5.random(20, 60), 600 - margin - x))
a = 300 * py5.noise(1000 + x * s) # lado a
# lado b (a e b são altura e profundidade)
b = 300 * py5.noise(x * s)
elements.append((x, w, a, b))
x += w
if modo_3D and not save_pdf:
py5.stroke(0)
py5.fill(255)
py5.lights()
py5.push()
py5.translate(py5.width / 2, py5.height / 4, -py5.height)
py5.rotate_x(py5.QUARTER_PI)
py5.rotate_z(py5.QUARTER_PI / 2)
py5.translate(-py5.width / 4, py5.height / 2, 0) # -height / 2, 0)
rect_base(0, 0, 600, 400, *[(x, 0, w, b) for x, w, a, b in elements])
for x, w, a, b in elements:
rect_h(x, 0, w, b, z=a)
py5.rotate_x(py5.HALF_PI)
rect_base(0, 0, 600, 400, *[(x, 0, w, a) for x, w, a, b in elements])
for x, w, a, b in elements:
rect_h(x, 0, w, a, z=-b)
py5.pop()
else:
py5.background(255)
py5.no_fill()
if save_pdf:
py5.begin_record(py5.PDF, 'output###.pdf')
py5.rect(0, 0, 600, 600)
py5.stroke(0, 0, 200)
py5.line(0, py5.height / 2, margin, py5.height / 2)
for x, w, a, b in elements:
y = 300
py5.stroke(200, 0, 0)
py5.line(x, y - a, x, y + b)
py5.line(x + w, y - a, x + w, y + b)
py5.stroke(0, 0, 200)
py5.line(x, y - a, x + w, y - a)
py5.line(x, y - a + b, x + w, y - a + b)
py5.line(x, y + b, x + w, y + b)
last_x = x + w
py5.stroke(0, 0, 200)
py5.line(last_x, py5.height / 2, py5.width, py5.height / 2)
if save_pdf:
py5.end_record()
save_pdf = False
def rect_h(x, y, w, h, z):
with py5.push_matrix():
py5.translate(0, 0, z)
py5.rect(x, y, w, h)
def rect_base(x, y, w, h, *furos):
if furos:
py5.begin_shape()
py5.vertex(x, y)
py5.vertex(x + w, y)
py5.vertex(x + w, y + h)
py5.vertex(x, y + h)
for furo in furos:
rect_base(*furo)
py5.end_shape(py5.CLOSE)
else:
py5.begin_contour()
py5.vertex(x, y)
py5.vertex(x, y + h)
py5.vertex(x + w, y + h)
py5.vertex(x + w, y)
py5.end_contour()
def key_pressed():
global modo_3D, save_pdf, seed
if py5.key == ' ':
seed = int(py5.random(10000))
py5.noise_seed(seed)
print('seed: {}'.format(seed))
elif py5.key == 's':
save_pdf = True
print('Salvando PDF')
elif py5.key == '3':
modo_3D = not modo_3D
py5.run_sketch()
# You'll need to install py5 - https://py5.ixora.io
# check instructions for imported mode
# Uncomment lines 26 to 35 for "automatic" Perlin noise + random version
modo_3D = True # toggle 3D pressing '3'
save_pdf = False # to save press 's'
seed = 5465 # lock random and noise
s = 0.005 # noise scale
def setup():
size(600, 600, P3D)
print('seed: {}'.format(seed))
def draw():
global save_pdf
background(10)
# "Manual" hard-coded tuples of 4 elements
elements = [ # [(x, el_width, el_height, el_depth), ... ]
(50, 60, 200, 30),
(110, 60, 50, 100),
(220, 160, 80, 60),
(380, 60, 180, 40),
]
margin = elements[0][0]
# "Automatic" Perlin noise based
# random_seed(seed)
# noise_seed(seed)
# elements = [] # ((x, ew, eh, ed), )
# x = margin = 50
# while x < 580 - margin:
# ew = max(20, min(random(20, 60), 600 - margin - x))
# eh = 300 * noise(1000 + x * s)
# ed = 300 * noise(x * s)
# elements.append((x, ew, eh, ed))
# x += ew
# Drawing part
if modo_3D and not save_pdf:
stroke(0)
fill(255)
lights()
push()
translate(width / 2, height / 4, -height)
rotate_x(QUARTER_PI)
rotate_z(QUARTER_PI / 2)
translate(-width / 4, height / 2, 0) # -height / 2, 0)
rect_base(0, 0, 600, 400, *[(x, 0, w, b) for x, w, a, b in elements])
for x, w, a, b in elements:
rect_h(x, 0, w, b, z=a)
rotate_x(HALF_PI)
rect_base(0, 0, 600, 400, *[(x, 0, w, a) for x, w, a, b in elements])
for x, w, a, b in elements:
rect_h(x, 0, w, a, z=-b)
pop()
else:
background(255)
no_fill()
if save_pdf:
begin_record(PDF, 'output###.pdf')
rect(0, 0, 600, 600)
last_x = 0
for x, w, a, b in elements:
if x != last_x:
stroke(0, 0, 200)
line(last_x, height / 2, x, height / 2)
y = 300
stroke(200, 0, 0)
line(x, y - a, x, y + b)
line(x + w, y - a, x + w, y + b)
stroke(0, 0, 200)
line(x, y - a, x + w, y - a)
line(x, y - a + b, x + w, y - a + b)
line(x, y + b, x + w, y + b)
last_x = x + w
stroke(0, 0, 200)
line(last_x, height / 2, width, height / 2)
if save_pdf:
end_record()
save_pdf = False
def rect_h(x, y, w, h, z):
with push_matrix():
translate(0, 0, z)
rect(x, y, w, h)
def rect_base(x, y, w, h, *furos):
if furos:
begin_shape()
vertex(x, y)
vertex(x + w, y)
vertex(x + w, y + h)
vertex(x, y + h)
for furo in furos:
rect_base(*furo)
end_shape(CLOSE)
else:
begin_contour()
vertex(x, y)
vertex(x, y + h)
vertex(x + w, y + h)
vertex(x + w, y)
end_contour()
def key_pressed():
global modo_3D, save_pdf, seed
if key == ' ':
seed = int(random(10000))
noise_seed(seed)
print('seed: {}'.format(seed))
elif key == 's':
save_pdf = True
print('Salvando PDF')
elif key == '3':
modo_3D = not modo_3D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment