Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- basado en:
- -- https://themathkid.tumblr.com/post/31090705353/the-midpoint-circle-algorithm-is-used-to-draw
- -- programa para construir torre de refrigeracion (figuras de revolucion)
- -- construye hacia la izquierda
- -- radio del circulo inicial
- r0 = 30
- -- altura incial (por si se ha estropeado). z0 por defecto
- -- se mide colocando el metro en un bloque de la primera capa
- z0 = 0
- -- altura de la torre
- hf = r0/0.4
- -- construccion de la torre
- turtle.select(1)
- -- comenzamos a medir el tiempo
- tiempo = os.clock()
- -- funcion redondeo
- function round(x)
- return math.floor(x+0.5)
- end
- -- funcion para buscar el siguiente slot con recursos
- -- si no quedan recursos devuelve false, en caso contrario true
- function placeBlock()
- while turtle.getItemCount() == 0 do
- turtle.select(turtle.getSelectedSlot() + 1)
- if turtle.getSelectedSlot() == 16 and turtle.getItemCount() == 0 then
- return false
- end
- end
- turtle.placeDown()
- return true
- end
- -- funcion para recoger mas bloques para construir de un inventario
- -- lo bloque se situaran en el centro de algun circulo inferior
- function reBlocks(r)
- turtle.turnLeft()
- for k = 1, r-1 do
- turtle.forward()
- end
- hi = 0
- turtle.select(1)
- while not turtle.suck() do
- turtle.down()
- hi = hi + 1
- end
- while turtle.suck() do
- end
- for k = 1, hi do
- turtle.up()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- for k = 1, r-1 do
- turtle.forward()
- end
- turtle.turnLeft()
- end
- -- hiperbola x=x(y)
- function hiper(y)
- return math.sqrt((0.86*y^2 - 1.26*y + 0.72)/4.75)
- -- return math.sqrt((0.61*y^2 - 0.87*y + 0.41)/2.74)
- -- return math.sqrt((0.73*y^2 - 1.04*y + 0.53)/3.51)
- end
- -- funcion de la curva r=r(z)
- function rfun(z)
- y0 = hiper(0)
- return r0/y0 * hiper(z/hf)
- end
- for z = z0, hf do
- -- cargamos combustible antes de empezar cada altura
- for k = 16, 1, -1 do
- if turtle.getItemCount(k)>0 then
- turtle.select(k)
- turtle.refuel(all)
- end
- end
- print(turtle.getFuelLevel())
- -- calculamos el radio de la altura actual y la siguiente
- r = rfun(z)
- rm1 = rfun(z+1)
- print("z=" .. z .. " r=" .. r)
- -- calculamos si tendremos suficientes recursos para la altura actual
- -- contamos cuantos recursos quedan
- items = 0
- for k = 1, 16 do
- items = items + turtle.getItemCount(k)
- end
- -- si no quedan suficientes recargamos
- if items < 8*r then
- reBlocks(round(r))
- end
- -- realizamos la circunferencia en cuatro cuartos
- for k = 1, 4 do
- -- reiniciamos la posicion a cada cuarto de vuelta
- x = round(r)
- y = 0
- -- creamos el primer octavo de vuelta
- while x>=y do
- -- para cada nivel de y miramos si la x actual es la mas cercana
- -- a la circunferencia de radio dado
- if round(math.sqrt(r^2 - y^2)) ~= x then
- turtle.turnLeft()
- turtle.forward()
- x = x-1
- turtle.turnRight()
- end
- if not placeBlock() then
- return
- end
- if x>y then
- turtle.forward()
- end
- y = y+1
- end
- y = y-1
- turtle.turnLeft()
- -- creamos el segundo octavo de vuelta
- -- hacemos lo mismo que para el primero pero cambiando x e y
- while x>=0 do
- if round(math.sqrt(r^2 - x^2)) ~= y then
- turtle.turnRight()
- turtle.forward()
- y = y+1
- turtle.turnLeft()
- end
- if not placeBlock() then
- return
- end
- if x>0 then
- turtle.forward()
- end
- x = x-1
- end
- x = x+1
- end
- -- redondeamos los datos para poder comparar los puntos de incio
- -- del circulo actual y el siguiente
- r = round(r)
- rm1 = round(rm1)
- -- al cambiar de altura debemos comprobar debemos movernos
- -- hacia dentro, hacia fuera o solo subir
- -- si el siguiente circulo es menor, nos movemos hacia dentro
- if r>rm1 then
- turtle.turnLeft()
- for k=1, r-rm1 do
- turtle.forward()
- end
- turtle.up()
- turtle.turnRight()
- -- si el siguiente circulo es mayor nos movemos hacia fuera
- elseif r<rm1 then
- turtle.turnRight()
- for k=1,rm1-r do
- turtle.forward()
- end
- turtle.up()
- turtle.turnLeft()
- -- si son iguales solo nos movemos hacia arriba
- else
- turtle.up()
- end
- end
- -- calculamos el tiempo requerido para la construccion
- tiempo = os.clock() - tiempo
- hor = math.floor(tiempo/3600)
- min = math.floor((tiempo - hor*3600)/60)
- seg = math.floor(tiempo - hor*3600 - min*60 + 0.5)
- print(string.format("Elapsed time: %02d:%02d:%02d",hor,min,seg))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement