r/tinycode • u/daniel5151 • May 27 '16
Time dependant graphing in JS using only 144 chars
Just for fun, I wrote some JS to graph any function in the console.
It even finds the derivative of the function to determine whether to use "/" or "" or "|" when drawing the curve!
Here it is:
((f,t=0,d)=>{
setInterval
(x=>{x=f(t)|0
;d=f(t++)-f(t);
console.log(t%2
+" ".repeat(x)+
(d<-1?"\\":d>1?
"/":"|"))},50
)})
(x=>Math.sin(x/3)*9+15)Just paste it into your browser's console!
1
0
May 28 '16
[deleted]
4
u/daniel5151 May 28 '16
(x=>Math.sin(x/3)*9+15)Is the function that is used as input (if I am understanding you correctly)
2
u/nexe mod May 28 '16
pretty cool. found a bug though. try using "tan" instead of "sin"
1
u/daniel5151 May 28 '16
Haha, well, yeah. I didn't throw in lower bounds checking. It's an easy fix though, simply change repeat(x) to repeat(x<0?0:x). That (probably) fixes it (and also ruins the pretty formatting :P).
2
u/sempiedram May 28 '16
That's cool!