Guest User

graph

a guest
Jun 5th, 2014
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.11 KB | None | 0 0
  1. local args = {...}
  2. --[[
  3. Usable arguments:
  4. keep - doesn't clear screen before drawing graph
  5. polar - draw polar graphs
  6. range - allows you to specify the range of x or theta to draw
  7. c:<colour> - the graph will be <colour>, for example c:0xFF0000 would be red
  8. noaxes - stop drawing of the axes
  9. --]]
  10.  
  11. local function checkForStringInArgs(stringToCheckFor)
  12.   local size = table.getn(args)
  13.  
  14.   for i = 1,size do
  15.     if args[i] == stringToCheckFor then
  16.       return true
  17.     end
  18.   end
  19.  
  20.   return false
  21. end
  22.  
  23. local function checkForColourInArgs() -- needs its own function because it's a substring we check for
  24.   local size = table.getn(args)
  25.  
  26.   for i = 1,size do
  27.     if string.sub(args[i],1,2) == "c:" then
  28.       return string.sub(args[i],3,-1)
  29.     end
  30.   end
  31.  
  32.   return false
  33. end
  34.  
  35. local function findGlassesBridge()
  36.   if peripheral.getType("left") == "openperipheral_glassesbridge" then
  37.     return "left"
  38.   end
  39.  
  40.   if peripheral.getType("right") == "openperipheral_glassesbridge" then
  41.     return "right"
  42.   end
  43.  
  44.   if peripheral.getType("top") == "openperipheral_glassesbridge" then
  45.     return "top"
  46.   end
  47.  
  48.   if peripheral.getType("bottom") == "openperipheral_glassesbridge" then
  49.     return "bottom"
  50.   end
  51.  
  52.   if peripheral.getType("back") == "openperipheral_glassesbridge" then
  53.     return "back"
  54.   end
  55.  
  56.   if peripheral.getType("front") == "openperipheral_glassesbridge" then
  57.     return "front"
  58.   end
  59.  
  60.   return "none"
  61. end
  62.  
  63. local function prepareFunctionFileCartesian() -- clears/creates the function file then writes "y = "
  64.   local file = fs.open("functionFile","w")
  65.   file.write("y = ")
  66.   file.close()
  67.   return true
  68. end
  69.  
  70. local function prepareFunctionFilePolar() -- clears/creates the function file then writes "r = "
  71.   local file = fs.open("functionFile","w")
  72.   file.write("r = ")
  73.   file.close()
  74.   return true
  75. end
  76.  
  77. local function drawAxes()
  78.   glassesBridge.addBox(0,127,480,1)
  79.   glassesBridge.addBox(240,0,1,255)
  80.   return true
  81. end
  82.  
  83. local function writeFunctionToFile()
  84.   if argPolar then
  85.     if advancedComp then
  86.       term.setTextColor(colors.green)
  87.     end
  88.     print("Use 'z' for theta")
  89.    
  90.     if advancedComp then
  91.       term.setTextColor(colors.white)
  92.     end
  93.     write("r = ")
  94.    
  95.     functionToDraw = read()
  96.   else
  97.     write("y = ")
  98.     functionToDraw = read()
  99.   end
  100.   local file = fs.open("functionFile","a")
  101.   file.write(functionToDraw)
  102.   file.close()
  103.   return true
  104. end
  105.  
  106. local function plotCartesian()
  107.   if argRange then
  108.     write("From: ")
  109.     lowerBound = read()
  110.     write("To: ")
  111.     upperBound = read()
  112.   end
  113.  
  114.   for i = lowerBound,upperBound do
  115.     x = i
  116.     shell.run("functionFile")
  117.     glassesBridge.addBox(x+240,y*-1+127,1,1,graphColour)
  118.   end
  119.   return true
  120. end
  121.  
  122. local function plotPolar()
  123.   if argRange then
  124.     write("From: ")
  125.     lowerBound = read()
  126.     write("To: ")
  127.     upperBound = read()
  128.   end
  129.  
  130.   for i = lowerBound*10,upperBound*10 do
  131.     z = i/10
  132.     shell.run("functionFile")
  133.     x = r * math.cos(z)
  134.     y = r * math.sin(z)
  135.     glassesBridge.addBox(x+240,y*-1+127,1,1,graphColour)
  136.   end
  137.   return true
  138. end
  139.  
  140. -- global variables
  141. x = 0
  142. y = 0
  143. r = 0
  144. argKeep = checkForStringInArgs("keep")
  145. argPolar = checkForStringInArgs("polar")
  146. argNoAxes = checkForStringInArgs("noaxes")
  147. argRange = checkForStringInArgs("range")
  148. argColour = checkForColourInArgs()
  149.  
  150. if term.isColor() then
  151.   advancedComp = true
  152. else
  153.   advancedComp = false
  154. end
  155.  
  156. glassesBridgeSide = findGlassesBridge()
  157. if side == "none" then
  158.   error("No glasses bridge detected")
  159. else
  160.   glassesBridge = peripheral.wrap(glassesBridgeSide)
  161. end
  162.  
  163. if not argKeep then
  164.   glassesBridge.clear()
  165. end
  166.  
  167. if argColour ~= false then
  168.   graphColour = tonumber(argColour)
  169. else
  170.   graphColour = 0x0033FF
  171. end
  172.  
  173. if not argPolar then
  174.   lowerBound = -240
  175.   upperBound = 240
  176.   prepareFunctionFileCartesian()
  177.   if not argNoAxes then
  178.     drawAxes()
  179.   end
  180.   writeFunctionToFile()
  181.   plotCartesian()
  182. else
  183.   lowerBound = 0
  184.   upperBound = 12.6
  185.   prepareFunctionFilePolar()
  186.   if not argNoAxes then
  187.     drawAxes()
  188.   end
  189.   writeFunctionToFile()
  190.   plotPolar()
  191. end
  192.  
  193. fs.delete("functionFile")
Advertisement
Add Comment
Please, Sign In to add comment