Skip to content

Commit ba000ee

Browse files
committed
offset graph towards center
1 parent caa0884 commit ba000ee

File tree

2 files changed

+11
-71
lines changed

2 files changed

+11
-71
lines changed

pyzx/drawing.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,21 +286,22 @@ def draw_matplotlib(
286286
# library_code += f.read() + '\n'
287287
# library_code += '</script>'
288288
# display(HTML(library_code))
289+
289290
def auto_draw_vertex_locs(g:BaseGraph[VT, ET]): #Force-based graph drawing algorithm given by Eades(1984):
290-
c1 = 2
291+
c1 = 2 #Sample parameters that work decently well
291292
c2 = 1
292293
c3 = 1
293294
c4 = .1
294295
v_loc:Dict[VT, Tuple[int, int]] = dict()
295296
for v in g.vertices():
296-
v_loc[v]=(random.random()*10, random.random()*10)
297+
v_locs[v]=(random.random()*math.sqrt(g.num_vertices()), random.random()*math.sqrt(g.num_vertices()))
297298
for i in range(100): #100 iterations of force-based drawing
298299
forces:Dict[VT, Tuple[int, int]] = dict()
299300
for v in g.vertices():
300301
forces[v] = (0, 0)
301302
for v1 in g.vertices():
302303
if(v!=v1):
303-
diff = (v_loc[v][0]-v_loc[v1][0], v_loc[v][1]-v_loc[v1][1])
304+
diff = (v_locs[v][0]-v_locs[v1][0], v_locs[v][1]-v_locs[v1][1])
304305
d = math.sqrt(diff[0]*diff[0]+diff[1]*diff[1])
305306
if g.connected(v1, v): #edge between vertices: apply rule c1*log(d/c2)
306307
force_mag = -c1*math.log(d/c2) #negative force attracts
@@ -311,15 +312,13 @@ def auto_draw_vertex_locs(g:BaseGraph[VT, ET]): #Force-based graph drawing algor
311312
v_force = (diff[0]*force_mag*c4/d, diff[1]*force_mag*c4/d)
312313
forces[v] = (forces[v][0]+v_force[0], forces[v][1]+v_force[1])
313314
for v in g.vertices(): #leave y value constant if input or output
314-
v_loc[v]=(v_loc[v][0]+forces[v][0], v_loc[v][1]+forces[v][1])
315+
v_locs[v]=(v_locs[v][0]+forces[v][0], v_locs[v][1]+forces[v][1])
315316
max_x = max(v[0] for v in v_loc.values())
316317
min_x = min(v[0] for v in v_loc.values())
317318
max_y = max(v[1] for v in v_loc.values())
318319
min_y = min(v[1] for v in v_loc.values())
319-
v_loc = {k:(v[0]-min_x, v[1]-min_y) for k, v in v_loc.items()} #dont rescale
320-
321-
#v_loc = {k:((v[0]-min_x)*(x_scale/(max_x-min_x)), (v[1]-min_y)*y_scale/(max_y-min_y)) for k, v in v_loc.items()} #rescale
322-
return v_loc, max_x-min_x, max_y-min_y
320+
v_locs = {k:(v[0]-min_x, v[1]-min_y) for k, v in v_locs.items()} #translate to origin
321+
return v_locs, max_x-min_x, max_y-min_y
323322

324323

325324
def draw_d3(
@@ -352,8 +351,8 @@ def draw_d3(
352351
if scale > 50: scale = 50
353352
if scale < 20: scale = 20
354353

355-
w = w * scale
356-
h = h * scale
354+
w = (w+2) * scale
355+
h = (h+3) * scale
357356
else:
358357
minrow = min([g.row(v) for v in g.vertices()], default=0)
359358
maxrow = max([g.row(v) for v in g.vertices()], default=0)
@@ -372,8 +371,8 @@ def draw_d3(
372371
if node_size < 2: node_size = 2
373372

374373
nodes = [{'name': str(v),
375-
'x': v_dict[v][0]*scale if auto_draw else (g.row(v)-minrow + 1) * scale,
376-
'y': v_dict[v][1]*scale if auto_draw else (g.qubit(v)-minqub + 2) * scale,
374+
'x': (v_dict[v][0]+1)*scale if auto_draw else (g.row(v)-minrow + 1) * scale,
375+
'y': (v_dict[v][1]+2)*scale if auto_draw else (g.qubit(v)-minqub + 2) * scale,
377376
't': g.type(v),
378377
'phase': phase_to_s(g.phase(v), g.type(v)) if g.type(v) != VertexType.Z_BOX else str(get_z_box_label(g, v)),
379378
'ground': g.is_ground(v),

tests/test_drawing.py

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

0 commit comments

Comments
 (0)