Alexandre B A Villares 2018-10-19 23:51:00 -03:00
rodzic bd9dc91898
commit b6f639e52b
11 zmienionych plików z 318 dodań i 0 usunięć

69
s285_order/node.py 100644
Wyświetl plik

@ -0,0 +1,69 @@
class Node():
nodes = []
grid = dict()
ver = set()
def __init__(self, x, y, z):
self.ix = x
self.iy = y
self.iz = z
self.x = Node.border + Node.spacing / 2 + x * Node.spacing - width / 2
self.y = Node.border + Node.spacing / 2 + y * Node.spacing - width / 2
self.z = Node.border + Node.spacing / 2 + z * Node.spacing - width / 2
self.size_ = 1
self.cor = None
self.nb = {0 : False, # |* left
1 : False, # *| right
2 : False, # - top
3 : False, # _ bottom
4 : False,
5 : False,
}
self.borders = []
def plot(self):
""" draws node """
if self.cor:
noFill() #stroke(0)
stroke(self.cor)
with pushMatrix():
translate(0, 0, self.z)
#rect(0, 0, Node.spacing * self.size_, Node.spacing * self.size_)
self.draw_borders()
def draw_borders(self):
if self.borders:
for x1, y1, x2, y2 in self.borders:
line(x1, y1, x2, y2)
def update_nbs(self):
nb0 = Node.grid.get((self.ix-1, self.iy, self.iz))
self.nb[0] = True if nb0 and nb0.cor else False
nb1 = Node.grid.get((self.ix+1, self.iy, self.iz))
self.nb[1] = True if nb1 and nb1.cor else False
nb2 = Node.grid.get((self.ix, self.iy-1, self.iz))
self.nb[2] = True if nb2 and nb2.cor else False
nb3 = Node.grid.get((self.ix, self.iy+1, self.iz))
self.nb[3] = True if nb3 and nb3.cor else False
nb4 = Node.grid.get((self.ix, self.iy, self.iz-1))
self.nb[4] = True if nb4 and nb4.cor else False
nb5 = Node.grid.get((self.ix, self.iy, self.iz+1))
self.nb[5] = True if nb5 and nb5.cor else False
b = self.borders
w = h = Node.spacing * self.size_
tlX, tlY = self.x - w/2, self.y - h/2
trX, trY = self.x + w/2, self.y - h/2
blX, blY = self.x - w/2, self.y + h/2
brX, brY = self.x + w/2, self.y + h/2
if not self.nb[0]: b.append((tlX, tlY, blX, blY))
if not self.nb[1]: b.append((trX, trY, brX, brY))
if not self.nb[2]: b.append((tlX, tlY, trX, trY))
if not self.nb[3]: b.append((blX, blY, brX, brY))
for x1, y1, x2, y2 in self.borders:
Node.ver.append(((x1, y1, self.z, self.iz),
(x2, y2, self.z, self.iz)))

Wyświetl plik

@ -0,0 +1,113 @@
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME = "s284" # 2018109
OUTPUT = ".gif"
GRID_SIZE = 4
add_library('peasycam')
from random import seed
from random import choice
from random import randint
from node import Node
def setup():
global ang
ang = 0
size(500, 500, P3D)
colorMode(HSB)
strokeWeight(2)
cam = PeasyCam(this, 100)
cam.setMinimumDistance(750)
cam.setMaximumDistance(750)
random_seed(101)
init_grid(GRID_SIZE)
global segmentos, first, next, ordenados
for l in range(1):
segmentos = [t for t in Node.ver if t[0][3] == l]
first, next = segmentos[-1]
ordenados = sort(segmentos)
def draw():
global ang, segmentos, next
background(0)
rotateY(ang)
for node in Node.nodes:
node.plot()
for l in range(GRID_SIZE):
fill(255)
beginShape()
for (x, y, z, iz), (x2, y2, z2, _) in segmentos:
if l == iz:
vertex(x, y, z-10)
vertex(x2, y2, z2-10)
endShape()
# while len(segmentos)>2:
if segmentos > 0:
print segmentos
p1, p2 = segmentos.pop()
if p1[:1] == next[:1] or p2[:1] == next[:1]:
if p1 == next:
ordenados.append(p2)
next = p2
else: # p2 == next
ordenados.append(p1)
next = p1
else:
segmentos = [(p1, p2)] + segmentos
# if ang <= TWO_PI:
# #saveFrame("###.png")
# ang += 0.02
def init_grid(grid_size):
Node.border = 10
Node.spacing = (width - Node.border * 2) / grid_size
Node.nodes = []
Node.ver = []
for x in range(grid_size):
for y in range(grid_size):
for z in range(1):
new_node = Node(x, y, z)
Node.nodes.append(new_node)
Node.grid[x, y, z] = new_node
if randint(1, 10) > 5:
new_node.cor = color(z * 28, 255, 255)
else:
new_node.cor = None
for node in Node.nodes:
if node.cor:
node.update_nbs()
def keyPressed():
if key == "n":
init_grid(GRID_SIZE)
if key == "s": saveFrame("###.png")
def random_seed(s=None):
global rnd_seed
if s:
rnd_seed = s
seed(rnd_seed)
randomSeed(rnd_seed)
else:
seed(rnd_seed)
randomSeed(rnd_seed)
# print text to add to the project's README.md
def settings():
println(
"""
![{0}]({0}/{0}{2})
{1}: [code](https://github.com/villares/sketch-a-day/tree/master/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
""".format(SKETCH_NAME, SKETCH_NAME[1:], OUTPUT)
)

