|
| 1 | +""" Module containing layout related algorithms.""" |
| 2 | +from typing import List, Tuple, Union |
| 3 | + |
| 4 | + |
| 5 | +def _get_coords( |
| 6 | + values: List[Union[float, int, None]] |
| 7 | +) -> Tuple[ |
| 8 | + List[Tuple[int, int, Union[float, int, None]]], List[Tuple[int, int, int, int]] |
| 9 | +]: |
| 10 | + """Generate the coordinates used for rendering the nodes and edges. |
| 11 | +
|
| 12 | + node and edges are stored as tuples in the form node: (x, y, label) and |
| 13 | + edge: (x1, y1, x2, y2) |
| 14 | +
|
| 15 | + Each coordinate is relative y is the depth, x is the position of the node |
| 16 | + on a level from left to right 0 to 2**depth -1 |
| 17 | +
|
| 18 | + :param values: Values of the binary tree. |
| 19 | + :type values: list of ints |
| 20 | + :return: nodes and edges list |
| 21 | + :rtype: two lists of tuples |
| 22 | +
|
| 23 | + """ |
| 24 | + x = 0 |
| 25 | + y = 0 |
| 26 | + nodes = [] |
| 27 | + edges = [] |
| 28 | + |
| 29 | + # root node |
| 30 | + nodes.append((x, y, values[0])) |
| 31 | + # append other nodes and their edges |
| 32 | + y += 1 |
| 33 | + for value in values[1:]: |
| 34 | + if value is not None: |
| 35 | + nodes.append((x, y, value)) |
| 36 | + edges.append((x // 2, y - 1, x, y)) |
| 37 | + x += 1 |
| 38 | + # check if level is full |
| 39 | + if x == 2 ** y: |
| 40 | + x = 0 |
| 41 | + y += 1 |
| 42 | + return nodes, edges |
| 43 | + |
| 44 | + |
| 45 | +def generate_svg(values: List[Union[float, int, None]]) -> str: |
| 46 | + """Generate a svg image from a binary tree |
| 47 | +
|
| 48 | + A simple layout is used based on a perfect tree of same height in which all |
| 49 | + leaves would be regularly spaced. |
| 50 | +
|
| 51 | + :param values: Values of the binary tree. |
| 52 | + :type values: list of ints |
| 53 | + :return: the svg image of the tree. |
| 54 | + :rtype: str |
| 55 | + """ |
| 56 | + node_size = 16.0 |
| 57 | + stroke_width = 1.5 |
| 58 | + gutter = 0.5 |
| 59 | + x_scale = (2 + gutter) * node_size |
| 60 | + y_scale = 3.0 * node_size |
| 61 | + |
| 62 | + # retrieve relative coordinates |
| 63 | + nodes, edges = _get_coords(values) |
| 64 | + y_min = min([n[1] for n in nodes]) |
| 65 | + y_max = max([n[1] for n in nodes]) |
| 66 | + |
| 67 | + # generate the svg string |
| 68 | + svg = f""" |
| 69 | + <svg width="{x_scale * 2**y_max}" height="{y_scale * (2 + y_max)}" |
| 70 | + xmlns="http://www.w3.org/2000/svg"> |
| 71 | + <style> |
| 72 | + .bt-label {{ |
| 73 | + font: 300 {node_size}px sans-serif;; |
| 74 | + text-align: center; |
| 75 | + dominant-baseline: middle; |
| 76 | + text-anchor: middle; |
| 77 | + }} |
| 78 | + .bt-node {{ |
| 79 | + fill: lightgray; |
| 80 | + stroke-width: {stroke_width}; |
| 81 | + }} |
| 82 | +
|
| 83 | + </style> |
| 84 | + <g stroke="#111"> |
| 85 | + """ |
| 86 | + # scales |
| 87 | + |
| 88 | + def scalex(x: int, y: int) -> float: |
| 89 | + depth = y_max - y |
| 90 | + # offset |
| 91 | + x = 2 ** (depth + 1) * x + 2 ** depth - 1 |
| 92 | + return 1 + node_size + x_scale * x / 2 |
| 93 | + |
| 94 | + def scaley(y: int) -> float: |
| 95 | + return float(y_scale * (1 + y - y_min)) |
| 96 | + |
| 97 | + # edges |
| 98 | + def svg_edge(x1: float, y1: float, x2: float, y2: float) -> str: |
| 99 | + """Generate svg code for an edge""" |
| 100 | + return f"""<line x1="{x1}" x2="{x2}" y1="{y1}" y2="{y2}"/>""" |
| 101 | + |
| 102 | + for a in edges: |
| 103 | + x1, y1, x2, y2 = a |
| 104 | + svg += svg_edge(scalex(x1, y1), scaley(y1), scalex(x2, y2), scaley(y2)) |
| 105 | + |
| 106 | + # nodes |
| 107 | + def svg_node(x: float, y: float, label: str = "") -> str: |
| 108 | + """Generate svg code for a node and his label""" |
| 109 | + return f""" |
| 110 | + <circle class="bt-node" cx="{x}" cy="{y}" r="{node_size}"/> |
| 111 | + <text class="bt-label" x="{x}" y="{y}">{label}</text>""" |
| 112 | + |
| 113 | + for n in nodes: |
| 114 | + x, y, label = n |
| 115 | + svg += svg_node(scalex(x, y), scaley(y), str(label)) |
| 116 | + |
| 117 | + svg += "</g></svg>" |
| 118 | + return svg |
0 commit comments