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))
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))
def __init__(self, **options): # initialise the base class Gate.__init__(self, **options) # process the options if any self.height = options.get("height", self.height) 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) # now draw the gate buff = 0.0 pinEdgeDist = 0.1*self.height pl = self.pinLength bodyHeight = self.height bodyWidth = self.width - 2.0*pl rad = 0.1 gateBody = Group( Path( P(pl, buff+0), P(pl, buff+bodyHeight), P(pl+bodyWidth/2., buff+bodyHeight)), Circle(c=P(pl+bodyWidth/2., buff+bodyHeight/2.), r=bodyHeight/2., start=0, end=180), Path( P(pl+bodyWidth/2., buff+0), P(pl, buff+0))) gatePinIn1 = Path( P(0, bodyHeight-pinEdgeDist), P(pl, bodyHeight-pinEdgeDist)) gatePinIn2 = Path( P(0, pinEdgeDist), P(pl, pinEdgeDist)) gatePinOut = Group( Circle(c=P(bodyWidth+pl+rad, bodyHeight/2.), r=rad), Path( P(bodyWidth+pl+2.*rad, bodyHeight/2.), P(bodyWidth+2.*rad+2.*pl, bodyHeight/2.))) # collect the objects together obj = Group(gateBody, gatePinIn1, gatePinIn2, gatePinOut) # 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) # now set the object to myself self.append(obj)
def Detector(e=P(0, 0), height=1.0, label=None): """ Detector @param height: height of detector @type height: float @param label: detector label @type label: string """ if label is not None: return Group(Path(e - P(0, height / 2.0), e + P(0, height / 2.0)), Circle(c=e, r=height / 2.0, start=0, end=180), label) else: return Group(Path(e - P(0, height / 2.0), e + P(0, height / 2.0)), Circle(c=e, r=height / 2.0, start=0, end=180))
def NOT(**options): """ NOT gate """ r = .2 return Gate( Group(Circle(r=r), Path(P(0, r), P(0, -r)), Path(P(-r, 0), P(r, 0))), **options)
def Cnot(c=P(0, 0), targetDist=1.0, direction="up"): """ Controlled NOT gate @param targetDist: distance to the target rail @type targetDist: float @param direction: in which direction is the target rail? up/down @type direction: string """ if direction is "up": return Group(Circle(r=0.06, bg=Color("black"), c=c), Circle(r=0.2, c=c + P(0, targetDist)), Path(c, c + P(0, targetDist + 0.2))) elif direction is "down": return Group(Circle(r=0.06, bg=Color("black"), c=c), Circle(r=0.2, c=c + P(0, -targetDist)), Path(c, c + P(0, -targetDist - 0.2)))
def __init__(self, **options): # initialise the base class Gate.__init__(self, **options) # process the options if any self.height = options.get("height", self.height) 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) # now draw the gate pl = self.pinLength pinEdgeDist = 0.1*self.height pinBackDist = -0.08*self.width bodyHeight = self.height bodyWidth = self.width - 2.0*pl rad = 0.1 gateBody = Group( Path( P(-pinBackDist, -pinEdgeDist), C(90, 225), P(1.25*bodyWidth, bodyHeight/2.0), C(-45, 90), P(-pinBackDist, bodyHeight+pinEdgeDist), C(140, 40), closed=1, ) ) gatePinIn1 = Path( P(0, bodyHeight-pinEdgeDist), P(pl, bodyHeight-pinEdgeDist)) gatePinIn2 = Path( P(0, pinEdgeDist), P(pl, pinEdgeDist)) gatePinOut = Group( Circle(w=gateBody.e, r=rad), Path( gateBody.e+P(0.2, 0), gateBody.e+P(pl+0.2, 0)), ) # collect the objects together obj = Group(gateBody, gatePinIn1, gatePinIn2, gatePinOut) # 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) # nwo set the object to myself self.append(obj)
def __init__(self, obj, **options): Circle.__init__(self, **options) Group.__init__(self, **options) bbox = obj.bbox() w = bbox.width + 2 * self.pad h = bbox.height + 2 * self.pad self.r = options.get('r', max(w, h) / 2.) self.append( Circle(r=self.r, bg=self.bg, fg=self.fg, c=obj.c, linewidth=self.linewidth, dash=self.dash), obj, )
def __init__(self, obj, **options): bbox = obj.bbox() pad = .1 r = max(bbox.width + 2 * pad, bbox.height + 2 * pad) / 2.0 self.width = 2.0 * r self.height = 2.0 * r 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(r, r) self.append( Circle(r=r, bg=self.bg, c=P(r, r)), obj, )
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 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)
def test_get_height_multiple_shapes(self): shape = LayeredShapes(Circle(1), Rectangle(5, 10), Rectangle(0, 1), Rectangle(3, 9)) self.assertEqual(shape._get_height(), 10)
def test_get_width_multiple_shapes(self): shape = LayeredShapes(Circle(1), Rectangle(5, 10), Circle(21), Rectangle(0, 1), Rectangle(3, 9)) self.assertEqual(shape._get_width(), 42)
def test_get_postscript_20_160_40(self): code = Circle(40)._get_postscript(Point(20, 160)) self.assertEqual(code, "newpath\n" "20 160 40 0 360 arc\n" "stroke\n")
def test_get_height_37(self): circle = Circle(37) self.assertEqual(circle._get_height(), 74)
def test_get_height_0(self): circle = Circle(0) self.assertEqual(circle._get_height(), 0)
def test_get_width_1(self): circle = Circle(1) self.assertEqual(circle._get_width(), 2)
def test_get_width_single_shape(self): horizontal_shapes = HorizontalShapes(Circle(3)) self.assertEqual(horizontal_shapes._get_width(), 6)
def test_export_postscript_circles_on_page(self): self._test_export_postscript( HorizontalShapes(Circle(10), Circle(20), Circle(30), Circle(20), Circle(10)), Point(90, 30))
def test_get_width_37(self): circle = Circle(37) self.assertEqual(circle._get_width(), 74)
def test_get_height_single_shape(self): horizontal_shapes = VerticalShapes(Circle(3)) self.assertEqual(horizontal_shapes._get_height(), 6)
def test_get_height_1(self): circle = Circle(1) self.assertEqual(circle._get_height(), 2)
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)
def test_get_postscript_80_80_80(self): code = Circle(80)._get_postscript(Point(80, 80)) self.assertEqual(code, "newpath\n" "80 80 80 0 360 arc\n" "stroke\n")
#!/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,
def test_get_width_0(self): circle = Circle(0) self.assertEqual(circle._get_width(), 0)
def test_get_width_single_shape(self): shape = LayeredShapes(Circle(3)) self.assertEqual(shape._get_width(), 6)