Skip to content

Instantly share code, notes, and snippets.

@vencejo
Created November 10, 2014 18:43
Show Gist options
  • Save vencejo/85b5c4f758d75ed40762 to your computer and use it in GitHub Desktop.
Save vencejo/85b5c4f758d75ed40762 to your computer and use it in GitHub Desktop.
Creando tus propios mundos fractales
{
"metadata": {
"name": "",
"signature": "sha256:1983b676901bae880e43a7f38bf49ffd9039a10ece451d8febcff3b7f37a2509"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Conectando"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#import the minecraft.py module from the minecraft directory\n",
"import mcpi.minecraft as minecraft\n",
"#import minecraft block module\n",
"import mcpi.block as block\n",
"import time \n",
"import minecraftstuff\n",
"\n",
"#connect to minecraft\n",
"mc = minecraft.Minecraft.create(\"192.168.1.37\")\n",
"mc.postToChat(\"Conectado a Minecraft\")"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Objetos Matem\u00e1ticos"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# load LSystem-en-Minecraft-/math3D.py"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"''' Programa basado en el trabajo de Daniel Bates http://www.cl.cam.ac.uk/~db434/\n",
"cuyo codigo fuente se puede ver en: http://www.cl.cam.ac.uk/~db434/files/setblockdemo.py '''\n",
"\n",
"from math import sin, cos, radians,degrees, sqrt, pow , acos\n",
"\n",
"class coordinate3d:\n",
" \"\"\"Class used to represent a point in 3D space.\"\"\"\n",
" def __init__(self,x,y,z):\n",
" self.x = x\n",
" self.y = y\n",
" self.z = z\n",
"\n",
" def __add__(self, other):\n",
" return coordinate3d(self.x+other.x, self.y+other.y, self.z+other.z)\n",
" \n",
" def __mul__(self, other):\n",
"\t #Multiplicacion por un escalar\n",
"\t return coordinate3d(self.x*other, self.y*other, self.z*other)\n",
" \n",
" def __str__(self):\n",
"\t return str([self.x, self.y, self.z])\t \n",
"\t \n",
" def modulo(self):\n",
"\t return sqrt(pow(self.x,2)+pow(self.y,2)+pow(self.z,2))\n",
"\n",
"\n",
"class transformation:\n",
" \"\"\"Representation of homogeneous matrices used to apply transformations to\n",
"coordinates - using a 4x4 matrix allows shifts as well as scales/rotations.\n",
"Transformations can be combined by multiplying them together.\"\"\"\n",
" def __init__(self, matrix):\n",
" self.matrix = matrix\n",
"\n",
" def __mul__(self, other):\n",
" if isinstance(other, transformation):\n",
" return self.compose(other)\n",
" elif isinstance(other, coordinate3d):\n",
" return self.apply(other)\n",
" else:\n",
" print \"Can't multiply transformation by {0}\".format(type(other))\n",
"\n",
" def compose(self, other):\n",
" \"\"\"Compose this transformation with another, returning a new transformation.\"\"\"\n",
" newmatrix = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]\n",
" for i in range(4):\n",
" for j in range(4):\n",
" for k in range(4):\n",
" newmatrix[i][k] += self.matrix[i][j]*other.matrix[j][k]\n",
" return transformation(newmatrix)\n",
"\n",
" def apply(self, point):\n",
" \"\"\"Apply this transformation to a coordinate, returning a new coordinate.\"\"\"\n",
" return coordinate3d(\n",
" self.matrix[0][0]*point.x + self.matrix[0][1]*point.y + self.matrix[0][2]*point.z + self.matrix[0][3],\n",
" self.matrix[1][0]*point.x + self.matrix[1][1]*point.y + self.matrix[1][2]*point.z + self.matrix[1][3],\n",
" self.matrix[2][0]*point.x + self.matrix[2][1]*point.y + self.matrix[2][2]*point.z + self.matrix[2][3])\n",
" \n",
" \n",
"## Transformation functions\n",
"\n",
"def identity():\n",
" return transformation([[1,0,0,0],\n",
" [0,1,0,0],\n",
" [0,0,1,0],\n",
" [0,0,0,1]])\n",
"\n",
"def shift(x,y,z):\n",
" \"\"\"Move by a given offset.\"\"\"\n",
" return transformation([[1,0,0,x],\n",
" [0,1,0,y],\n",
" [0,0,1,z],\n",
" [0,0,0,1]])\n",
"\n",
"def rotationx(angle):\n",
" \"\"\"Rotate about the x axis by the given number of degrees.\"\"\"\n",
" angle = radians(angle)\n",
" return transformation([[1, 0, 0, 0],\n",
" [0, cos(angle), sin(angle), 0],\n",
" [0, -sin(angle), cos(angle), 0],\n",
" [0, 0, 0, 1]])\n",
"\n",
"def rotationy(angle):\n",
" \"\"\"Rotate about the y axis by the given number of degrees.\"\"\"\n",
" angle = radians(angle)\n",
" return transformation([[ cos(angle), 0, sin(angle), 0],\n",
" [ 0, 1, 0, 0],\n",
" [-sin(angle), 0, cos(angle), 0],\n",
" [ 0, 0, 0, 1]])\n",
"\n",
"def rotationz(angle):\n",
" \"\"\"Rotate about the z axis by the given number of degrees.\"\"\"\n",
" angle = radians(angle)\n",
" return transformation([[ cos(angle), sin(angle), 0, 0],\n",
" [-sin(angle), cos(angle), 0, 0],\n",
" [ 0, 0, 1, 0],\n",
" [ 0, 0, 0, 1]])\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"La tortuga de Logo"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# load LSystem-en-Minecraft-/tortuga3D.py"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Este programa simula el lenguaje de la tortuga de logo en las 3 dimensiones de Minecraft\n",
"\n",
"mcd = minecraftstuff.MinecraftDrawing(mc) \n",
"\n",
"class Tortuga:\n",
"\t\n",
"\tdef __init__(self,paso,tipoBloque,tipoTortuga,mostrarTortuga):\n",
" \n",
"\t\tself.paso = paso\n",
"\t\tself.tipoBloque = tipoBloque\n",
"\t\tself.tipoTortuga = tipoTortuga\n",
"\t\tself.mostrarTortuga = mostrarTortuga\n",
"\t\t#Posicion inicial de la tortuga\n",
"\t\tself.posicion = coordinate3d(0,0,0)\n",
"\t\t#vector direccion de la tortuga\n",
"\t\tself.vectorDireccion = coordinate3d(0,1,0)\n",
"\t\t#Modo inicial de la tortuga \n",
"\t\tself.lapizArriba = False\n",
"\t\t#Variables para depuracion\n",
"\t\tself.pausaSegundos = 0\n",
"\t\tself.intervaloIteraciones = 0\n",
"\t\tself.contadorIteraciones = 0\n",
"\t\tself.trazando = False\n",
" \n",
" \n",
"\tdef hideturtle(self):\n",
"\t\tself.mostrarTortuga = False\n",
"\n",
"\tdef showturtle(self):\n",
"\t\tself.mostrarTortuga = True\t\n",
"\t\t\n",
"\tdef up(self):\n",
"\t\tself.lapizArriba = True\n",
"\t\n",
"\tdef down(self):\n",
"\t\tself.lapizArriba = False\n",
"\t\n",
"\tdef initDirection(self):\n",
"\t\t#vector direccion de la tortuga\n",
"\t\tself.vectorDireccion = coordinate3d(0,1,0)\n",
"\t\t\n",
"\tdef setDirection(self,x,y,z):\n",
"\t\t#vector direccion de la tortuga\n",
"\t\tself.vectorDireccion = coordinate3d(x,y,z)\n",
"\t\n",
"\tdef position(self):\n",
"\t\treturn (self.posicion.x, self.posicion.y,self.posicion.z)\n",
"\t\n",
"\tdef setheading(self,alpha, beta, gamma):\n",
"\t\t\"\"\" Pone al vector direccion mirando en la direccion marcada por los angulos \n",
"\t\talpha (con el eje x), beta (con el eje y) y gamma (con el eje z) \"\"\"\n",
"\t\t#Actualiza el vector direccion\n",
"\t\tself.setDirection(cos(radians(alpha)),cos(radians(beta)),cos(radians(gamma)))\n",
"\t\n",
"\tdef heading(self):\n",
"\t\treturn (degrees(acos(self.vectorDireccion.x)), \n",
"\t\t\t\t degrees(acos(self.vectorDireccion.y)), \n",
"\t\t\t\t degrees(acos(self.vectorDireccion.z)))\n",
"\t\n",
"\tdef rotaDireccion(self,pitchAngle,yawAngle,rollAngle):\n",
"\t\tself.vectorDireccion = rotationx(pitchAngle)*(rotationy(yawAngle)*(rotationz(rollAngle) * self.vectorDireccion))\n",
"\t\n",
"\tdef yaw(self,angle):\n",
"\t\tself.rotaDireccion(0 ,angle,0)\n",
"\t\t\n",
"\tdef pitch(self, angle):\n",
"\t\tself.rotaDireccion(angle, 0,0)\n",
"\t\n",
"\tdef roll(self, angle):\n",
"\t\tself.rotaDireccion(0, 0,angle)\n",
"\t\t\n",
"\tdef goto(self,px,py,pz):\n",
"\t\t\n",
"\t\t#Borra la tortuga de su anterior posicion\n",
"\t\tmc.setBlock(self.posicion.x,self.posicion.y,self.posicion.z,self.tipoBloque)\n",
"\t\t#Cambia la posicion\n",
"\t\tself.posicion = coordinate3d(px, py,pz)\n",
"\t\tif self.mostrarTortuga :\n",
"\t\t\tmc.setBlock(px,py,pz,self.tipoTortuga)\n",
"\t\tif self.trazando:\n",
"\t\t\tself.goTracer()\n",
" \n",
"\tdef forward(self,distancia):\n",
"\t\t \n",
"\t\tnuevaPos = self.posicion + self.vectorDireccion * distancia \n",
"\t\t\n",
"\t\tif not self.lapizArriba: #Si el lapiz no esta arriba pinta una linea desde la antigua posicion a la nueva\n",
"\t\t\t mcd.drawLine( int(self.posicion.x), int(self.posicion.y), int(self.posicion.z), \n",
"\t\t\t\t\t\t int(nuevaPos.x), int(nuevaPos.y), int(nuevaPos.z), self.tipoBloque)\n",
"\t\t\n",
"\t\t#Borra la tortuga de su anterior posicion\n",
"\t\tmc.setBlock(self.posicion.x,self.posicion.y,self.posicion.z,self.tipoBloque)\n",
"\t\t#La dibuja en la nueva\n",
"\t\tif self.mostrarTortuga :\n",
"\t\t\tmc.setBlock(nuevaPos.x,nuevaPos.y,nuevaPos.z,self.tipoTortuga)\n",
"\t\t\n",
"\t\tself.posicion = nuevaPos \n",
"\t\t\t\n",
"\t\tif self.trazando:\n",
"\t\t\tself.goTracer()\n",
"\t\t\n",
"\t\t\n",
"\t## Funciones de depuracion de la tortuga\n",
"\n",
"\tdef tracer(self, numIteraciones,segundos):\n",
"\t\t# Muestra las posicion de la tortuga cada num de iteraciones con un retraso de los segundos marcados\n",
"\t\tself.trazando = True\n",
"\t\tself.intervaloIteraciones = numIteraciones\n",
"\t\tself.pausaSegundos = segundos\n",
"\t\t\n",
"\tdef goTracer(self):\n",
"\t\t\n",
"\t\tself.contadorIteraciones += 1\n",
"\t\t\n",
"\t\tif self.contadorIteraciones >= self.intervaloIteraciones:\n",
"\t\t\ttime.sleep(self.pausaSegundos)\n",
"\t\t\tself.contadorIteraciones = 0\n",
"\t\t\tprint \"angulos: \" + str(self.heading()), \"posicion: \" + str(self.posicion)\n",
"\t\n",
"\n",
"\t\t\n",
"def setup(vectorDireccion=(0,1,0)):\n",
"\n",
"\ttortuga = Tortuga(4,block.LEAVES ,block.NETHER_REACTOR_CORE,False)\n",
"\ttortuga.vectorDireccion = coordinate3d(vectorDireccion[0],vectorDireccion[1],vectorDireccion[2])\n",
"\ttortuga.down()\n",
"\t\n",
"\t#Limpia el escenario\n",
"\tmc.setBlocks(-100,-50,-50, 100, 50, 50,block.AIR)\n",
"\t#Pone a el jugador cerca del origen\n",
"\tmc.player.setTilePos(0,-40,-18) \n",
"\t#Muestra la tortuga\n",
"\tmc.setBlock(0,0,0,tortuga.tipoTortuga)\n",
"\n",
"\treturn tortuga\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"L-Sistemas"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#load LSystem-en-Minecraft-/Lsystem3D.py"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''\n",
"Bernie's L System demo in Python.\n",
"\n",
"This program implements a simple context free L System,\n",
"and renders two dimensional images in a window.\n",
"It needs (at least) python 2.6, and uses the turtle \n",
"graphics library, which is based on TK graphics.\n",
"\n",
"To try it out, either load into Python and run the \n",
"demo functions, or run it from the command line:\n",
" \n",
" python lsystem.py\n",
"\n",
"Author: Bernie Pope: www.cs.mu.oz.au/~bjpop/\n",
"Licence: unrestricted.\n",
"\n",
"Feel free to play with the code as much as you like\n",
"and share your changes with the world.\n",
"\n",
"Some remarks about the implementation:\n",
"\n",
"An L-System is a term rewriting system made up of one or\n",
"more rules. Rules have the form:\n",
"\n",
" head -> body\n",
"\n",
"where head is a variable, and body is a non-empty string\n",
"made up of variables and constants. Variables are \n",
"denoted by upper-case alphabetic letters. Constants\n",
"are denoted by any character which is not a variable.\n",
"\n",
"Here is an example L-System: \n",
"\n",
" X -> F-[[X]+X]+F[+FX]-X\n",
" F -> FF\n",
"\n",
"In this program the convention is that the first rule\n",
"is taken as the starting point.\n",
"\n",
"An LSystem object is constructed like so:\n",
"\n",
" rules = ['X -> F-[[X]+X]+F[+FX]-X', 'F -> FF'] \n",
" my_system = LSystem(rules)\n",
"\n",
"That is, the LSystem constructor takes a list of strings\n",
"as its argument, where each string is a single rule.\n",
"\n",
"An LSystem object doesn't do anything on its own - it must\n",
"be interpreted.\n",
"\n",
"LSystem objects have a run method which takes two parameters:\n",
" 1) An non-negative integer which indicates how many times\n",
" the rules should be iterated. \n",
" 2) An interpreter object which implements an 'interpretTokens' \n",
" method.\n",
"\n",
"Here is a simple example:\n",
"\n",
" class SimpleInterp(object):\n",
" def interpretTokens(self, tokens): return tokens\n",
"\n",
" answer = my_system().run(6, SimpleInterp())\n",
"\n",
"A more sophisticated interpreter is defined called Visualise\n",
"which renders the result of iterating an LSystem as turtle\n",
"graphics.\n",
"\n",
"The Visualise constructor takes a dictionary mapping LSystem\n",
"variables to functions, here is an example:\n",
"\n",
" { '-': lambda _ : rollLess(rollLess_angle)\n",
" , '+': lambda _ : right(right_angle)\n",
" , 'F': lambda _ : forward(fwd_distance)\n",
" , '[': lambda obj : obj.push()\n",
" , ']': lambda obj : obj.pop()\n",
" }\n",
"\n",
"'''\n",
"import sys\n",
"from collections import deque\n",
"\n",
"tortuga = setup()\n",
"\n",
"# Configuration of graphics window\n",
"def initDisplay():\n",
"\ttortuga.showturtle()\n",
" # Codigo para depuracion\n",
" #tracer(1,1) # Dibuja una iteracion cada segundo.\n",
" #hideturtle() # don't draw the turtle; increase drawing speed.\n",
"\n",
"def initPosition(x,y,z):\n",
" tortuga.up() \n",
" tortuga.goto (x, y,z)\n",
" tortuga.down()\n",
"\n",
"\n",
"class LSystem(object):\n",
" def __init__ (self, rules):\n",
" if len(rules) > 0:\n",
" for r in rules:\n",
" exec(compile(r)) in locals()\n",
" firstRuleName,_ = decomposeRule(rules[0])\n",
" exec('def start(n): return ' + firstRuleName + '(n)') in locals()\n",
" self.rule = start\n",
" else:\n",
" self.rule = lambda _ : ''\n",
" def run(self, maxIterations, interpreter):\n",
" return interpreter.interpretTokens(self.rule(maxIterations))\n",
"\n",
"class Visualise (object):\n",
" def __init__(self, dict, initCommand=None):\n",
"\t self.actions = dict\n",
"\t self.initCommand = initCommand\n",
"\t self.stack_pos = deque()\n",
"\t self.stack_orient = deque()\n",
"\n",
" def interpretTokens(self, tokens):\n",
" initDisplay()\n",
" if self.initCommand != None: self.initCommand()\n",
" def action_fun(token):\n",
" return self.actions.get(token, lambda _ : None)(self)\n",
" self.stack_pos = deque() \n",
" self.stack_orient = deque()\n",
" map (action_fun, tokens)\n",
"\n",
" def push(self):\n",
" orient = tortuga.heading()\n",
" pos = tortuga.position()\n",
" self.stack_pos.append(pos)\n",
" self.stack_orient.append(orient)\n",
"\n",
" def pop(self):\n",
" stack_pos = self.stack_pos\n",
" stack_orient = self.stack_orient\n",
" if len(stack_pos) == 0 or len(stack_orient) == 0:\n",
" raise Exception('Attempt to pop empty stack')\n",
" pos = stack_pos.pop()\n",
" orient = stack_orient.pop()\n",
" tortuga.up()\n",
" tortuga.goto(pos[0],pos[1],pos[2]) \n",
" tortuga.setheading(orient[0],orient[1],orient[2])\n",
" tortuga.down()\n",
" \n",
"def basic_actions (left_angle, right_angle, fwd_distance):\n",
" return { '-': lambda _ : tortuga.roll(left_angle)\n",
" , '+': lambda _ : tortuga.roll(-right_angle)\n",
" , '|': lambda _ : tortuga.roll(180)\n",
" , '^': lambda _ : tortuga.yaw(left_angle)\n",
" , '&': lambda _ : tortuga.yaw(-right_angle)\n",
" , '}': lambda _ : tortuga.pitch(-left_angle)\n",
" , '{': lambda _ : tortuga.pitch(right_angle)\n",
" , 'F': lambda _ : tortuga.forward(fwd_distance)\n",
" , '[': lambda obj : obj.push()\n",
" , ']': lambda obj : obj.pop()\n",
" }\n",
" \n",
"def basic_actions2 (left_angle, right_angle, fwd_distance):\n",
" return { '-': lambda _ : tortuga.yaw(-left_angle)\n",
" , '+': lambda _ : tortuga.yaw(left_angle)\n",
" , '^': lambda _ : tortuga.pitch(-left_angle)\n",
" , '&': lambda _ : tortuga.pitch(right_angle)\n",
" , '>': lambda _ : tortuga.roll(-left_angle)\n",
" , '<': lambda _ : tortuga.roll(right_angle)\n",
" , 'F': lambda _ : tortuga.forward(fwd_distance)\n",
" , '[': lambda obj : obj.push()\n",
" , ']': lambda obj : obj.pop()\n",
" }\n",
"\n",
"\n",
"\n",
"'''\n",
"The input rule:\n",
" \n",
" X -> X+X+F\n",
"\n",
"is compiled to:\n",
"\n",
" def X(n): \n",
" if n > 0:\n",
" xn = X(n-1)\n",
" fn = F(n-1)\n",
" return ''.join([xn,'+',xn,'+',fn])\n",
" else:\n",
" return 'X'\n",
"'''\n",
"\n",
"def compile(rule):\n",
" (name, body) = decomposeRule(rule)\n",
" (vars,listIds) = varsIds(body)\n",
" defPart = 'def ' + name + '(n):'\n",
" varBinds = list(map(mkVarBind,vars))\n",
" joinListPart = \"''.join([\" + ','.join(listIds) + '])'\n",
" ifHead = 'if n > 0:' \n",
" ifBody = varBinds + ['return ' + joinListPart]\n",
" elsePart = 'else: return ' + quote(name)\n",
" return '\\n'.join(\n",
" [defPart] + map(indent,\n",
" [ifHead] + map(indent,\n",
" ifBody) + [elsePart]))\n",
"\n",
"def decomposeRule(rule):\n",
" splitRule = rule.split('->')\n",
" if len(splitRule) != 2:\n",
" raise Exception(\"badly formed L-System rule: \" + quote(str(rule)))\n",
" name = splitRule[0].strip()\n",
" body = splitRule[1].strip()\n",
" if len(name) != 1 or len(body) == 0:\n",
" raise Exception(\"badly formed L-System rule: \" + quote(str(rule)))\n",
" return (name, body)\n",
"\n",
"def mkVarBind(var):\n",
" return var.lower() + 'n = ' + var + '(n-1)'\n",
"\n",
"def quote(str): return \"'\" + str + \"'\"\n",
"\n",
"def indent(str): return ' ' + str\n",
"\n",
"def varsIds(str):\n",
" vars = set()\n",
" list = []\n",
" for c in str:\n",
" if c.isupper():\n",
" vars.add(c)\n",
" list.append(c.lower()+'n')\n",
" else:\n",
" list.append(quote(c))\n",
" return (vars, list)\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Algunos ejemplos de L-Sistemas"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#load LSystem-en-Minecraft-/demos.py"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def demo0():\n",
" def init():\n",
" initPosition(0,-45,0)\n",
" vis = Visualise(basic_actions(60,60,2), init)\n",
" basico().run(4,vis)\n",
"\n",
"def basico():\n",
" return LSystem(['A -> FFFFFFFFX ', \n",
" 'X -> [+FFFFY][-FFFFY]',\n",
" 'Y -> [+FFY][-FFY]',\n",
" 'F -> F']) \n",
"\n",
"\n",
"def demo1():\n",
" def init():\n",
" initPosition(0,-50,0)\n",
" vis = Visualise(basic_actions(25,25,5), init)\n",
" bushy_tree().run(5,vis)\n",
"\n",
"def bushy_tree():\n",
" return LSystem(['F -> [FF-[-F+F+F]+[+F-F-F]] FF{[{F}F}F]}[}F{F{F]']) # Regla en 1D: 'F -> FF-[-F+F+F]+[+F-F-F]'\n",
"\n",
"\n",
"def demo9():\n",
" def init():\n",
" initPosition(0,-50,0)\n",
" tortuga = setup()\n",
" tortuga.roll(-90)\n",
" actions = basic_actions(90,90,2)\n",
" vis = Visualise(actions, init)\n",
" peano_curve().run(4,vis)\n",
"\n",
"def peano_curve():\n",
" rules = [ 'X -> XFYFX+F+YFXFY-F-XFYFX'\n",
" , 'Y -> YFXFY-F-XFYFX+F+YFXFY'\n",
" , 'F -> F' ]\n",
" return LSystem(rules)\n",
" \n",
"def demo10():\n",
" def init():\n",
" initPosition(0,-40,0)\n",
" tortuga = setup()\n",
" tortuga.roll(90)\n",
" actions = basic_actions(90,90,2)\n",
" vis = Visualise(actions, init)\n",
" hilbert().run(5,vis)\n",
"\n",
"def hilbert():\n",
" rules = [ 'A -> -BF+AFA+FB-','B -> +AF-BFB-FA+', 'F -> F']\n",
" return LSystem(rules)\n",
" \n",
"\n",
"def demo11():\n",
" def init():\n",
" initPosition(0,-45,0)\n",
" #tracer(1,1)\n",
" \n",
" actions = basic_actions2(90,90,6)\n",
" vis = Visualise(actions, init)\n",
" hilbert3d().run( 5,vis)\n",
"\n",
"\n",
"def hilbert3d():\n",
" rules = [ 'X -> ^ < X F ^ < X F X - F ^ > > X F X & F + > > X F X - F > X - >', # ^ < X F ^ < X F X - F ^ > > X F X & F + > > X F X - F > X - >\n",
" 'F -> F']\n",
" return LSystem(rules)\n",
" \n",
"def interactive_demo():\n",
" def show_demo(name, action):\n",
" action()\n",
" \n",
" show_demo(\"Pruebas Simples\",demo0)\n",
" \n",
" #mc.setBlocks(-100,-50,-50, 100, 50, 50,block.AIR) #Limpia el escenario \n",
" #show_demo(\"Peano curve\", demo9)\n",
" \n",
" #mc.setBlocks(-100,-50,-50, 100, 50, 50,block.AIR) #Limpia el escenario \n",
" #show_demo(\"Hilbert curve\", demo10)\n",
" \n",
" #mc.setBlocks(-100,-50,-50, 100, 50, 50,block.AIR) #Limpia el escenario \n",
" #show_demo(\"Tree\", demo1)\n",
" \n",
"\n",
" \n",
"if __name__ == \"__main__\":\n",
" interactive_demo() \n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment