Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Terminal size
- local w,h = term.getSize()
- if w < 50 then w = w-5
- else w = 50 end
- if h < 20 then h = h-4
- else h = 20 end
- --Query function
- local function ask(q,c)
- if not c then c = colors.white end
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColour(c)
- term.write(q)
- term.setTextColour(colors.white)
- term.setCursorPos(1,2)
- return read()
- end
- --Parameter capture
- term.clear()
- local type = ask("Type of wave? (sin/cos)")
- while type ~= "sin" and type ~= "cos" do
- type = ask("Type of wave? (sin/cos)")
- end
- local p = {}
- p.a = tonumber(ask("Value for 'a'?"))
- while not p.a do
- p.a = tonumber(ask("Value for 'a'? (Please enter a number value.)"))
- end
- p.b = ask("Value for 'b'?")
- while not p.a do
- p.b = tonumber(ask("Value for 'b'? (Please enter a number value.)"))
- end
- p.c = ask("Value for 'c'?")
- while not p.a do
- p.c = tonumber(ask("Value for 'c'? (Please enter a number value.)"))
- end
- p.d = ask("Value for 'd'?")
- while not p.a do
- p.d = tonumber(ask("Value for 'd'? (Please enter a number value.)"))
- end
- local anim = ask("Animated? (y/n)")
- while anim ~= "y" and anim ~= "n" do
- anim = ask("Animated? (y/n)")
- end
- --Pixel plotting function
- local function ppix(x,y,b,c,t)
- --x/y = position of pixel
- --c = color
- --t = text
- term.setCursorPos(x,y)
- term.setTextColour(c)
- term.setBackgroundColor(b)
- term.write(t)
- term.setTextColour(colors.white)
- term.setBackgroundColor(colors.black)
- end
- local function txt(y,t)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,y)
- term.write(t)
- end
- --draws border and parameters for graph
- local function drawFrame(p)
- --top/bottom
- for i=2, w-1 do
- ppix(i,1,colors.black,colors.yellow,"_")
- ppix(i,h,colors.black,colors.yellow,"_")
- end
- --sides
- for i=2, h do
- ppix(1,i,colors.black,colors.yellow,"|")
- ppix(w,i,colors.black,colors.yellow,"|")
- end
- txt(h+2,"y = "..type.."("..p.b.."x".."+"..p.c..")+"..p.d)
- txt(h+3,"Amplitude: "..(p.a))
- txt(h+4,"Period: 2pi/"..p.b)
- txt(h+5,"Displacement: "..(-p.c/p.b))
- txt(h+6,"Vertical Displacement: "..p.d)
- end
- --p = parameter table, i = x value increment. defaults to 0 in the drawWave function
- local function calcSin(p,x,i)
- return p.a*math.sin(p.b*(x+i)+p.c)+p.d
- end
- local function calcCos(p,x,i)
- return p.a*math.cos(p.b*(x+i)+p.c)+p.d
- end
- local calc = calcSin
- --Single frame graph function
- local function drawWave(p,i)
- if not i then i = 0 end
- for x = 2, w-1 do
- local y = calc(p,x-2,i)
- if y < h-1 and y+5 > 1 then
- ppix(x,y+5,colors.black,colors.red,".")
- end
- end
- drawFrame(p) --ERROR: Trying to grab variable 'type' before it gets assigned
- end
- --Animation function
- local function animWave(p,speed)
- for i = 0, 1000, speed do
- term.clear()
- drawWave(p,i)
- ppix(w-24,h+2,colors.black,colors.white,"X animation increment: "..speed)
- os.sleep(0.2)
- end
- end
- --Function assignment
- if type == "cos" then
- calc = calcCos
- end
- --Program execution
- if anim == "y" then
- local speed = tonumber(ask("Animation speed?"))
- while not speed do
- speed = tonumber(ask("Animation speed? (enter a number value to increment X by)"))
- end
- animWave(p,speed)
- elseif anim == "n" then
- drawWave(p)
- end
- --Done statement
- ppix(1,30,colors.white,colors.blue,"...Done!")
Add Comment
Please, Sign In to add comment