# sin/cos grapher

Jan 27th, 2022 (edited)
542
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. --Terminal size
2. local w,h = term.getSize()
3. if w < 50 then w = w-5
4. else w = 50 end
5. if h < 20 then h = h-4
6. else h = 20 end
7.
8. --Query function
9. local function ask(q,c)
10.     if not c then c = colors.white end
11.     term.clear()
12.     term.setCursorPos(1,1)
13.     term.setTextColour(c)
14.     term.write(q)
15.     term.setTextColour(colors.white)
16.     term.setCursorPos(1,2)
18. end
19.
20. --Parameter capture
21. term.clear()
22.
23. local type = ask("Type of wave? (sin/cos)")
24. while type ~= "sin" and type ~= "cos" do
25.     type = ask("Type of wave? (sin/cos)")
26. end
27. local p = {}
28. p.a = tonumber(ask("Value for 'a'?"))
29. while not p.a do
30.     p.a = tonumber(ask("Value for 'a'? (Please enter a number value.)"))
31. end
32. p.b = ask("Value for 'b'?")
33. while not p.a do
34.     p.b = tonumber(ask("Value for 'b'? (Please enter a number value.)"))
35. end
36. p.c = ask("Value for 'c'?")
37. while not p.a do
38.     p.c = tonumber(ask("Value for 'c'? (Please enter a number value.)"))
39. end
40. p.d = ask("Value for 'd'?")
41. while not p.a do
42.     p.d = tonumber(ask("Value for 'd'? (Please enter a number value.)"))
43. end
44. local anim = ask("Animated? (y/n)")
45. while anim ~= "y" and anim ~= "n" do
46.     anim = ask("Animated? (y/n)")
47. end
48.
49. --Pixel plotting function
50. local function ppix(x,y,b,c,t)
51.     --x/y = position of pixel
52.     --c = color
53.     --t = text
54.     term.setCursorPos(x,y)
55.     term.setTextColour(c)
56.     term.setBackgroundColor(b)
57.     term.write(t)
58.     term.setTextColour(colors.white)
59.     term.setBackgroundColor(colors.black)
60. end
61.
62. local function txt(y,t)
63.     term.setTextColor(colors.white)
64.     term.setBackgroundColor(colors.black)
65.     term.setCursorPos(1,y)
66.     term.write(t)
67. end
68.
69. --draws border and parameters for graph
70. local function drawFrame(p)
71.     --top/bottom
72.     for i=2, w-1 do
73.         ppix(i,1,colors.black,colors.yellow,"_")
74.         ppix(i,h,colors.black,colors.yellow,"_")
75.     end
76.     --sides
77.     for i=2, h do
78.         ppix(1,i,colors.black,colors.yellow,"|")
79.         ppix(w,i,colors.black,colors.yellow,"|")
80.     end
81.     txt(h+2,"y = "..type.."("..p.b.."x".."+"..p.c..")+"..p.d)
82.     txt(h+3,"Amplitude: "..(p.a))
83.     txt(h+4,"Period: 2pi/"..p.b)
84.     txt(h+5,"Displacement: "..(-p.c/p.b))
85.     txt(h+6,"Vertical Displacement: "..p.d)
86. end
87.
88. --p = parameter table, i = x value increment. defaults to 0 in the drawWave function
89. local function calcSin(p,x,i)
90.     return p.a*math.sin(p.b*(x+i)+p.c)+p.d
91. end
92.
93. local function calcCos(p,x,i)
94.     return p.a*math.cos(p.b*(x+i)+p.c)+p.d
95. end
96.
97. local calc = calcSin
98.
99. --Single frame graph function
100. local function drawWave(p,i)
101.     if not i then i = 0 end
102.     for x = 2, w-1 do
103.         local y = calc(p,x-2,i)
104.         if y < h-1 and y+5 > 1 then
105.             ppix(x,y+5,colors.black,colors.red,".")
106.         end
107.     end
108.     drawFrame(p) --ERROR: Trying to grab variable 'type' before it gets assigned
109. end
110.
111. --Animation function
112. local function animWave(p,speed)
113.     for i = 0, 1000, speed do
114.         term.clear()
115.         drawWave(p,i)
116.         ppix(w-24,h+2,colors.black,colors.white,"X animation increment: "..speed)
117.         os.sleep(0.2)
118.     end
119. end
120.
121. --Function assignment
122. if type == "cos" then
123.     calc = calcCos
124. end
125.
126. --Program execution
127. if anim == "y" then
128.     local speed = tonumber(ask("Animation speed?"))
129.     while not speed do
130.         speed = tonumber(ask("Animation speed? (enter a number value to increment X by)"))
131.     end
132.     animWave(p,speed)
133. elseif anim == "n" then
134.     drawWave(p)
135. end
136.
137. --Done statement
138. ppix(1,30,colors.white,colors.blue,"...Done!")
139.
RAW Paste Data Copied