def CZGate(c=P(0, 0), controlDist=1.0, direction="up", side=0.5):
    """
    Controlled Z gate

    @param controlDist: distance to the control
    @type controlDist: float

    @param direction: in which direction is the control?  up/down
    @type direction: string

    @param side: length of the box side
    @type side: float
    """
    if direction is "up":
        return Group(
            Circle(c=c + P(0, controlDist), r=0.065, bg=Color("black")),
            Path(c + P(0, side / 2.), c + P(0, controlDist)),
            Rectangle(width=side, height=side, c=c, bg=Color("white")),
            TeX(r'Z', c=c))
    elif direction is "down":
        return Group(
            Circle(c=c - P(0, controlDist), r=0.65, bg=Color("black")),
            Path(c - P(0, side / 2.), c - P(0, controlDist)),
            Rectangle(width=side, height=side, c=c, bg=Color("white")),
            TeX(r'Z', c=c))
Exemple #2
0
    def __init__(self, obj, **options):
        Rectangle.__init__(self, **options)
        Group.__init__(self, **options)

        bbox = obj.bbox()

        self.object = obj

        w = bbox.width + 2 * self.pad
        h = bbox.height + 2 * self.pad

        # overide the width and height if supplied
        if self.width is None:
            self.width = options.get('width', w)
        if self.height is None:
            self.height = options.get('height', h)

        self.append(
            Rectangle(width=self.width,
                      height=self.height,
                      bg=self.bg,
                      fg=self.fg,
                      c=obj.c,
                      r=self.r,
                      linewidth=self.linewidth,
                      dash=self.dash),
            obj,
        )
def ZGate(c=P(0, 0), side=0.5):
    """
    Z gate

    @param side: length of the box side
    @type side: float
    """
    return Group(Rectangle(width=side, height=side, c=c, bg=Color("white")),
                 TeX(r'Z', c=c))
Exemple #4
0
 def test_get_postscript(self):
     code = Rectangle(40, 80)._get_postscript(Point(100, 100))
     self.assertEqual(
         code, "newpath\n"
         "80.0 60.0 moveto\n"
         "40 0 rlineto\n"
         "0 80 rlineto\n"
         "-40 0 rlineto\n"
         "closepath\n"
         "stroke\n")
Exemple #5
0
 def test_export_postscript_circles_and_rectangles(self):
     self._test_export_postscript(
         HorizontalShapes(Circle(10), Rectangle(20, 20), Circle(20),
                          Rectangle(40, 40), Circle(30), Rectangle(120, 60),
                          Circle(20), Rectangle(80, 40), Circle(10),
                          Rectangle(40, 20), Rectangle(20, 40)),
         Point(300, 200))
Exemple #6
0
    def __init__(self, obj, **options):

        Rectangle.__init__(self, **options)
        Group.__init__(self, **options)

        bbox = obj.bbox()

        w = bbox.width + 2 * self.pad
        h = bbox.height + 2 * self.pad

        self.width = options.get('width', w)
        self.height = options.get('height', h)

        self.append(
            Rectangle(width=self.width,
                      height=self.height,
                      bg=self.bg,
                      fg=self.fg,
                      c=obj.c,
                      r=self.r,
                      linewidth=self.linewidth,
                      dash=self.dash),
            obj,
        )
Exemple #7
0
    def background(self):
        '''
        Return background for poster
        '''
        area = self.area()

        signature = Text(
            'Created with PyScript.  http://pyscript.sourceforge.net',
            size=14,
            fg=Color(1))

        signature.se = area.se + P(-.5, .5)
        return Group(
            Rectangle(width=area.width,
                      height=area.height,
                      fg=None,
                      bg=self.bg),
            signature,
        )
    def __init__(self, **options):
        # intitialise base class
        Group.__init__(self, **options)

        self.length = 3.0
        self.width = 1.0
        self.angle = 0.0
        self.pinLength = 0.5
        self.fg = Color(0)
        self.bg = Color(1)

        # process the options if any
        self.length = options.get("length", self.length)
        self.width = options.get("width", self.width)
        self.angle = options.get("angle", self.angle)
        self.pinLength = options.get("pinLength", self.pinLength)
        self.fg = options.get("fg", self.fg)
        self.bg = options.get("bg", self.bg)

        pinIn = Group(
                Path( 
                    P(0, 0), 
                    P(self.pinLength, 0)
                    )
                )
        resistor = Rectangle(w=pinIn.e, width=self.length, height=self.width)
        pinOut = Path(
                resistor.e, 
                resistor.e+P(self.pinLength, 0))

        # collect the objects together
        obj = Group(pinIn, pinOut, resistor)

        # apply the colours
        obj.apply(fg=self.fg, bg=self.bg)

        # rotate if necessary
        if self.angle != 0.0:
            obj.rotate(self.angle, p=obj.c)

        # return object to myself
        self.append(obj)
Exemple #9
0
    def __init__(self, **args):
        Group.__init__(self, **args)

        h = self.height
        w = self.width

        self.append(Rectangle(width=1.8 * h, height=h, bg=self.bg))

        p = Path(P(.1, .1),
                 C(0, 0),
                 P(w - .1, .1),
                 P(w - .2, .1),
                 C(0, 0),
                 P(.2, .1),
                 closed=1,
                 bg=self.mcolor,
                 fg=None)

        self.append(
            p,
            Path(P(w / 2., .1), U(self.angle, h * .9)),
        )
