Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- --[[
- Usable arguments:
- keep - doesn't clear screen before drawing graph
- polar - draw polar graphs
- range - allows you to specify the range of x or theta to draw
- c:<colour> - the graph will be <colour>, for example c:0xFF0000 would be red
- noaxes - stop drawing of the axes
- --]]
- local function checkForStringInArgs(stringToCheckFor)
- local size = table.getn(args)
- for i = 1,size do
- if args[i] == stringToCheckFor then
- return true
- end
- end
- return false
- end
- local function checkForColourInArgs() -- needs its own function because it's a substring we check for
- local size = table.getn(args)
- for i = 1,size do
- if string.sub(args[i],1,2) == "c:" then
- return string.sub(args[i],3,-1)
- end
- end
- return false
- end
- local function findGlassesBridge()
- if peripheral.getType("left") == "openperipheral_glassesbridge" then
- return "left"
- end
- if peripheral.getType("right") == "openperipheral_glassesbridge" then
- return "right"
- end
- if peripheral.getType("top") == "openperipheral_glassesbridge" then
- return "top"
- end
- if peripheral.getType("bottom") == "openperipheral_glassesbridge" then
- return "bottom"
- end
- if peripheral.getType("back") == "openperipheral_glassesbridge" then
- return "back"
- end
- if peripheral.getType("front") == "openperipheral_glassesbridge" then
- return "front"
- end
- return "none"
- end
- local function prepareFunctionFileCartesian() -- clears/creates the function file then writes "y = "
- local file = fs.open("functionFile","w")
- file.write("y = ")
- file.close()
- return true
- end
- local function prepareFunctionFilePolar() -- clears/creates the function file then writes "r = "
- local file = fs.open("functionFile","w")
- file.write("r = ")
- file.close()
- return true
- end
- local function drawAxes()
- glassesBridge.addBox(0,127,480,1)
- glassesBridge.addBox(240,0,1,255)
- return true
- end
- local function writeFunctionToFile()
- if argPolar then
- if advancedComp then
- term.setTextColor(colors.green)
- end
- print("Use 'z' for theta")
- if advancedComp then
- term.setTextColor(colors.white)
- end
- write("r = ")
- functionToDraw = read()
- else
- write("y = ")
- functionToDraw = read()
- end
- local file = fs.open("functionFile","a")
- file.write(functionToDraw)
- file.close()
- return true
- end
- local function plotCartesian()
- if argRange then
- write("From: ")
- lowerBound = read()
- write("To: ")
- upperBound = read()
- end
- for i = lowerBound,upperBound do
- x = i
- shell.run("functionFile")
- glassesBridge.addBox(x+240,y*-1+127,1,1,graphColour)
- end
- return true
- end
- local function plotPolar()
- if argRange then
- write("From: ")
- lowerBound = read()
- write("To: ")
- upperBound = read()
- end
- for i = lowerBound*10,upperBound*10 do
- z = i/10
- shell.run("functionFile")
- x = r * math.cos(z)
- y = r * math.sin(z)
- glassesBridge.addBox(x+240,y*-1+127,1,1,graphColour)
- end
- return true
- end
- -- global variables
- x = 0
- y = 0
- r = 0
- argKeep = checkForStringInArgs("keep")
- argPolar = checkForStringInArgs("polar")
- argNoAxes = checkForStringInArgs("noaxes")
- argRange = checkForStringInArgs("range")
- argColour = checkForColourInArgs()
- if term.isColor() then
- advancedComp = true
- else
- advancedComp = false
- end
- glassesBridgeSide = findGlassesBridge()
- if side == "none" then
- error("No glasses bridge detected")
- else
- glassesBridge = peripheral.wrap(glassesBridgeSide)
- end
- if not argKeep then
- glassesBridge.clear()
- end
- if argColour ~= false then
- graphColour = tonumber(argColour)
- else
- graphColour = 0x0033FF
- end
- if not argPolar then
- lowerBound = -240
- upperBound = 240
- prepareFunctionFileCartesian()
- if not argNoAxes then
- drawAxes()
- end
- writeFunctionToFile()
- plotCartesian()
- else
- lowerBound = 0
- upperBound = 12.6
- prepareFunctionFilePolar()
- if not argNoAxes then
- drawAxes()
- end
- writeFunctionToFile()
- plotPolar()
- end
- fs.delete("functionFile")
Advertisement
Add Comment
Please, Sign In to add comment