sketch-a-day/s050/s050.pyde

65 wiersze
1.8 KiB
Plaintext
Czysty Zwykły widok Historia

2018-02-19 21:26:46 +00:00
"""
sketch 50 180219 - Alexandre B A Villares
https://abav.lugaralgum.com/sketch-a-day
"""
2018-02-19 17:53:30 +00:00
import random as rnd
LISTA = []
2018-02-19 20:43:58 +00:00
MARGIN = 100
2018-02-19 17:53:30 +00:00
def setup():
2018-02-19 20:43:58 +00:00
size(712, 712)
2018-02-19 17:53:30 +00:00
noFill()
2018-02-19 20:43:58 +00:00
nova_lista()
println("'s' to save, and 'n' for a new drawing")
2018-02-19 17:53:30 +00:00
2018-02-19 20:43:58 +00:00
def nova_lista():
2018-02-19 17:53:30 +00:00
LISTA[:] = []
2018-02-19 20:43:58 +00:00
for _ in range(30):
2018-02-19 17:53:30 +00:00
LISTA.append((
random(MARGIN, width - MARGIN), # x
random(MARGIN, height - MARGIN), # y
2018-02-19 21:26:46 +00:00
rnd.choice([10, 20, 30]), # circle size
2018-02-19 20:43:58 +00:00
rnd.choice([2, 4, 6]), # strokeWeight
2018-02-19 21:26:46 +00:00
rnd.choice([True, False]), # is arrow
list() # sub_list of nodes
2018-02-19 17:53:30 +00:00
))
for node in LISTA:
random_node = rnd.choice(LISTA)
if random_node != node:
2018-02-19 21:26:46 +00:00
node[-1].append(random_node) # adds random node to the sub_list
2018-02-19 17:53:30 +00:00
2018-02-19 20:43:58 +00:00
def seta(x1, y1, x2, y2, shorter=12, head=12):
""" draws an arrow """
L = dist(x1, y1, x2, y2)
2018-02-19 17:53:30 +00:00
with pushMatrix():
translate(x2, y2)
angle = atan2(x1 - x2, y2 - y1)
rotate(angle)
2018-02-19 20:43:58 +00:00
offset = -shorter * .6
line(0, offset, 0, -L - offset)
2018-02-19 17:53:30 +00:00
line(0, offset, -head / 3, -head + offset)
line(0, offset, head / 3, -head + offset)
2018-02-19 20:43:58 +00:00
def draw():
background(200)
2018-02-19 21:26:46 +00:00
# x, y, s: circle size, w: strokeWeightm, arrow T/F, points to...
for x1, y1, d1, sw, arrow, points_to in LISTA:
strokeWeight(sw)
for other in points_to:
x2, y2 = other[0], other[1]
2018-02-19 20:43:58 +00:00
if arrow:
stroke(0)
2018-02-19 21:26:46 +00:00
# x1, y1, x2, y2, circle offset, arrow head size
seta(x1, y1, x2, y2, d1, sw * 5)
2018-02-19 20:43:58 +00:00
else:
stroke(255)
2018-02-19 21:26:46 +00:00
line(x1, y1, x2, y2)
ellipse(x1, y1, d1, d1)
2018-02-19 20:43:58 +00:00
def keyPressed():
if key == 's':
saveFrame("####.png")
if key == 'n':
nova_lista()