back to graphs!

main
Alexandre B A Villares 2020-08-01 20:17:05 -03:00
rodzic 82a07db1b3
commit 9f566c8ad2
3 zmienionych plików z 64 dodań i 33 usunięć

Plik binarny nie jest wyświetlany.

Po

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

Wyświetl plik

@ -1,21 +1,24 @@
from graph import Graph
graph = Graph({"g": ["d", "f"],
"i": ["c"],
"c": ["i", "c", "d", "e"],
"d": ["f", "c"],
"e": ["c"],
"f": ["d", "h"],
"a": ["b"],
"h": ["f"],
"b": ["a"]
})
graph = Graph({
'a': ['b', 'k'],
'b': ['a'],
'c': ['i', 'c', 'd', 'e'],
'd': ['f', 'c'],
'e': ['c'],
'f': ['d', 'h', 'j', 'k'],
'g': ['d', 'f'],
'h': ['f'],
'i': ['c'],
'j': ['f'],
'k': ['a', 'f'],
})
MARGIN = 50
def setup():
size(500, 500)
noLoop()
setup_graph(graph)
size(600, 600)
setup_grid(graph)
fill(0)
textSize(18)
textAlign(CENTER, CENTER)
@ -23,29 +26,47 @@ def setup():
strokeWeight(3)
def draw():
background(200)
for v in grid.keys():
xa, ya = grid[v]
xa, ya, za = grid[v]
for e in graph[v]:
xb, yb = grid[e]
line(xa, ya, xb, yb)
xb, yb, zb = grid[e]
line(xa - za, ya - za, xb - zb, yb - zb)
for v in grid.keys():
x, y = grid[v]
text(v, x, y)
x, y, z = grid[v]
text(v, x - z, y - z)
saveFrame("sketch_2020_07_14c.png")
saveFrame('sketch_2020_08_01a.png')
def setup_graph(g):
def setup_grid(g):
global cols, rows, grid
n = len(g)
ang = TWO_PI / n
cols, rows = dimensionar_grade(len(g))
w, h = (width - MARGIN * 2) / cols, (height - MARGIN * 2) / rows
grid = {}
v_list = g.vertices()
for i in range(n):
v = v_list[i]
x = cos(ang * i) * width * .4 + width / 2
y = sin(ang * i) * height * .4 + height / 2
grid[v] = (x, y)
c, r = 0, 0
next = None
while v_list:
if next is None:
next = v_list.pop()
else:
v_list.remove(next)
z = len(graph[next])
grid[next] = (MARGIN + w / 2 + c * w,
MARGIN + h / 2 + r * h,
z * w / 10)
for v in graph[next]:
if v in v_list:
next = v
break
else:
if v_list:
next = None
c += 1
if c == cols:
c = 0
r += 1
def dimensionar_grade(n):
@ -57,9 +78,13 @@ def dimensionar_grade(n):
return a, b
def keyPressed():
global n
redraw()
if str(key) in '+=':
n += 1
if key == '-' and n > 2:
n -= 1
from random import choice
global graph
graph = Graph()
names = "abcdefghijkl"
for v in names:
graph.add_vertex(v)
if random(100) < 80:
graph.add_edge((v, choice(names)))
setup_grid(graph)

Wyświetl plik

@ -21,6 +21,12 @@
---
![sketch_2020_08_01a](2020/sketch_2020_08_01a/sketch_2020_08_01a.png)
[sketch_2020_08_01a](https://github.com/villares/sketch-a-day/tree/master/2020/sketch_2020_08_01a) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
---
![sketch_2020_07_31a](2020/sketch_2020_07_31a/sketch_2020_07_31a.gif)
[sketch_2020_07_31a](https://github.com/villares/sketch-a-day/tree/master/2020/sketch_2020_07_31a) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]