Advertisement
lilo_booter

Plotify v2

Nov 19th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.19 KB | None | 0 0
  1. (
  2. PLOTIFY
  3.  
  4. A very silly sample FORTH vocabulary which provides a means to plot graphs in a
  5. standard terminal.
  6.  
  7. Examples:
  8.  
  9. \ Plot axis
  10. t-green xy-axis
  11.  
  12. \ y = x
  13. t-red s" x" x-loop-equation xy-home
  14.  
  15. \ x = y
  16. t-green s" y fnegate" y-loop-equation xy-home
  17.  
  18. \ y = sin x
  19. t-blue s" x fsin" x-loop-equation xy-home
  20.  
  21. \ y = x^2 - 10
  22. t-magenta s" x 2e f** 10e f-" x-loop-equation xy-home
  23. )
  24.  
  25. \ TERMINAL CONTROL
  26.  
  27.     \ Standard ASCII Codes (not comprehensive)
  28.  
  29.         \ Emits a bell
  30.         : t-bel 7 emit ;
  31.  
  32.         \ Emits a backspace
  33.         : t-bs 8 emit ;
  34.  
  35.         \ Emits a tab
  36.         : t-tab 9 emit ;
  37.  
  38.         \ Emits a line feed
  39.         : t-lf 10 emit ;
  40.  
  41.         \ Emits a vertical tab
  42.         : t-tab-v 11 emit ;
  43.  
  44.         \ Emits a form feed
  45.         : t-ff 12 emit ;
  46.  
  47.         \ Emits a carriage return
  48.         : t-cr 13 emit ;
  49.  
  50.         \ Emits an escape
  51.         : t-esc 27 emit ;
  52.  
  53.         \ Emits a delete
  54.         : t-del 127 emit ;
  55.  
  56.     \ Control Sequence Introducer (CSI) commands (not comprehensive)
  57.  
  58.         \ Emit CSI
  59.         : t-csi ( -- ) t-esc '[' emit ;
  60.  
  61.         \ Returns cursor to top left
  62.         : t-home ( -- ) t-csi 'H' emit ;
  63.  
  64.         \ Places cursor at the requested column and row
  65.         : t-at ( col row -- ) t-csi .nb ';' emit .nb 'H' emit ;
  66.  
  67.         \ Erase from cursor until end of screen
  68.         : t-cleol ( -- ) t-csi ." J" ;
  69.  
  70.         \ Clear the entire screen and move cursor to top left
  71.         : t-cls ( -- ) t-csi ." 2J" t-home ;
  72.  
  73.         \ Sets cursor to default foreground
  74.         : t-default t-csi ." 0;0m" ;
  75.  
  76.         \ Sets foreground color to black
  77.         : t-red t-csi ." 0;30m" ;
  78.  
  79.         \ Sets foreground color to red
  80.         : t-red t-csi ." 0;31m" ;
  81.  
  82.         \ Sets foreground color to green
  83.         : t-green t-csi ." 0;32m" ;
  84.  
  85.         \ Sets foreground color to yellow
  86.         : t-yellow t-csi ." 0;33m" ;
  87.  
  88.         \ Sets foreground color to blue
  89.         : t-blue t-csi ." 0;34m" ;
  90.  
  91.         \ Sets foreground color to magenta
  92.         : t-magenta t-csi ." 0;35m" ;
  93.  
  94.         \ Sets foreground color to cyan
  95.         : t-cyan t-csi ." 0;36m" ;
  96.  
  97.         \ Sets foreground color to white
  98.         : t-white t-csi ." 0;37m" ;
  99.  
  100.         \ Sets background color to black
  101.         : t-red-bg t-csi ." 0;40m" ;
  102.  
  103.         \ Sets background color to red
  104.         : t-red-bg t-csi ." 0;41m" ;
  105.  
  106.         \ Sets background color to green
  107.         : t-green-bg t-csi ." 0;42m" ;
  108.  
  109.         \ Sets background color to yellow
  110.         : t-yellow-bg t-csi ." 0;43m" ;
  111.  
  112.         \ Sets background color to blue
  113.         : t-blue-bg t-csi ." 0;44m" ;
  114.  
  115.         \ Sets background color to magenta
  116.         : t-magenta-bg t-csi ." 0;45m" ;
  117.  
  118.         \ Sets background color to cyan
  119.         : t-cyan-bg t-csi ." 0;46m" ;
  120.  
  121.         \ Sets background color to white
  122.         : t-white-bg t-csi ." 0;47m" ;
  123.  
  124.         \ Set custom foreground
  125.         : t-rgb-fg ( r g b -- ) t-csi ." 38;2;" rot .nb ';' emit swap .nb ';' emit .nb 'm' emit ;
  126.  
  127.         \ Set custom background
  128.         : t-rgb-bg ( r g b -- ) t-csi ." 48;2;" rot .nb ';' emit swap .nb ';' emit .nb 'm' emit ;
  129.  
  130.         \ Provides the terminal width
  131.         : t-width ( -- cols ) cols ;
  132.  
  133.         \ Provides the terminal height
  134.         : t-height ( -- rows ) rows ;
  135.  
  136.         \ Save the terminal position
  137.         : t-save ( -- ) t-csi ." s" ;
  138.  
  139.         \ Returns position to previously saved
  140.         : t-restore ( -- ) t-csi ." u" ;
  141.  
  142.  
  143. \ GENERAL PURPOSE WORDS
  144.  
  145.     \ Negative pi for convenience
  146.     : -pi ( -- F: -pi ) pi fnegate ;
  147.  
  148.     \ Order the top 2 ints on the stack such that the lowest value is tos
  149.     : max-min ( a b -- a b | b a ) 2dup > if swap endif ;
  150.  
  151.     \ Duplicate the top two floats on the float stack
  152.     : f2dup ( F: a F: b -- F: a F: b F: a F: b ) fover fover ;
  153.  
  154.     \ Order the top 2 floats on the stack such that the lowest value is tos
  155.     : fmax-min ( F: a F: b -- F: a F: b | F: b F: a ) f2dup f> if fswap endif ;
  156.  
  157.     \ Create and assign a new variable
  158.     : var! ( r "name" -- ) create , does> ;
  159.  
  160.     \ Create and assign a new float variable
  161.     : fvar! ( r "name" -- ) create f, does> ;
  162.  
  163.     \ Check if value is between lower and upper inclusive
  164.     : between ( value lower upper -- bool ) 2 pick >= swap rot <= and ;
  165.  
  166.     \ Check if x,y is in 2d range
  167.     : in-range ( x y x-low x-up y-low y-up -- x y bool ) 4 pick -rot between -rot 4 pick -rot between and ;
  168.  
  169.     \ Obtain a random number
  170.     2345 value seed0
  171.     6789 value seed1
  172.  
  173.     : rand ( -- u )
  174.         seed0
  175.         seed1 to seed0
  176.         dup 13 lshift xor
  177.         dup 17 rshift xor
  178.         dup 5 lshift xor
  179.         dup seed1 xor to seed1
  180.     ;
  181.  
  182.     \ Debug stack
  183.     : .sd ( -- ) cr .s ;
  184.  
  185.     \ Debug float stack
  186.     : f.sd ( -- ) cr f.s ;
  187.  
  188. \ GRAPH PLOTTING
  189.  
  190.     \ We'll use the following variables to define the ranges of x and y and specify
  191.     \ the current plot character
  192.     -10e fvar! x-lower
  193.     10e  fvar! x-upper
  194.     -10e fvar! y-lower
  195.     10e  fvar! y-upper
  196.     '*'  var!  xy-symbol
  197.  
  198.     \ Graphs are drawn in all but the last 5 rows of the terminal
  199.     : xy-height ( -- rows ) t-height 5 - ;
  200.  
  201.     \ Returns the usable width of the terminal
  202.     : xy-width ( -- cols ) t-width ;
  203.  
  204.     \ Places cursor below graph after rendering
  205.     : xy-home ( -- ) 0 xy-height t-at t-default t-cleol ;
  206.  
  207.     \ Allows us to redefine the range of x
  208.     : x-range ( F: lower F: upper -- ) x-upper f! x-lower f! ;
  209.  
  210.     \ Allows us to redefine the range of y
  211.     : y-range ( F: lower F: upper -- ) y-upper f! y-lower f! ;
  212.  
  213.     \ Allows us to change the character plotted
  214.     : set-xy-symbol ( char -- ) xy-symbol ! ;
  215.  
  216.     \ Determines the floating point increment of x
  217.     : x-increment x-upper f@ x-lower f@ f- xy-width s>f f/ ;
  218.  
  219.     \ Determines the floating point increment of y
  220.     : y-increment y-upper f@ y-lower f@ f- xy-height s>f f/ ;
  221.  
  222.     \ Determines the number of terminal columns covered by the value r
  223.     : r-to-cols x-increment f/ fround f>s ;
  224.  
  225.     \ Maps x value to column
  226.     : x-to-col ( F: x -- column ) x-lower f@ f- x-increment f/ fround f>s ;
  227.  
  228.     \ Maps y value to column
  229.     : y-to-row ( F: y -- row ) y-lower f@ f- y-increment f/ fround f>s ;
  230.  
  231.     \ Determines if the given column and row are in the range of the graphs width and height
  232.     : col-row-in-range ( col row -- col row bool ) 0 xy-width 0 xy-height in-range ;
  233.  
  234.     \ Render xy-symbol at col,row
  235.     : col-row-at
  236.         col-row-in-range
  237.         if
  238.             xy-height swap - t-at xy-symbol @ xemit
  239.         else
  240.             2drop
  241.         endif
  242.     ;
  243.  
  244.     \ Render associated to x,y
  245.     : xy-plot
  246.         y-lower f@ f- y-increment f/ xy-height s>f fswap f- fswap
  247.         x-lower f@ f- x-increment f/
  248.         f>s f>s col-row-at
  249.     ;
  250.  
  251.     \ Map incoming col to x
  252.     : col-to-x ( col -- F: x ) s>f x-increment f* x-lower f@ f+ ;
  253.  
  254.     \ Map incoming row to y
  255.     : row-to-y ( row -- F: y ) s>f y-increment f* y-lower f@ f+ ;
  256.  
  257.     \ Create an equation
  258.     : x-create-equation ( "equation" -- xt ) s" :noname { F: x } " 2swap s+ s"  ;" s+ evaluate ;
  259.  
  260.     \ Invoke x-equation for each x
  261.     : x-loop-equation
  262.         ( "equation" -- ) x-create-equation { equation }
  263.         xy-width 0 do
  264.             i col-to-x equation execute i y-to-row col-row-at
  265.         loop
  266.     ;
  267.  
  268.     \ Plots coordinates of equation
  269.     : x-loop ( "equation" -- ) x-loop-equation xy-home ;
  270.  
  271.     \ Create an y-equation
  272.     : y-create-equation ( "equation" -- xt ) s" :noname { F: y } " 2swap s+ s"  ;" s+ evaluate ;
  273.  
  274.     \ Invoke y-equation for each y
  275.     : y-loop-equation ( "equation" -- )
  276.         ( "equation" -- ) y-create-equation { equation }
  277.         xy-height 0
  278.         do
  279.             i row-to-y equation execute x-to-col i col-row-at
  280.         loop
  281.     ;
  282.  
  283.     \ Plots coordinates of equation
  284.     : y-loop ( "equation" -- ) y-loop-equation xy-home ;
  285.  
  286.     \ Draws the axis
  287.     : xy-axis
  288.         t-cls
  289.         xy-symbol @
  290.         '-' xy-symbol !
  291.         s" 0e" x-loop-equation
  292.         '|' xy-symbol !
  293.         s" 0e" y-loop-equation
  294.         xy-symbol !
  295.         xy-home
  296.     ;
  297.  
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement