TreeObserver

class foronoi.TreeObserver(visualize_steps=True, visualize_result=True, text_based=False, callback=None)

Observers the state of the status tree (foronoi.algorithm.Algorithm.status_tree) and visualizes the result using GraphViz.

Parameters
  • visualize_steps (bool) – Visualize all individual steps

  • visualize_result (bool) – Visualize the final result

  • text_based (bool) – Visualize the tree using plain text instead of GraphViz

  • callback (function) – By default, the TreeObserver renders and shows the result in a window, or prints the result when text_based is true. When a callback function is given, either the GraphViz diagram or the text-string is passed to the callback.

Examples

>>> from foronoi import Voronoi, TreeObserver, Polygon
>>> points = [
...    (2.5, 2.5), (4, 7.5), (7.5, 2.5), (6, 7.5), (4, 4), (3, 3), (6, 3)
... ]
>>> poly = Polygon(
...    [(2.5, 10), (5, 10), (10, 5), (10, 2.5), (5, 0), (2.5, 0), (0, 2.5), (0, 5)]
... )
>>> v = Voronoi(poly)
>>>
>>> # Define callback
>>> def callback(observer, dot):
...    dot.render(f"output/tree/{observer.n_messages:02d}")
>>>
>>> # Attach observer
>>> v.attach_observer(TreeObserver(callback=callback))
>>>
>>> # Start diagram creation
>>> v.create_diagram(points)
update(subject: foronoi.algorithm.Algorithm, message: foronoi.observers.message.Message, **kwargs)

Send the updated state of the algorithm to the TreeObserver.

Parameters
  • subject (Algorithm) – The algorithm to observe

  • message (Message) – The message type