Esempio n. 1
0
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
Esempio n. 2
0
def test_canvas():

    #
    # [<<< table of contents](index.html)
    #
    #  ---
    #
    # Canvas
    # ======
    #
    # The canvas is a front-end drawing API
    # modelled after the amazing
    # [PyX](https://pyx-project.org/) package.
    #
    # The canvas coordinates are positive in the
    # upper right quadrant.

    from huygens import canvas, path, color

    cvs = canvas.canvas()
    cvs.stroke(path.line(0., 0., 3., 2.))
    cvs.fill(path.circle(3., 2., 0.2), [color.rgb.red])
    cvs.writeSVGfile("output.svg")

    # This produces the following SVG image:

    yield cvs

    # The canvas also has a `writePDFfile` method.
    #
    # Unlike PyX, angles are specified in
    # radians in calls to `path.arc` and `trafo.rotate`.

    from math import pi
    from huygens import style, trafo, linestyle

    cvs = canvas.canvas()

    cvs.stroke(path.circle(0., 0., 1.),
               [style.linewidth.thick, color.rgb.blue, linestyle.dashed])
    cvs.text(0., 0., "hey there!", [trafo.rotate(0.5 * pi)])

    yield cvs

    # Composite paths are built using the `path.path` constructor:

    cvs = canvas.canvas()
    p = path.path([
        path.moveto(0., 0.),
        path.arc(0., 0., 1., 0., 0.5 * pi),
        path.lineto(-1., 1.),
        path.arc(-1., 0., 1., 0.5 * pi, 1.0 * pi),
        path.arc(-1.5, 0., 0.5, 1.0 * pi, 2.0 * pi),
        path.closepath()
    ])

    cvs.fill(p, [color.rgb.red, trafo.scale(1.2, 1.2)])
    cvs.stroke(p, [color.rgb.black, style.linewidth.THick])

    yield cvs
Esempio n. 3
0
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
Esempio n. 4
0
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