Skip to content

Commit 59ad0ee

Browse files
committed
interesting mistake
1 parent 861daab commit 59ad0ee

File tree

7 files changed

+146
-48
lines changed

7 files changed

+146
-48
lines changed

angular_lines.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
import cairo as cr
4+
import numpy as np
5+
from random import choice
6+
from math import pi
7+
8+
from src.objects import DrawGraph
9+
from src.draw import Canvas
10+
from fn import Fn
11+
from matplotlib import cm
12+
13+
# Constants
14+
15+
16+
# Create graph
17+
18+
19+
# Document configuration
20+
cnv_conf = {
21+
"fname": f"temp/{Fn().name()}",
22+
"dpi": 300,
23+
"width": 10,
24+
"height": 20,
25+
"bg_rgb": [(0.05, 0.05, 0.05), (0.15, 0.15, 0.15)]
26+
}
27+
28+
n_lines = 3000
29+
cm = cm.viridis
30+
31+
# Draw to canvas
32+
with Canvas(**cnv_conf) as cnv:
33+
34+
angle = 0.5*pi*np.random.uniform()
35+
y0 = np.random.uniform(0.1*1, 1, n_lines)
36+
x0 = y0*np.tan(y0)
37+
col_mid = choice(y0)
38+
col_vec = 1.1 - y0 + np.random.normal(0, 0.5, n_lines)
39+
col_vec = (col_vec - min(col_vec))/(max(col_vec) - min(col_vec))
40+
colors = cm(col_vec, alpha=0.15)
41+
42+
for x, y, col in zip(x0, y0, colors):
43+
edge = [[0, y], [x, 0]]
44+
cnv.draw_edge(edge, 0.00001, col)

draw.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

random_walks_jumps.py

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

33
import cairo as cr
44
import numpy as np
5-
from matplotlib import cm
65

7-
from objects import DrawGraph
8-
from rnd import rnd_crc_pnt
9-
from draw import Canvas
6+
from src.objects import DrawGraph
7+
from src.rnd import rnd_crc_pnt
8+
from src.draw import Canvas
109
from fn import Fn
1110

1211
init_vert = [0.5, 0.5]
13-
edge_length = 0.0025
12+
edge_length = 0.01
13+
num_verts = 1000
1414

1515
graph = DrawGraph()
1616
graph.add_vertex(init_vert)
1717

1818
tries = 0
19-
while len(graph.verts) < 17_500:
19+
while len(graph.verts) < num_verts:
2020
if len(graph.verts) % 100 == 0 and tries == 0:
2121
print(f"vertices: {len(graph.verts)}")
2222
tries += 1
@@ -39,8 +39,8 @@
3939
"fname": f"temp/{Fn().name()}",
4040
"dpi": 300,
4141
"width": 10,
42-
"height": 10,
43-
"bg_rgb": (0.05, 0.05, 0.05)
42+
"height": 15,
43+
"bg_rgb": [(0.05, 0.05, 0.05), (0.7, 0.7, 0.7)]
4444
}
4545

4646
with Canvas(**cnv_settings) as cnv:
@@ -49,4 +49,4 @@
4949
for edge in graph.edges:
5050
edge_coord = [graph.verts[i] for i in edge]
5151
if np.linalg.norm(edge_coord[0] - edge_coord[1]) < 2*edge_length:
52-
cnv.draw_edge(edge_coord, 0.01, [0.95, 0.95, 0.95])
52+
cnv.draw_edge(edge_coord, 0.05, [0.95, 0.95, 0.95])

src/draw.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import cairo as cr
2+
3+
4+
class Canvas:
5+
6+
def __init__(self, width, height, dpi, fname, bg_rgb):
7+
"""
8+
Create a canvas object.
9+
:param width: Width in inch (of inner canvas)
10+
:param height: Height in inch (of inner canvas)
11+
:param dpi: Dots-per-inch of final png
12+
:param fname:
13+
:param bg_rgb:
14+
:param zoom: Fractional border size between outer and inner canvas
15+
"""
16+
self.inner_width = width * dpi
17+
self.inner_height = height * dpi
18+
self.dpi = dpi
19+
self.fname = fname
20+
self.zoom_factor = 1.1
21+
if width > height:
22+
self.outer_width = self.inner_width * self.zoom_factor
23+
self.outer_height = self.inner_width * self.zoom_factor
24+
else:
25+
self.outer_width = self.inner_height * self.zoom_factor
26+
self.outer_height = self.inner_height * self.zoom_factor
27+
if len(bg_rgb) is 1:
28+
self.bg_rgb = bg_rgb[0]
29+
self.fg_rgb = bg_rgb[0]
30+
elif len(bg_rgb) is 2:
31+
self.bg_rgb = bg_rgb[0]
32+
self.fg_rgb = bg_rgb[1]
33+
34+
def __enter__(self):
35+
self.surface = cr.SVGSurface(f"{self.fname}.svg",
36+
self.outer_width,
37+
self.outer_height)
38+
self.ctx = cr.Context(self.surface)
39+
40+
self.ctx.save()
41+
self.ctx.set_source_rgb(*self.bg_rgb)
42+
self.ctx.paint()
43+
self.ctx.restore()
44+
45+
# Paint inner background
46+
self.ctx.save()
47+
self.ctx.set_source_rgb(*self.fg_rgb)
48+
y_top = 0.5*(self.outer_height - self.inner_height)
49+
x_top = 0.5*(self.outer_width - self.inner_width)
50+
self.ctx.rectangle(x_top, y_top, self.inner_width, self.inner_height)
51+
self.ctx.fill()
52+
self.ctx.restore()
53+
54+
xtrans = 0.5 * (self.outer_width - self.inner_width)
55+
ytrans = 0.5 * (self.outer_height - self.inner_height)
56+
xmult = self.inner_width
57+
ymult = self.inner_height
58+
59+
trans_matrix = cr.Matrix(x0=xtrans, xx=xmult, y0=ytrans, yy=ymult, xy=0, yx=0)
60+
self.ctx.set_matrix(trans_matrix)
61+
62+
return self
63+
64+
def __exit__(self, exc_type, exc_val, exc_tb):
65+
# Close canvas and export it
66+
self.surface.write_to_png(f"{self.fname}.png")
67+
self.surface.finish()
68+
69+
def draw_edge(self, edge, line_width, rgb):
70+
self.ctx.set_source_rgba(*rgb)
71+
self.ctx.set_line_width(line_width * self.dpi)
72+
self.ctx.move_to(*edge[0])
73+
self.ctx.line_to(*edge[1])
74+
self.ctx.save()
75+
self.ctx.stroke_preserve()
76+
self.ctx.fill()
77+
self.ctx.restore()
File renamed without changes.

rnd.py renamed to src/rnd.py

File renamed without changes.

test_draw.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
3+
from src.draw import Canvas
4+
from fn import Fn
5+
6+
cnv_settings = {
7+
"fname": f"temp/test",
8+
"dpi": 300,
9+
"width": 15,
10+
"height": 10,
11+
"bg_rgb": [(0.05, 0.05, 0.05), (0.7, 0.7, 0.7)]
12+
}
13+
14+
with Canvas(**cnv_settings) as cnv:
15+
cnv.draw_edge([[0, 0], [1, 1]], 0.00001, [1, 1, 1])
16+
cnv.draw_edge([[1, 0], [0, 1]], 0.00001, [1, 1, 1])

0 commit comments

Comments
 (0)