kopia lustrzana https://github.com/villares/sketch-a-day
				
				
				
			back to graphs!
							rodzic
							
								
									82a07db1b3
								
							
						
					
					
						commit
						9f566c8ad2
					
				
										
											Plik binarny nie jest wyświetlany.
										
									
								
							| Po Szerokość: | Wysokość: | Rozmiar: 19 KiB | 
|  | @ -1,21 +1,24 @@ | ||||||
| from graph import Graph | from graph import Graph | ||||||
| 
 | 
 | ||||||
| graph = Graph({"g": ["d", "f"], | graph = Graph({ | ||||||
|                "i": ["c"], |     'a': ['b', 'k'], | ||||||
|                "c": ["i", "c", "d", "e"], |     'b': ['a'], | ||||||
|                "d": ["f", "c"], |     'c': ['i', 'c', 'd', 'e'], | ||||||
|                "e": ["c"], |     'd': ['f', 'c'], | ||||||
|                "f": ["d", "h"], |     'e': ['c'], | ||||||
|                "a": ["b"], |     'f': ['d', 'h', 'j', 'k'], | ||||||
|                "h": ["f"], |     'g': ['d', 'f'], | ||||||
|                "b": ["a"] |     'h': ['f'], | ||||||
|                }) |     'i': ['c'], | ||||||
|  |     'j': ['f'], | ||||||
|  |     'k': ['a', 'f'], | ||||||
|  | }) | ||||||
| 
 | 
 | ||||||
|  | MARGIN = 50 | ||||||
| 
 | 
 | ||||||
| def setup(): | def setup(): | ||||||
|     size(500, 500) |     size(600, 600) | ||||||
|     noLoop() |     setup_grid(graph) | ||||||
|     setup_graph(graph) |  | ||||||
|     fill(0) |     fill(0) | ||||||
|     textSize(18) |     textSize(18) | ||||||
|     textAlign(CENTER, CENTER) |     textAlign(CENTER, CENTER) | ||||||
|  | @ -23,29 +26,47 @@ def setup(): | ||||||
|     strokeWeight(3) |     strokeWeight(3) | ||||||
| 
 | 
 | ||||||
| def draw(): | def draw(): | ||||||
|  |     background(200) | ||||||
|     for v in grid.keys(): |     for v in grid.keys(): | ||||||
|         xa, ya = grid[v] |         xa, ya, za = grid[v] | ||||||
|         for e in graph[v]: |         for e in graph[v]: | ||||||
|             xb, yb = grid[e] |             xb, yb, zb = grid[e] | ||||||
|             line(xa, ya, xb, yb) |             line(xa - za, ya - za, xb - zb, yb - zb) | ||||||
| 
 | 
 | ||||||
|     for v in grid.keys(): |     for v in grid.keys(): | ||||||
|         x, y = grid[v] |         x, y, z = grid[v] | ||||||
|         text(v, x, y) |         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 |     global cols, rows, grid | ||||||
|     n = len(g) |     cols, rows = dimensionar_grade(len(g)) | ||||||
|     ang = TWO_PI / n |     w, h = (width - MARGIN * 2) / cols, (height - MARGIN * 2) / rows | ||||||
|     grid = {} |     grid = {} | ||||||
|     v_list = g.vertices() |     v_list = g.vertices() | ||||||
|     for i in range(n): |     c, r = 0, 0 | ||||||
|         v = v_list[i] |     next = None | ||||||
|         x = cos(ang * i) * width * .4 + width / 2 |     while v_list: | ||||||
|         y = sin(ang * i) * height * .4 + height / 2 |         if next is None: | ||||||
|         grid[v] = (x, y) |             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): | def dimensionar_grade(n): | ||||||
|  | @ -57,9 +78,13 @@ def dimensionar_grade(n): | ||||||
|     return a, b |     return a, b | ||||||
| 
 | 
 | ||||||
| def keyPressed(): | def keyPressed(): | ||||||
|     global n |     from random import choice | ||||||
|     redraw() |     global graph | ||||||
|     if str(key) in '+=': |     graph = Graph() | ||||||
|         n += 1 |     names = "abcdefghijkl" | ||||||
|     if key == '-' and n > 2: |     for v in names: | ||||||
|         n -= 1 |         graph.add_vertex(v) | ||||||
|  |         if random(100) < 80: | ||||||
|  |             graph.add_edge((v, choice(names))) | ||||||
|  | 
 | ||||||
|  |     setup_grid(graph) | ||||||
|  |  | ||||||
|  | @ -21,6 +21,12 @@ | ||||||
| 
 | 
 | ||||||
| --- | --- | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | 
 | ||||||
|  | [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](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)] | [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)] | ||||||
|  |  | ||||||
		Ładowanie…
	
		Reference in New Issue
	
	 Alexandre B A Villares
						Alexandre B A Villares