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
| 1 | 1 | -- test1.lua - simple function plotter in lua | |
| 2 | 2 | -- written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 3 | 3 | ||
| 4 | 4 | -- Terminal size | |
| 5 | - | TermSize = {w = 70, h = 20} | |
| 5 | + | TermSize = {w = 376-2, h = 119-2} | |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | 8 | function term_clear () | |
| 9 | 9 | io.write("\27[2J") | |
| 10 | 10 | end | |
| 11 | 11 | ||
| 12 | 12 | function term_cup (x,y) | |
| 13 | 13 | io.write( | |
| 14 | 14 | string.format("\27[%d;%dH", | |
| 15 | 15 | math.floor(y), | |
| 16 | 16 | math.floor(x) | |
| 17 | 17 | ) | |
| 18 | 18 | ) | |
| 19 | 19 | end | |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | 22 | -- plot a function | |
| 23 | 23 | -- (assume that domain&&image are in the range [-1,+1]) | |
| 24 | 24 | function plot (f) | |
| 25 | + | local i | |
| 26 | 28 | ||
| 29 | + | term_clear() | |
| 27 | 31 | 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 | |
| 30 | 34 | ||
| 31 | 35 | term_cup(i, y) | |
| 32 | 36 | io.write('X') | |
| 33 | 37 | end | |
| 34 | 38 | ||
| 35 | 39 | -- move cursor to the bottom | |
| 36 | 40 | term_cup(1, TermSize.h+1) | |
| 37 | 41 | end | |
| 38 | 42 | ||
| 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) | |
| 40 | 44 | ||
| 41 | 45 | -- EOF. | |
| 42 | 46 | ||
| 43 | 47 |
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.
iljamainz.kpaste.net RSS