pastebin - collaborative debugging tool
iljamainz.kpaste.net RSS

Difference between
modified post e0bfcc287f by Anonymous on Fri 3rd Feb 2023 10:59
original post 20667d by Anonymous on Wed 1st Feb 2023 14:22
Download diff
Show old version | new version | both versions

    
11
-- test1.lua - simple function plotter in lua
22
-- written by Roland Mainz <roland.mainz@nrubsig.org>
33
44
-- Terminal size
5-
TermSize = {w = 70, h = 20}
5+
TermSize = {w = 376-2, h = 119-2}
66
77
88
function term_clear ()
99
	io.write("\27[2J")
1010
end
1111
1212
function term_cup (x,y)
1313
	io.write(
1414
		string.format("\27[%d;%dH",
1515
			math.floor(y),
1616
			math.floor(x)
1717
			)
1818
		)
1919
end
2020
2121
2222
-- plot a function
2323
-- (assume that domain&&image are in the range [-1,+1])
2424
function plot (f)
25+
	local i
2628
29+
	term_clear()
2731
	for i=1, TermSize.w do
28-
		local x = (i/TermSize.w)*2 - 1
32+
		x = (i/TermSize.w)*2 - 1
29-
		local y = (f(x) + 1)/2 * TermSize.h
33+
		y = (f(x) + 1)/2 * TermSize.h
3034
3135
		term_cup(i, y)
3236
		io.write('X')
3337
	end
3438
3539
	-- move cursor to the bottom
3640
	term_cup(1, TermSize.h+1)
3741
end
3842
39-
plot(function (x) return math.sin(x*2*math.pi) end)
43+
plot(function (x) return math.sin(x*2*math.pi)*math.sin(x*3*math.pi) end)
4044
4145
-- EOF.
4246
4347

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at