Connections
In [1]:
Copied!
from colour import Color
from chalk.core import BaseDiagram
from chalk import *
def help(f):
import pydoc
from IPython.display import HTML
return HTML(pydoc.HTMLDoc().docroutine(f))
set_svg_height(100)
from colour import Color from chalk.core import BaseDiagram from chalk import * def help(f): import pydoc from IPython.display import HTML return HTML(pydoc.HTMLDoc().docroutine(f)) set_svg_height(100)
Chalk supports basic methods for complex connected layouts and diagrams. Individual elements can be assigned names, and then be referenced in their subdiagram locations. As we will see, names are particularly useful for connecting different parts of the diagram with arrows. Our implementation follows the API of the Haskell diagrams library, but named nodes are also common in TikZ.
Diagram.named¶
In [2]:
Copied!
help(BaseDiagram.named)
help(BaseDiagram.named)
Out[2]:
- named(self, name: 'Union[str, Tuple[str]]') -> 'Diagram'
- Add a name to a diagram.
Args:
name (Union[str, Tuple[str]]): Diagram name.
Returns:
Diagram: A diagram object.
In [3]:
Copied!
diagram = circle(0.5).named("x") | square(1)
diagram
diagram = circle(0.5).named("x") | square(1) diagram
Out[3]:
Diagram.get_subdiagram¶
In [4]:
Copied!
help(BaseDiagram.get_subdiagram)
help(BaseDiagram.get_subdiagram)
Out[4]:
- get_subdiagram(self: 'Diagram', name: 'Union[str, Name]', t: 'Affine' = Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0)) -> 'Optional[Subdiagram]'
A Subdiagram
is a Diagram
paired with its enclosing context (a Transformation
for the moment; but Style
should also be added at some point). It has the following methods:
get_envelope
, which returns the correspondingEnvelope
get_trace
, which returns the correspondingTrace
get_location
, which returns the local origin of theSubdiagram
boundary_from
, which return the furthest point on the boundary of theSubdiagram
, starting from the local origin of theSubdigram
and going in the direction of a given vector.
In [5]:
Copied!
diagram = circle(0.5).named("x") | square(1)
sub = diagram.get_subdiagram("x")
diagram + circle(0.2).translate_by(sub.get_location())
diagram = circle(0.5).named("x") | square(1) sub = diagram.get_subdiagram("x") diagram + circle(0.2).translate_by(sub.get_location())
Out[5]:
Diagram.with_names¶
In [6]:
Copied!
help(BaseDiagram.with_names)
help(BaseDiagram.with_names)
Out[6]:
- with_names(self: 'Diagram', names: 'List[Name]', f: 'Callable[[List[Subdiagram], Diagram], Diagram]') -> 'Diagram'
In [7]:
Copied!
root = circle(1).named("root")
leaves = hcat([circle(1).named(c) for c in "abcde"], sep=0.5).center()
root = circle(1).named("root") leaves = hcat([circle(1).named(c) for c in "abcde"], sep=0.5).center()
In [8]:
Copied!
def connect(subs, nodes):
root, leaf = subs
pp = tuple(root.boundary_from(unit_y))
pc = tuple(leaf.boundary_from(-unit_y))
return nodes + make_path([pp, pc])
def connect(subs, nodes): root, leaf = subs pp = tuple(root.boundary_from(unit_y)) pc = tuple(leaf.boundary_from(-unit_y)) return nodes + make_path([pp, pc])
In [9]:
Copied!
nodes = root / vstrut(2) / leaves
nodes = root / vstrut(2) / leaves
In [10]:
Copied!
for c in "abcde":
nodes = nodes.with_names(["root", c], connect)
nodes
for c in "abcde": nodes = nodes.with_names(["root", c], connect) nodes
Out[10]:
Diagram.qualify¶
In [11]:
Copied!
help(BaseDiagram.qualify)
help(BaseDiagram.qualify)
Out[11]:
- qualify(self, name: 'Union[str, Name]') -> 'Diagram'
- Prefix names in the diagrame by the given `name`, which can be
either a string or a list of strings.
In [12]:
Copied!
red = Color("red")
red = Color("red")
In [13]:
Copied!
def attach(subs, dia):
sub1, sub2 = subs
p1 = tuple(sub1.get_location())
p2 = tuple(sub2.get_location())
return dia + make_path([p1, p2]).line_color(red)
def attach(subs, dia): sub1, sub2 = subs p1 = tuple(sub1.get_location()) p2 = tuple(sub2.get_location()) return dia + make_path([p1, p2]).line_color(red)
In [14]:
Copied!
def squares():
s = square(1)
return (
(s.named("NW") | s.named("NE")) /
(s.named("SW") | s.named("SE")))
def squares(): s = square(1) return ( (s.named("NW") | s.named("NE")) / (s.named("SW") | s.named("SE")))
In [15]:
Copied!
dia = hcat([squares().qualify(str(i)) for i in range(5)], sep=0.5)
pairs = [
(("0", "NE"), ("2", "SW")),
(("1", "SE"), ("4", "NE")),
(("3", "NW"), ("3", "SE")),
(("0", "SE"), ("1", "NW")),
]
dia = hcat([squares().qualify(str(i)) for i in range(5)], sep=0.5) pairs = [ (("0", "NE"), ("2", "SW")), (("1", "SE"), ("4", "NE")), (("3", "NW"), ("3", "SE")), (("0", "SE"), ("1", "NW")), ]
In [16]:
Copied!
dia
dia
Out[16]:
In [17]:
Copied!
for pair in pairs:
dia = dia.with_names(pair, attach)
for pair in pairs: dia = dia.with_names(pair, attach)
In [18]:
Copied!
dia
dia
Out[18]:
Diagram.show_labels¶
In [19]:
Copied!
help(BaseDiagram.show_labels)
help(BaseDiagram.show_labels)
Out[19]:
- show_labels(self: chalk.types.Diagram, font_size: int = 1) -> chalk.types.Diagram
- Shows the labels of all named subdiagrams of a diagram at their
corresponding origin.
In [20]:
Copied!
dia.show_labels(font_size=0.2)
dia.show_labels(font_size=0.2)
Out[20]:
Diagram.connect¶
In [21]:
Copied!
help(BaseDiagram.connect)
help(BaseDiagram.connect)
Out[21]:
- connect(self: chalk.types.Diagram, name1: str, name2: str, style: chalk.arrow.ArrowOpts = ArrowOpts(head_style=Style(line_width_=None, line_color_=None, fill_color_=None, fill_opacity_=None, dashing_=None, output_size=None), head_pad=0.0, tail_pad=0.0, head_arrow=None, shaft_style=Style(line_width_=None, line_color_=None, fill_color_=None, fill_opacity_=None, dashing_=None, output_size=None), trail=None, arc_height=0.0)) -> chalk.types.Diagram
In [22]:
Copied!
diagram = circle(0.5).named("x") | hstrut(1) | square(1).named("y")
diagram.connect("x", "y")
diagram = circle(0.5).named("x") | hstrut(1) | square(1).named("y") diagram.connect("x", "y")
Out[22]:
Diagram.connect_outside¶
In [23]:
Copied!
help(BaseDiagram.connect_outside)
help(BaseDiagram.connect_outside)
Out[23]:
- connect_outside(self: chalk.types.Diagram, name1: str, name2: str, style: chalk.arrow.ArrowOpts = ArrowOpts(head_style=Style(line_width_=None, line_color_=None, fill_color_=None, fill_opacity_=None, dashing_=None, output_size=None), head_pad=0.0, tail_pad=0.0, head_arrow=None, shaft_style=Style(line_width_=None, line_color_=None, fill_color_=None, fill_opacity_=None, dashing_=None, output_size=None), trail=None, arc_height=0.0)) -> chalk.types.Diagram
In [24]:
Copied!
diagram = circle(0.5).named("x") | hstrut(1) | square(1).named("y")
diagram.connect_outside("x", "y")
diagram = circle(0.5).named("x") | hstrut(1) | square(1).named("y") diagram.connect_outside("x", "y")
Out[24]:
Last update: 2022-10-11