BIN
s294/059.png 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 9.0 KiB

BIN
s294/063.png 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 7.1 KiB

BIN
s294/069.png 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 8.6 KiB

BIN
s294/075.png 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 8.4 KiB

BIN
s294/083.png 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 7.2 KiB

BIN
s294/093.png 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 6.6 KiB

BIN
s294/099.png 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 9.4 KiB

BIN
s294/106.png 100644

Plik binarny nie jest wyświetlany.

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 8.6 KiB

136
s294/s294.pyde 100644
Wyświetl plik

@ -0,0 +1,136 @@
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME = "s293" # 20181018
OUTPUT = ".gif"
NUM = 8
BORDER = 50
SPACING = 10
def setup():
global previous_points, next_points, points
size(500, 500)
frameRate(5 )
points = previous_points = create_points()
next_points = create_points()
def create_points(non_intersecting=True):
background(200)
done = False
while not done:
points = [PVector(random(BORDER, width - BORDER),
random(BORDER, height - BORDER))
for _ in range(NUM)]
edges = pairwise(points) + [(points[-1], points[0])]
done = True
if non_intersecting:
for p1, p2 in edges:
for p3, p4 in edges:
# test only non consecutive edges
if (p1 != p3) and (p2 != p3) and (p1 != p4):
if line_instersect(Line(p1, p2), Line(p3, p4)):
stroke(255, 51)
strokeWeight(2)
line(p1.x, p1.y, p2.x, p2.y)
done = False
break
return points
def draw():
stroke(0, 0, 100)
for x in range(0, width, SPACING):
a = PVector(x, 0)
b = PVector(x, height)
inter_points = inter_line(points, Line(a, b))
draw_inter(inter_points)
stroke(100, 0, 0)
for y in range(0, height, SPACING):
a = PVector(0, y)
b = PVector(width, y)
inter_points = inter_line(next_points, Line(a, b))
draw_inter(inter_points)
def inter_line(points, L):
edges = pairwise(points) + [(points[-1], points[0])]
inter_points = []
for p1, p2 in edges:
strokeWeight(2)
line(p1.x, p1.y, p2.x, p2.y)
inter = line_instersect(Line(p1, p2), L)
if inter:
inter_points.append(inter)
return inter_points
def draw_inter(inter_points):
if inter_points and len(inter_points) > 1:
# strokeWeight(7)
# for p in inter_points:
# point(p.x, p.y)
strokeWeight(1)
inter_points.sort()
pairs = zip(inter_points[::2], inter_points[1::2])
for p1, p2 in pairs:
if p2:
line(p1.x, p1.y, p2.x, p2.y)
def keyPressed():
global points, next_points
if key == " ":
points = create_points()
next_points = create_points()
if key == "s": saveFrame("###.png")
class Line():
""" I should change this to a named tuple... """
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def line_instersect(line_a, line_b):
"""
code adapted from Bernardo Fontes
https://github.com/berinhard/sketches/
"""
x1, y1 = line_a.p1.x, line_a.p1.y
x2, y2 = line_a.p2.x, line_a.p2.y
x3, y3 = line_b.p1.x, line_b.p1.y
x4, y4 = line_b.p2.x, line_b.p2.y
try:
uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1));
except ZeroDivisionError:
return
if not(0 <= uA <= 1 and 0 <= uB <= 1):
return
x = line_a.p1.x + uA * (line_a.p2.x - line_a.p1.x)
y = line_a.p1.y + uA * (line_a.p2.y - line_a.p1.y)
return PVector(x, y)
def pairwise(iterable):
import itertools
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
# print text to add to the project's README.md
def settings():
println(
"""
![{0}]({0}/{0}{2})
{1}: [code](https://github.com/villares/sketch-a-day/tree/master/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
""".format(SKETCH_NAME, SKETCH_NAME[1:], OUTPUT)
)