pastebin - collaborative debugging tool
iljamainz.kpaste.net RSS


LUA function plotter for sin(x)
Posted by Anonymous on Fri 3rd Feb 2023 10:59
raw | new post
view followups (newest first): LUA function plotter for sin(x) by Anonymous
modification of post by Anonymous (view diff)

  1. -- test1.lua - simple function plotter in lua
  2. -- written by Roland Mainz <roland.mainz@nrubsig.org>
  3.  
  4. -- Terminal size
  5. TermSize = {w = 376-2, h = 119-2}
  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.         local i
  26.         local x
  27.         local y
  28.  
  29.         term_clear()
  30.  
  31.         for i=1, TermSize.w do
  32.                 x = (i/TermSize.w)*2 - 1
  33.                 y = (f(x) + 1)/2 * TermSize.h
  34.  
  35.                 term_cup(i, y)
  36.                 io.write('X')
  37.         end
  38.  
  39.         -- move cursor to the bottom
  40.         term_cup(1, TermSize.h+1)
  41. end
  42.  
  43. plot(function (x) return math.sin(x*2*math.pi)*math.sin(x*3*math.pi) end)
  44.  
  45. -- 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