pastebin - collaborative debugging tool
iljamainz.kpaste.net RSS


LUA function plotter for sin(x)
Posted by Anonymous on Wed 1st Feb 2023 14:22
raw | new post
view followups (newest first): LUA function plotter for sin(x) by Anonymous

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

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