Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sX, sY, sZ = gps.locate() -- offsets to "0,0,0"
- local canvas = peripheral.call("back", "canvas3d")
- canvas.clear()
- local c = canvas.create()
- --c.recenter()
- local function clear()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function cPrint(key, val)
- print(string.format("Press '%s' to %s.", key, val))
- end
- local function createLine()
- local stage = 1
- -- get the points for the line
- local points = {}
- local boxes = {}
- for stage = 1, 2 do
- while true do
- clear()
- cPrint("enter", string.format("set position %d of line", stage))
- cPrint("backspace", "cancel creating a new line")
- local _, key = os.pullEvent("key")
- if key == keys.enter then
- -- create new point
- points[stage] = {gps.locate()}
- boxes[stage] = c.addBox(
- points[stage][1] - sX - 0.25,
- points[stage][2] - sY - 0.25,
- points[stage][3] - sZ - 0.25,
- 0.5,
- 0.5,
- 0.5
- )
- os.sleep(1)
- break
- elseif key == keys.backspace then
- for i = 1, #boxes do
- boxes[i].remove()
- end
- return
- end
- end
- end
- local p1 = {
- points[1][1] - sX,
- points[1][2] - sY,
- points[1][3] - sZ
- }
- local p2 = {
- points[2][1] - sX,
- points[2][2] - sY,
- points[2][3] - sZ
- }
- -- create and return the line (with the proper offsets applied)
- return {
- c.addLine(p1, p2, 30),
- boxes[1],
- boxes[2]
- }
- end
- local function confirmDeletion(lines, pos)
- local line = lines[pos]
- for i = 1, #line do
- line[i].setColor(255, 0, 0, 255)
- end
- while true do
- clear()
- print("The line selected has been highlighted red. Confirm deletion?")
- cPrint('y', "delete")
- cPrint('n', "cancel")
- local _, key = os.pullEvent("key")
- if key == keys.y then
- for i = 1, #line do
- line[i].remove()
- end
- table.remove(lines, pos)
- print("Deleted.")
- return
- elseif key == keys.n then
- break
- end
- end
- for i = 1, #line do
- line[i].setColor(255, 255, 255, 255)
- end
- end
- local function selectLine(lines, cr, cy, cb)
- -- determine player position
- local pX, pY, pZ = gps.locate()
- pX = pX - sX; pY = pY - sY; pZ = pZ - sZ
- -- determine what line is nearby
- local near = false
- for i = 1, #lines do
- line = lines[i][1]
- for o = 1, 2 do
- lX, lY, lZ = line.getPoint(o)
- pt = lines[i][o + 1]
- pt.setColor(cr, cy, cb, 255)
- if lX > pX - 1 and lX < pX + 1
- and lY > pY - 1 and lY < pY + 1
- and lZ > pZ - 1 and lZ < pZ + 1 then
- near = i
- pt.setColor(255, 255, 255, 255)
- break
- end
- pt.setColor(255, 255, 255, 255)
- if near then
- break
- end
- end
- end
- return near
- end
- local function deleteLine(lines)
- while true do
- clear()
- print("Stand at the start or end position of a line to select it for removal.")
- cPrint('enter', "select line for deletion")
- cPrint('backspace', "cancel")
- local _, key = os.pullEvent("key")
- if key == keys.enter then
- local near = selectLine(lines, 255, 0, 0)
- if near then
- -- confirm deletion
- confirmDeletion(lines, near)
- else
- print("Could not find any nearby lines.")
- end
- os.sleep(2)
- elseif key == keys.backspace then
- return
- end
- end
- end
- --------------------------------------------------------------------------------
- --------------------------------------------------------------------------------
- --
- -- Intersection functions
- --
- --------------------------------------------------------------------------------
- --------------------------------------------------------------------------------
- local function calcIntersection(l1, l2, plane)
- if plane == 'xz' then -- xz
- local p11 = {l1[1].getPoint(1)}
- local p12 = {l1[1].getPoint(2)}
- local a1 = p12[3] - p11[3]
- local b1 = p11[1] - p12[1]
- local c1 = a1 * p11[1] + b1 * p11[3]
- local p21 = {l2[1].getPoint(1)}
- local p22 = {l2[1].getPoint(2)}
- local a2 = p22[3] - p21[3]
- local b2 = p21[1] - p22[1]
- local c2 = a2 * p21[1] + b2 * p21[3]
- local det = a1 * b2 - a2 * b1
- if det == 0 then
- -- lines parallel
- return false, "Lines are parallel, no intersection."
- end
- return {(b2 * c1 - b1 * c2) / det, (a1 * c2 - a2 * c1) / det}
- elseif plane == 'xy' then -- xy
- elseif plane == 'zy' then -- zy
- end
- return false, "Not a plane."
- end
- local function intersect(lines, points)
- local intersectors = {}
- local i = 1
- while not intersectors[2] do
- clear()
- print("Stand at the start or end position of a line to select it for intersection.")
- print(string.format("This will be line %d", i))
- cPrint('enter', "select line for intersection")
- cPrint('backspace', "cancel")
- local _, key = os.pullEvent("key")
- if key == keys.enter then
- local near = selectLine(lines, 0, 255, 0)
- local line = lines[near]
- if line then
- intersectors[i] = line
- for o = 1, #intersectors do
- for p = 1, #intersectors[o] do
- intersectors[o][p].setColor(0, 255, 0)
- end
- end
- i = i + 1
- else
- print("Could not find any lines nearby.")
- os.sleep(2)
- end
- elseif key == keys.backspace then
- return
- end
- end
- while true do
- clear()
- print("Select the plane to check for intersections on")
- cPrint('x', "select xz plane (flat)")
- cPrint('y', "select xy plane")
- cPrint('z', "select zy plane")
- local _, key = os.pullEvent("key")
- local hit, err = calcIntersection(
- intersectors[1],
- intersectors[2],
- key == keys.x and 'xz' or key == keys.y and 'xy' or key == keys.z and 'zy'
- )
- if hit then
- local function hString(a, b, c)
- print(string.format("Calculated intersection point to be %.2f, %.2f, %.2f", a, b, c))
- end
- local extra = {intersectors[1][1].getPoint(1)}
- local h1 = hit[1] - 0.25
- local h2 = hit[2] - 0.25
- local e1 = extra[1] - 0.25
- local e2 = extra[2] - 0.25
- local e3 = extra[3] - 0.25
- if key == keys.x then -- xz
- points[#points + 1] = {
- c.addBox(h1, e2, h2, 0.5, 0.5, 0.5),
- c.addLine({0, 0, 0}, {hit[1], extra[2], hit[2]}, 10)
- }
- for i = 1, #points[#points] do
- points[#points][i].setColor(0, 255, 0)
- end
- hString(hit[1], extra[2], hit[2])
- elseif key == keys.y then -- xy
- points[#points + 1] = {
- c.addBox(h1, h2, e3, 0.5, 0.5, 0.5),
- c.addLine({0, 0, 0}, {hit[1], hit[2], extra[3]}, 10)
- }
- for i = 1, #points[#points] do
- points[#points][i].setColor(0, 255, 0)
- end
- hString(hit[1], hit[2], extra[3])
- elseif key == keys.z then -- zy
- points[#points + 1] = {
- c.addBox(e1, h2, h1, 0.5, 0.5, 0.5),
- c.addLine({0, 0, 0}, {extra[1], hit[2], hit[1]}, 10)
- }
- for i = 1, #points[#points] do
- points[#points][i].setColor(0, 255, 0)
- end
- hString(extra[1], hit[2], hit[1])
- end
- os.sleep(2)
- break
- else
- print(err)
- os.sleep(2)
- return
- end
- end
- for o = 1, #intersectors do
- for p = 1, #intersectors[o] do
- intersectors[o][p].setColor(255, 255, 255)
- end
- end
- end
- local function recenter(lines, points)
- local nX, nY, nZ = gps.locate()
- local dX, dY, dZ = nX - sX, nY - sY, nZ - sZ
- c.recenter()
- for i = 1, #lines do
- local oX1, oY1, oZ1 = lines[i][1].getPoint(1)
- local oX2, oY2, oZ2 = lines[i][1].getPoint(2)
- local x1, y1, z1 = oX1 - dX, oY1 - dY, oZ1 - dZ
- local x2, y2, z2 = oX2 - dX, oY2 - dY, oZ2 - dZ
- lines[i][1].setPoint(1, x1, y1, z1)
- lines[i][1].setPoint(2, x2, y2, z2)
- lines[i][2].setPosition(x1 - 0.25, y1 - 0.25, z1 - 0.25)
- lines[i][3].setPosition(x2 - 0.25, y2 - 0.25, z2 - 0.25)
- end
- for i = 1, #points do
- local oX, oY, oZ = points[i][2].getPoint(2)
- local x, y, z = oX - dX, oY - dY, oZ - dZ
- points[i][1].setPosition(x - 0.25, y - 0.25, z - 0.25)
- points[i][2].setPoint(2, x, y, z)
- end
- sX, sY, sZ = nX, nY, nZ
- end
- local lines = {}
- local points = {}
- local t = 0.5
- local tmr = os.startTimer(t)
- print("YABA DABA DOO")
- while true do
- clear()
- print(string.format("Currently %d lines", #lines))
- print(string.format("Currently %d points", #points))
- print(string.format("Origin set at %d, %d, %d", sX, sY, sZ))
- cPrint('n', "create a new line")
- cPrint('d', "delete a line")
- cPrint('r', "delete a point (not implemented)")
- cPrint('c', "calculate intersections")
- cPrint('r', "recenter (use if lines disappear)")
- cPrint('backspace', "clear everything")
- local ev = {os.pullEvent()}
- print(ev[1], tmr, ev[2])
- if ev[1] == "key" then
- local key = ev[2]
- if key == keys.n then
- -- new line
- lines[#lines+1] = createLine()
- elseif key == keys.d then
- -- delete line
- deleteLine(lines)
- elseif key == keys.c then
- intersect(lines, points)
- elseif key == keys.r then
- recenter(lines, points)
- elseif key == keys.backspace then
- lines = {}
- points = {}
- canvas.clear()
- c = canvas.create()
- sX, sY, sZ = gps.locate()
- end
- tmr = os.startTimer(t)
- elseif ev[1] == "timer" and ev[2] == tmr then
- local pX, pY, pZ = gps.locate()
- if pX then
- pX = pX - sX; pY = pY - sY; pZ = pZ - sZ
- for i = 1, #points do
- points[i][2].setPoint(1, pX, pY + 0.1, pZ)
- end
- end
- tmr = os.startTimer(t)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement