Alexandre B A Villares 2018-10-23 22:46:32 -02:00
rodzic ae172d1f99
commit 9e5a0b921c
4 zmienionych plików z 196 dodań i 24 usunięć

Wyświetl plik

@ -6,6 +6,11 @@ Hi! I'm [Alexandre Villares](https://abav.lugaralgum.com), let's see if I can ma
If you enjoy this, be a [patreon](https://patreon.com/arteprog) or make a donation [here](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=HCGAKACDMVNV2)
---
![s298](s298/s298.png)
298: [code](https://github.com/villares/sketch-a-day/tree/master/s298) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
---

Wyświetl plik

@ -0,0 +1,132 @@
def create_points(non_intersecting=True):
background(200)
done = False
while not done:
poly_points = [PVector(random(BORDER, width - BORDER),
random(BORDER, height - BORDER)
)
for _ in range(NUM)]
ed = edges(poly_points)
done = True
if non_intersecting:
for p1, p2 in ed[::-1]:
for p3, p4 in ed[2::]:
# test only non consecutive edges
if (p1 != p3) and (p2 != p3) and (p1 != p4):
if line_instersect(Line(p1, p2), Line(p3, p4)):
done = False
break
return poly_points
def is_inside(x, y, poly_points):
min_, max_ = min_max(poly_points)
if x < min_.x or y < min_.y or x > max_.x or y > max_.y:
return False
a = PVector(x, min_.y)
b = PVector(x, max_.y)
v_lines = inter_lines(Line(a, b), poly_points)
if not v_lines:
return False
a = PVector(min_.x, y)
b = PVector(max_.x, y)
h_lines = inter_lines(Line(a, b), poly_points)
if not h_lines:
return False
for v in v_lines:
for h in h_lines:
if line_instersect(v, h):
return True
return False
def inter_lines(L, poly_points):
inter_points = []
for p1, p2 in edges(poly_points):
inter = line_instersect(Line(p1, p2), L)
if inter:
inter_points.append(inter)
if not inter_points:
return []
inter_lines = []
if len(inter_points) > 1:
inter_points.sort()
pairs = zip(inter_points[::2], inter_points[1::2])
for p1, p2 in pairs:
if p2:
inter_lines.append(Line(PVector(p1.x, p1.y),
PVector(p2.x, p2.y)))
return inter_lines
class Line():
""" I should change this to a named tuple... """
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def plot(self):
line(self.p1.x, self.p1.y, self.p2.x, self.p2.y)
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 edges(poly_points):
return pairwise(poly_points) + [(poly_points[-1], poly_points[0])]
def pairwise(iterable):
import itertools
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
def min_max(points):
points = iter(points)
try:
p = points.next()
min_x, min_y = max_x, max_y = p.x, p.y
except StopIteration:
raise ValueError, "min_max requires at least one point"
for p in points:
if p.x < min_x:
min_x = p.x
elif p.x > max_x:
max_x = p.x
if p.y < min_y:
min_y = p.y
elif p.y > max_y:
max_y = p.y
return (PVector(min_x, min_y),
PVector(max_x, max_y))

BIN
s298/s298.png 100644

Plik binarny nie jest wyświetlany.

Po

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

Wyświetl plik

@ -3,14 +3,21 @@ SKETCH_NAME = "s298" # 20181023
OUTPUT = ".png"
GRID_SIZE = 16
from line_geometry import edges
from line_geometry import Line
from line_geometry import inter_lines
def setup():
global xo, yo
size(500, 500)
init_grid(GRID_SIZE)
def draw():
translate(Cell.spacing/2 + width/2, Cell.spacing/2 + height/2)
noLoop()
background(200)
for c in Cell.cells:
c.plot()
c.draw_vers()
c.hatch()
def init_grid(grid_size):
Cell.border = 50
@ -27,8 +34,11 @@ def init_grid(grid_size):
for x in range(-1, grid_size+1, 2):
for y in range(-1, grid_size+1, 2):
new_node = Node(x, y)
Cell.cells.append(new_node) # mudar!
#Cell.cells.append(new_node) # mudar!
Cell.grid[x, y] = new_node # extrarir do dict
for c in Cell.cells:
c.update_vers()
class Node():
nodes = []
@ -37,36 +47,61 @@ class Node():
def __init__(self, x, y):
self.ix = x
self.iy = y
self.px = Cell.border + Cell.spacing / 2 + x * Cell.spacing - width / 2
self.py = Cell.border + Cell.spacing / 2 + y * Cell.spacing - width / 2
def plot(self):
ellipse(self.px, self.py, 5, 5)
self.px = Cell.border + Cell.spacing / 2 + x * Cell.spacing
self.py = Cell.border + Cell.spacing / 2 + y * Cell.spacing
self.px += random(-10, 10)
self.py += random(-10, 10)
self.x = self.px
self.y = self.py
class Cell():
cells = []
grid = dict()
ver = []
vers = []
def __init__(self, x, y):
self.ix = x
self.iy = y
self.px = Cell.border + Cell.spacing / 2 + x * Cell.spacing - width / 2
self.py = Cell.border + Cell.spacing / 2 + y * Cell.spacing - width / 2
self.ver = []
def plot(self):
ellipse(self.px, self.py, 10, 10)
def draw_ver(self):
if len(self.ver) > 1:
for n0, n1 in pairwise(self.ver):
line(n0.x, n0.y, n1.x, n1.y)
self.px = Cell.border + Cell.spacing / 2 + x * Cell.spacing
self.py = Cell.border + Cell.spacing / 2 + y * Cell.spacing
self.vers = []
def draw_vers(self):
if len(self.vers) > 1:
for n0, n1 in edges(self.vers):
line(n0.px, n0.py, n1.px, n1.py)
for l in self.lines:
l.plot()
def update_vers(self):
v0 = Cell.grid.get((self.ix-1, self.iy-1))
v1 = Cell.grid.get((self.ix-1, self.iy+1))
v3 = Cell.grid.get((self.ix+1, self.iy-1))
v2 = Cell.grid.get((self.ix+1, self.iy+1))
self.vers = [v for v in [v0, v1, v2, v3] if v]
self.hatch()
def hatch(self):
poly_points = self.vers
# min_, max_ = min_max(poly_points)
if random(10) > 5:
for y in range(-height, height*2, int(random(2, 7))):
a = PVector(-width, y)
b = PVector(width*2, y)
lines = inter_lines(Line(a, b), poly_points)
if lines: self.lines.append(lines)
else:
for x in range(-width, width*2, int(random(2, 7))):
a = PVector(x, -height)
b = PVector(x, height*2,)
lines = inter_lines(Line(a, b), poly_points)
if lines: self.lines.append(lines)
def keyPressed():
if key == "n":
init_grid(GRID_SIZE)
if key == "s": saveFrame("###.png")
# print text to add to the project's README.md
def settings():
println(