def test_turtle(): # # [<<< table of contents](index.html) # # --- # # Turtle graphics # =============== # Use a turtle to keep track of a path as you build it. from huygens import canvas, style, color from huygens.turtle import Turtle cvs = canvas.canvas() turtle = Turtle(cvs=cvs) n = 8 angle = 360. / n R = 3.0 for i in range(n): turtle.fwd(1. * R) turtle.left((1. / 3) * angle) turtle.back(0.5 * R) turtle.left((1. / 3) * angle) turtle.back(0.7 * R) turtle.left((1. / 3) * angle) turtle.stroke() cvs.writeSVGfile("output.svg") yield cvs # We can change the stroke style that the turtle uses # to a thick green line. attrs = [style.linewidth.THIck, color.rgb(0.2, 0.6, 0.2)] cvs = canvas.canvas() turtle = Turtle(cvs=cvs, attrs=attrs) turtle.fwd(2.) turtle.right(300, 1.) turtle.fwd(2.) turtle.arrow(0.4) turtle.stroke() yield cvs cvs = canvas.canvas() turtle = Turtle(cvs=cvs, attrs=attrs) R = 1.0 for i in range(24 * 2): turtle.left(320, 0.6 * R) turtle.left(-60, 0.3 * R) turtle.right(90, 0.6 * R) turtle.stroke(attrs=attrs) yield cvs
def XXXtest_braid(): from random import shuffle, seed, randint from operator import matmul from functools import reduce seed(1) scale = 0.5 w = 1.4 * scale h = 1.8 * scale Id = lambda: VWire(min_height=0.5, min_width=0.5) Swap = lambda inverse: Braid( inverse=inverse, min_width=w, min_height=h, space=0.7) box = None m, n = 4, 4 k = 2 * m + n for count in range(6): items = [Swap(randint(0, 1)) for k in range(m)] + [Id() for k in range(n)] shuffle(items) row = reduce(matmul, items) if box is None: box = row else: box = box * row #box = Id() @ box lhs = reduce(matmul, [Id() for i in range(k)]) box = lhs @ box rels = [(i, 2 * k - i - 1) for i in range(k)] #rels = [(i, i+k) for i in range(k)] rel = Relation(0, 2 * k, botbot=rels, weight=200.0) box = rel * box rels = [(i, 2 * k - i - 1) for i in range(k)] rel = Relation(2 * k, 0, toptop=rels, weight=200.0) box = box * rel #rect = RectBox(box, bg=color.rgb(0.9, 0.9, 0.3, 0.6)) Box.DEBUG = False cvs = canvas.canvas() #cvs.append(trafo.rotate(pi/2)) system = box.layout(cvs) def rectbox(box): x = system[box.llx] y = system[box.lly] width = system[box.width] height = system[box.height] return x, y, width, height def fillbox(box, st): rect = rectbox(box) p = path.rect(*rect) cvs.fill(p, st) #fillbox(box, [color.rgb(0.9, 0.8, 0.5)]) sub = box[0][1][1] x = 0.5 * (system[box.llx] + system[box.urx]) y = system[sub.lly] width = system[box.urx] - x height = system[sub.ury] - y p = path.rect(x, y, width, height) cvs.fill(p, [color.rgb(0.9, 0.9, 0.6)]) cvs.stroke(p, [style.linewidth.thick]) cvs.append(style.linewidth.THICk) #cvs.append(color.rgb(0.2,0.5,0.2)) box.render(cvs) cvs.append(style.linewidth.thick) cvs.append(color.rgb(0.9, 0.9, 0.9)) box.render(cvs) #cvs.writePDFfile("test_diagram.pdf") yield cvs
def test_braid(): from random import shuffle, seed, randint from operator import matmul from functools import reduce from huygens import canvas, color, style, path from huygens.box import Box, HBox from huygens.diagram import VWire, Braid, Relation seed(1) scale = 0.3 w = 1.4 * scale h = 1.8 * scale Id = lambda: VWire(min_height=scale, min_width=scale) Swap = lambda inverse: Braid( inverse=inverse, min_width=w, min_height=h, space=0.5) box = None m, n = 3, 3 k = 2 * m + n for count in range(3): items = [Swap(randint(0, 1)) for k in range(m)] + [Id() for k in range(n)] shuffle(items) row = reduce(matmul, items) if box is None: box = row else: box = box * row # This is what the `box` looks like now: yield box, "braid-strands" # Now we take the closure of this braid: #box = Id() @ box lhs = reduce(matmul, [Id() for i in range(k)]) box = lhs @ box rels = [(i, 2 * k - i - 1) for i in range(k)] #rels = [(i, i+k) for i in range(k)] rel = Relation(0, 2 * k, botbot=rels, weight=200.0) box = rel * box rels = [(i, 2 * k - i - 1) for i in range(k)] rel = Relation(2 * k, 0, toptop=rels, weight=200.0) box = box * rel yield box # Draw this now with some more fancy tricks. cvs = canvas.canvas() system = box.layout(cvs) sub = box[0][1][1] x = 0.5 * (box.llx + box.urx) y = sub.lly width = box.urx - x height = sub.ury - y p = path.rect(x, y, width, height) cvs.fill(p, [color.rgb(0.9, 0.9, 0.6)]) cvs.stroke(p, [style.linewidth.thick]) system.refresh() # refresh for a new render cvs.append(style.linewidth.THICk) #cvs.append(color.rgb(0.2,0.5,0.2)) box.render(cvs) cvs.append(style.linewidth.thick) cvs.append(color.rgb(0.9, 0.0, 0.0)) box.render(cvs) #cvs.writePDFfile("test_diagram.pdf") yield cvs