Exemple #10
0
class AllShapesTestCase(ShapeTestCase):

    _base_circle = Circle(80)
    _rectangle = Rectangle(100, 60)
    _spacer = Spacer(40, 40)
    _square = Square(80)
    _vertical_shapes = VerticalShapes(
        _base_circle,
        LayeredShapes(ScaledShape(_base_circle, 0.75, 0.75), Polygon(5, 20)),
        LayeredShapes(ScaledShape(_base_circle, 0.5, 0.5),
                      RotatedShape(Triangle(20), 180)))
    _shape = HorizontalShapes(_rectangle, _spacer, _square, _vertical_shapes,
                              _square, _spacer, _rectangle)

    def test_get_width(self):
        self.assertEqual(self._shape._get_width(), 600)

    def test_get_height(self):
        self.assertEqual(self._shape._get_height(), 360)

    def test_export_postscript_all_shapes(self):
        self._test_export_postscript(self._shape, Point(305, 300))
    def __init__(self, obj, **options):
        bbox = obj.bbox()

        pad = .1
        w = bbox.width + 2 * pad
        h = bbox.height + 2 * pad

        self.width = w
        self.height = h
        self.bg = options.get('bg', Color(1))
        if options.has_key('bg'):
            del options['bg']

        apply(Group.__init__, (self, ), options)
        apply(Area.__init__, (self, ), options)

        obj.c = P(w / 2., h / 2.)

        self.append(
            Rectangle(width=w, height=h, bg=self.bg),
            obj,
        )
Exemple #12
0
 def test_get_width_1(self):
     rectangle = Rectangle(1, 5)
     self.assertEqual(rectangle._get_width(), 1)
Exemple #13
0
 def test_get_width_0(self):
     rectangle = ScaledShape(Rectangle(0, 5), 2, 3)
     self.assertEqual(rectangle._get_width(), 0 * 2)
Exemple #14
0
 def test_get_height_37(self):
     rectangle = ScaledShape(Rectangle(5, 37), 2, 3)
     self.assertEqual(rectangle._get_height(), 37 * 3)
Exemple #15
0
 def test_get_height_1(self):
     rectangle = ScaledShape(Rectangle(5, 1), 2, 3)
     self.assertEqual(rectangle._get_height(), 1 * 3)
Exemple #16
0
 def test_get_height_1(self):
     rectangle = Rectangle(5, 1)
     self.assertEqual(rectangle._get_height(), 1)
Exemple #17
0
 def test_get_height_270(self):
     shape = RotatedShape(Rectangle(20, 30), 270)
     self.assertEqual(shape._get_height(), 20)
Exemple #18
0
 def test_get_width_37(self):
     rectangle = Rectangle(37, 5)
     self.assertEqual(rectangle._get_width(), 37)
Exemple #19
0
 def test_value_error_720(self):
     with self.assertRaises(ValueError):
         RotatedShape(Rectangle(10, 20), 720)
Exemple #20
0
 def test_value_error_negative(self):
     with self.assertRaises(ValueError):
         RotatedShape(Rectangle(10, 20), -90)
Exemple #21
0
 def test_create_270(self):
     RotatedShape(Rectangle(10, 20), 270)
Exemple #22
0
 def test_create_180(self):
     RotatedShape(Rectangle(10, 20), 180)
Exemple #23
0
 def test_get_width_0(self):
     rectangle = Rectangle(0, 5)
     self.assertEqual(rectangle._get_width(), 0)
Exemple #24
0
 def test_create_90(self):
     RotatedShape(Rectangle(10, 20), 90)
Exemple #25
0
 def test_get_height_multiple_shapes(self):
     horizontal_shapes = VerticalShapes(Circle(1), Rectangle(5, 10),
                                        Circle(21), Rectangle(0, 1),
                                        Rectangle(3, 9))
     self.assertEqual(horizontal_shapes._get_height(), 2 + 10 + 42 + 1 + 9)
Exemple #26
0
#!/usr/bin/env python3

# Laura originally created this shape for use in an automated test, but it also
# serves as a nice demonstration of the pyscript module.

from pyscript import (
    Point, Rectangle, Spacer, Square, Circle, HorizontalShapes, VerticalShapes,
    LayeredShapes, ScaledShape, RotatedShape, Triangle, Polygon
)

if __name__ == "__main__":
    base_circle = Circle(80)
    rectangle = Rectangle(100, 60)
    spacer = Spacer(40, 40)
    square = Square(80)
    vertical_shapes = VerticalShapes(
        base_circle,
        LayeredShapes(
            ScaledShape(base_circle, 0.75, 0.75), Polygon(5, 20)
        ),
        LayeredShapes(
            ScaledShape(base_circle, 0.5, 0.5), RotatedShape(Triangle(20), 180)
        )
    )
    shape = HorizontalShapes(
        rectangle,
        spacer,
        square,
        vertical_shapes,
        square,
        spacer,
Exemple #27
0
 def test_get_width_multiple_shapes(self):
     horizontal_shapes = VerticalShapes(Circle(1), Rectangle(5, 10),
                                        Circle(21), Rectangle(0, 1),
                                        Rectangle(3, 9))
     self.assertEqual(horizontal_shapes._get_width(), 42)
Exemple #28
0
 def test_get_width_37(self):
     rectangle = ScaledShape(Rectangle(37, 5), 2, 3)
     self.assertEqual(rectangle._get_width(), 37 * 2)
Exemple #29
0
 def test_get_width_270(self):
     shape = RotatedShape(Rectangle(20, 30), 270)
     self.assertEqual(shape._get_width(), 30)
Exemple #30
0
 def test_get_height_37(self):
     rectangle = Rectangle(5, 37)
     self.assertEqual(rectangle._get_height(), 37)