Skip to content

Instantly share code, notes, and snippets.

@danoneata
Created September 16, 2023 21:13
Show Gist options
  • Save danoneata/9ebdee1caa087ee82c69bb0266fd948f to your computer and use it in GitHub Desktop.
Save danoneata/9ebdee1caa087ee82c69bb0266fd948f to your computer and use it in GitHub Desktop.
Z-order in chalk (issue 124)
from chalk.arrow import connect_outside
from chalk import *
from colour import Color
dx = 0.25
dy = 2
colors = {
"stroke": Color("black"),
"fill": Color("#c4bedf"),
}
def make_block(layer, label):
block = (
rectangle(2, 1, 0.1)
.line_color(colors["stroke"])
.fill_color(colors["fill"])
.line_width(0.25)
)
block = block + text("{} · {}".format(layer, label), 0.5)
return block.named("{} {}".format(layer, label))
def make_blocks(layer):
return concat([make_block(layer, i).translate(i * dx, i * dx) for i in range(3)])
def make_blocks_vertical(i):
dia = make_block("top", i) // vstrut(dy) // make_block("bot", i)
return connect_outside(dia, f"bot {i}", f"top {i}")
def connect_outside_reverse(*args, **kwargs):
dia = connect_outside(*args, **kwargs)
return dia.diagram2 + dia.diagram1
def show_desired():
dia = concat([make_blocks_vertical(i).translate(i * dx, i * dx) for i in range(3)])
dia.render_svg("/tmp/124-desired.svg", height=256)
def show_original():
blocks_top = make_blocks("top")
blocks_bot = make_blocks("bot")
dia = blocks_top // vstrut(dy) // blocks_bot
for i in range(3):
dia = connect_outside(dia, f"bot {i}", f"top {i}")
dia.render_svg("/tmp/124-original.svg", height=256)
def show_reverse():
blocks_top = make_blocks("top")
blocks_bot = make_blocks("bot")
dia = blocks_top // vstrut(dy) // blocks_bot
for i in range(3):
dia = connect_outside_reverse(dia, f"bot {i}", f"top {i}")
dia.render_svg("/tmp/124-reverse.svg", height=256)
if __name__ == "__main__":
show_desired()
show_original()
show_reverse()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment