Exemple #1
0
def main():
    """Create the Sierpinski triangle

    https://en.wikipedia.org/wiki/Sierpinski_triangle
    """
    axiom = 'F-G-G'
    rules = {
        'F': 'F-G+F+G-F',
        'G': 'GG',
    }

    angle = 2 * np.pi / 3
    cols = 512
    rows = int(np.ceil(cols * np.sin(angle / 2)))
    n = 8

    a = np.zeros((rows, cols))
    s = l_system.grow(axiom, rules, n)

    t = Turtle(a)
    t.position = rows, 0
    t.rotate(np.pi / 2)

    for s_n in s:
        if s_n == 'F' or s_n == 'G':
            t.forward(cols / 2**n)
        elif s_n == '-':
            t.rotate(angle)
        elif s_n == '+':
            t.rotate(-angle)

    toimage(a).show()
Exemple #2
0
 def setUp(self):
     self.size = 10
     self.array = np.zeros((self.size, self.size), dtype=np.uint8)
     self.turtle = Turtle(self.array)
Exemple #3
0
def main():
    """Create a fractal plant"""
    axiom = 'X'
    rules = {
        'X': 'F+[[X]-X]-F[-FX]+X',
        'F': 'FF',
    }

    angle = np.pi / 7
    cols, rows = 512, 512
    padding = 32
    n = 6

    a = np.zeros((rows, cols, 4))
    s = l_system.grow(axiom, rules, n)

    t = Turtle(a, aa=True)
    t.position = rows - padding, padding
    t.rotate(np.pi - angle)
    t.color = (0, 1, 0, 1)

    for s_n in s:
        if s_n == 'F':
            t.forward(3)
        elif s_n == '-':
            t.rotate(-angle)
        elif s_n == '+':
            t.rotate(angle)
        elif s_n == '[':
            t.push()
        elif s_n == ']':
            t.pop()

    imsave('images/fractal_plant.png', a)
Exemple #4
0
class TestTurtleUint8(TestCase):
    def setUp(self):
        self.size = 10
        self.array = np.zeros((self.size, self.size), dtype=np.uint8)
        self.turtle = Turtle(self.array)

    def test_pos_forward(self):
        self.turtle.forward(1)
        self.turtle.forward(1)
        self.assertTupleEqual(self.turtle.position, (2, 0))

    def test_pos_forward_out_of_bounds(self):
        self.turtle.forward(10)
        self.turtle.forward(10)
        self.assertTupleEqual(self.turtle.position, (20, 0))

    def test_full_square(self):
        for _ in range(4):
            self.turtle.forward(10)
            self.turtle.rotate(0.5 * np.pi)

        full_square = np.zeros(self.array.shape, self.array.dtype)
        full_square[0:self.size, 0] = self.turtle.color
        full_square[0:self.size, self.size - 1] = self.turtle.color
        full_square[0, 0:self.size] = self.turtle.color
        full_square[self.size - 1, 0:self.size] = self.turtle.color

        npt.assert_array_equal(self.array, full_square)

    def test_eye(self):
        self.turtle.rotate(0.25 * np.pi)
        self.turtle.forward(np.sqrt(2 * 10**2))

        eye = np.eye(self.size, dtype=self.array.dtype) * self.turtle.color
        npt.assert_array_equal(self.array, eye)
Exemple #5
0
def main():
    """Create a fractal plant"""
    axiom = 'X'
    rules = {
        'X': 'F+[[X]-X]-F[-FX]+X',
        'F': 'FF',
    }

    angle = np.pi / 7
    cols, rows = 512, 512
    padding = 32
    n = 6

    a = np.zeros((rows, cols))
    s = l_system.grow(axiom, rules, n)

    t = Turtle(a)
    t.position = rows - padding, padding
    t.rotate(np.pi - angle)

    for s_n in s:
        if s_n == 'F':
            t.forward(3)
        elif s_n == '-':
            t.rotate(-angle)
        elif s_n == '+':
            t.rotate(angle)
        elif s_n == '[':
            t.push()
        elif s_n == ']':
            t.pop()

    toimage(a).show()