Skip to content

Commit bef1010

Browse files
committed
Starting optimizations to graph structure (array to list for vectors)
1 parent e28dbae commit bef1010

File tree

7 files changed

+272983
-15
lines changed

7 files changed

+272983
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ __pycache__/
66
# Pycharm
77
.idea/
88

9+
# Img output
910
temp/
1011

1112
# C extensions

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Experimenting with generative art
22

3-
Here I try and create a set of simple rules for which interact to (hopefully) create interesting emergent patterns.
3+
Here I try and create a set of simple rules for which interact to (hopefully) create interesting emergent patterns.
44

5-
<p align="center">
6-
<img src="examples/random_walks_jumps.svg" width="40%">
5+
<p align="center">
6+
<img src="examples/20181028-143633-372201-e28dbae-1a67244.png" width="40%">
77
</p>
88

99
These posts will be heavily inspired by the work of Anders Hoff, which has done some really interesting things in this area. Besides sharing is work and the [code he uses to generate them](https://github.com/inconvergent), he also writes about his process and how he creates what he does on [his website](https://inconvergent.net).
@@ -12,6 +12,6 @@ These posts will be heavily inspired by the work of Anders Hoff, which has done
1212

1313
Although you're free to use anything shared here, beware that this won't be very well maintained. I hope to eventually write a library (which will be maintained) for creating stuff like this, but that's still a ways off.
1414

15-
Using pycairo+gtk3 requires a lot of dependencies outside of python, so I've provided a conda `environment.yml` file. Check [this](https://conda.io/docs/user-guide/tasks/manage-environments.html#sharing-an-environment) out for how to use it.
15+
Using pycairo+gtk3 requires a lot of dependencies outside of python, so I've provided a conda `environment.yml` file. Check [this](https://conda.io/docs/user-guide/tasks/manage-environments.html#sharing-an-environment) out for how to use it.
1616

1717
I'm running Ubuntu 18.04, so your mileage on other systems may vary.

draw.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Canvas:
66
def __init__(self, width, height, dpi, fname, bg_rgb):
77
self.width = width*dpi
88
self.height = height*dpi
9+
self.dpi = dpi
910
self.fname = fname
1011
self.bg_rgb = bg_rgb
1112

@@ -32,7 +33,7 @@ def draw_edge(self, edge, line_width, rgb):
3233
self.ctx.move_to(*edge[0])
3334
self.ctx.line_to(*edge[1])
3435
self.ctx.save()
35-
self.ctx.set_line_width(line_width)
36+
self.ctx.set_line_width(line_width*self.dpi)
3637
self.ctx.stroke_preserve()
3738
self.ctx.fill()
3839
self.ctx.restore()
Loading

examples/20181028-143633-372201-e28dbae-1a67244.svg

Lines changed: 272965 additions & 0 deletions
Loading

objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def add_edge(self, ind_1, ind_2):
2222
raise ValueError
2323
self.edges.add((ind_1, ind_2))
2424

25-
def closest_vert(self, pos, excluding=[]):
25+
def closest_vert_dist(self, pos, excluding=[]):
2626
dist = np.linalg.norm(self.verts - pos, axis=1)
2727
dist[excluding] += dist.max() + 1
28-
return dist.min(), dist.argmin()
28+
return dist.min()

random_walks_jumps.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,33 @@
22

33
import cairo as cr
44
import numpy as np
5+
from matplotlib import cm
56

67
from objects import DrawGraph
78
from rnd import rnd_crc_pnt
89
from draw import Canvas
910
from fn import Fn
1011

1112
init_vert = [0.5, 0.5]
12-
edge_length = 0.0025
13+
edge_length = 0.001
1314

1415
graph = DrawGraph()
1516
graph.add_vertex(init_vert)
1617

1718
tries = 0
18-
while graph.verts.shape[0] < 20_000:
19-
if graph.verts.shape[0] % 100 == 0 and tries == 0:
19+
while len(graph.verts) < 150_000:
20+
if len(graph.verts) % 100 == 0 and tries == 0:
2021
print(f"vertices: {len(graph.verts)}")
2122
tries += 1
22-
new_vert = graph.verts[-1] + tries/50*edge_length*rnd_crc_pnt()
23+
new_vert = graph.verts[-1] + (1.25 + 0.1*tries)*edge_length*rnd_crc_pnt()
2324
# Don't draw edge if we wrap-around to other side
2425
if (new_vert != new_vert % 1).any():
2526
graph.add_vertex(new_vert % 1)
2627
continue
27-
closest_dist, closest_vert = graph.closest_vert(new_vert)
2828
# If we have a candidate point far away enough, add it with edge
29-
if closest_dist > 1.25*edge_length:
30-
if graph.verts.shape[0] % 100 == 0: print(tries)
29+
if graph.closest_vert_dist(new_vert) > 1.25*edge_length:
30+
if len(graph.verts) % 100 == 0:
31+
print(tries)
3132
tries = 0
3233
graph.add_vertex(new_vert)
3334
graph.add_edge(len(graph.verts) - 1, len(graph.verts) - 2)
@@ -48,4 +49,4 @@
4849
for edge in graph.edges:
4950
edge_coord = [graph.verts[i] for i in edge]
5051
if np.linalg.norm(edge_coord[0] - edge_coord[1]) < 2*edge_length:
51-
cnv.draw_edge(edge_coord, 5, [0.3, 0.3, 0.3])
52+
cnv.draw_edge(edge_coord, 0.01, [0.3, 0.3, 0.3])

0 commit comments

Comments
 (0)