def test_rotation(self): t = Turtle() self.assertEqual(t.rotation, 0) t.right(30) self.assertEqual(t.rotation, 30) t.left(30) self.assertEqual(t.rotation, 0)
def test_position(self): t = Turtle() self.assertEqual(t.pos_x, 0) self.assertEqual(t.pos_y, 0) t.move(1, 1) self.assertEqual(t.pos_x, 1) self.assertEqual(t.pos_y, 1)
def lsystem2drawille(lsystem, n, size, initial_rotation): def restore(): pos, angl = q.pop() t.up() t.move(pos[0], pos[1]) t.rotation = angl t.down() def move(pen): if not pen: t.up() t.fd(size) if not pen: t.down() q = [] methods = { '-': lambda: t.left(lsystem.angle), '+': lambda: t.right(lsystem.angle), '[': lambda: q.append(([t.pos_x, t.pos_y], t.rotation)), ']': restore, } for v in lsystem.ignore: methods[v] = lambda: None t = Turtle() t.rotation = initial_rotation for c in lsystem.iterate(n): try: methods[c]() except KeyError: move(c.isupper()) pass return t.frame()
def test_brush(self): t = Turtle() self.assertFalse(t.get(t.pos_x, t.pos_y)) t.forward(1) self.assertTrue(t.get(0, 0)) self.assertTrue(t.get(t.pos_x, t.pos_y)) t.up() t.move(2, 0) self.assertFalse(t.get(t.pos_x, t.pos_y)) t.down() t.move(3, 0) self.assertTrue(t.get(t.pos_x, t.pos_y))
#!/usr/bin/env python # -*- coding: utf-8 -*- from drawille import Turtle t = Turtle() for _ in range(36): t.right(10) for _ in range(36): t.right(10) t.forward(8) print(t.frame())
def goldenratio2drawille(goldenratio, n, s, angle): def square(size): for i in range(4): t.forward(size) t.right(goldenratio.angle) # Taken from python turtle library def circle(radius, extent=None, steps=None): fullCircle = 360 if extent is None: extent = fullCircle if steps is None: frac = abs(extent) / fullCircle steps = 1+int(min(11+abs(radius) / 6.0, 59.0)*frac) w = 1.0 * extent / steps w2 = 0.5 * w l = 2.0 * radius * sin(w2*pi/180.0) if radius < 0: l, w, w2 = -l, -w, -w2 t.right(w2) for i in range(steps): t.forward(l) t.right(w) t.left(w2) t = Turtle() size = getTerminalSize()[0] + s t.rotation = goldenratio.angle + angle for i in range(n): square(size) t.forward(size) t.right(goldenratio.angle) t.forward(size) size /= goldenratio.phi t.up() t.move(0, 0) t.down() size = getTerminalSize()[0] + s t.rotation = goldenratio.angle + angle for i in range(n): circle(size, 90) size /= goldenratio.phi return t.frame()
def drawille_frame(data: List[Ohlc], *, scale=4.0, offset=0.0): t = Turtle() scaled = (o.transform(scale, offset) for o in data) x = 0 for o, h, l, c in scaled: x += 1 top = max(o, c) bot = min(o, c) t.up() t.move(x, bot) t.down() # draw box t.move(x, top) t.move(x + 2, top) t.move(x + 2, bot) t.move(x, bot) # draw spikes t.up() t.move(x + 1, l) t.down() if o <= c: t.move(x + 1, bot) t.up() # draw lower bullish spike t.move(x + 1, top) t.down() # move to upper bullish spike t.move(x + 1, h) # draw uppper bullish or full bearish spike t.up() x += 3 return t.frame()
def test_brush(self): turtle = Turtle() self.assertFalse(turtle.get(turtle.pos_x, turtle.pos_y)) turtle.forward(1) self.assertTrue(turtle.get(0, 0)) self.assertTrue(turtle.get(turtle.pos_x, turtle.pos_y)) turtle.up() turtle.move(2, 0) self.assertFalse(turtle.get(turtle.pos_x, turtle.pos_y)) turtle.down() turtle.move(3, 0) self.assertTrue(turtle.get(turtle.pos_x, turtle.pos_y))