42
\$\begingroup\$

Print a continuous sinusoidal wave scrolling vertically on a terminal. The program should not terminate and should continuously scroll down the wave (except until it is somehow interrupted). You may assume overflow is not a problem (i.e. you may use infinite loops with incrementing counters, or infinite recursion).

The wave should satisfy the following properties:

  • Amplitude = 20 chars (peak amplitude)
  • Period = 60 to 65 lines (inclusive)
  • The output should only consist of spaces, newline and |
  • After each line of output, pause for 50ms

Sample output:

                    |
                      |
                        |
                          |
                            |
                              |
                                |
                                 |
                                   |
                                    |
                                     |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
            |
          |
        |
       |
     |
    |
   |
  |
 |
 |
 |
 |
 |
 |
 |
  |
   |
    |
     |
      |
        |
         |
           |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                             |
                              |
                                |
                                  |
                                   |
                                    |
                                      |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                    |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
           |
          |
        |
       |
     |
    |
   |
  |
 |
 |
 |
 |
 |
 |
 |
  |
   |
    |
     |
       |
        |
          |
            |
             |
               |
                 |
                   |

The above output should go on forever unless otherwise interrupted, e.g. SIGINT or SIGKILL, or closing terminal window, or you power off your machine, or the Sun swallows the Earth, etc.

Shortest code wins.

Note. I am aware of a similar problem on Display Scrolling Waves but this isn't exactly the same. In my problem, the wave is not to be scrolled "in place" - just output it on a terminal. Also, this is an ascii-art problem, so don't use Mathematica to plot it.

\$\endgroup\$
  • 1
    \$\begingroup\$ Peak amplitude, peak-to-peak amplitude, or root-square amplitude? \$\endgroup\$ – DavidC Jan 16 '14 at 20:31
  • \$\begingroup\$ Peak amplitude. \$\endgroup\$ – user12205 Jan 16 '14 at 20:38
  • 1
    \$\begingroup\$ Is it ok to draw a wave with just |s and no spaces? \$\endgroup\$ – Gelatin Jan 17 '14 at 1:23
  • 1
    \$\begingroup\$ All answers yet are invalid. They also stop for SIGKILL not just for SIGINT. \$\endgroup\$ – Max Ried Apr 4 '14 at 9:02
  • 1
    \$\begingroup\$ @Max Ried fine, I will change it to "should go on forever unless otherwise interrupted". \$\endgroup\$ – user12205 Apr 4 '14 at 10:28

38 Answers 38

1 2
0
\$\begingroup\$

TI-BASIC, 37

Executed from a TI-84 calculator

:While 1:DrawInv 2.9sin(X:ClrDraw:End
  • Peak Amplitude = 20
  • Period = 62
  • Pause between lines 50-60 ms (very close as there is no built-in clock)
\$\endgroup\$
  • 5
    \$\begingroup\$ OP: this is an ascii-art problem \$\endgroup\$ – MrZander Jan 17 '14 at 0:13
  • 1
    \$\begingroup\$ almost all of the programming language answers use a predefined function for Sine, so I don't see how this would be against the rules. as this uses the predefined function for the language it is written in just the same as say Javascript \$\endgroup\$ – Malachi Jan 17 '14 at 0:44
  • 1
    \$\begingroup\$ The problem @MrZander is trying to bring up here is that you're not doing this with ASCII characters, but by drawing a graph. That's against the rules. \$\endgroup\$ – joeytje50 Jan 17 '14 at 1:14
  • 5
    \$\begingroup\$ No, DrawInv is a graphing feature of the TI-84, so this is just like using Mathematica to plot a sine wave. tibasicdev.wikidot.com/drawinv Also, all TI-84s have RTCs. \$\endgroup\$ – Kevin Chen Jan 17 '14 at 4:56
  • 7
    \$\begingroup\$ I agree with @Kevin Chen that this is essentially just like using Mathematica to plot a sine wave. So, I still do not consider this to be a valid answer. Also, according to the picture in the link given by Kevin, I do not consider this to be ascii art. I think this is just some low resolution bitmap, so this answer does not qualify. \$\endgroup\$ – user12205 Jan 17 '14 at 16:42
0
\$\begingroup\$

GTB, 14

Executed from a TI-84 calculator

[i;2.9sin(Xc;]

Amplitude = 20, Period = 62, Pause = ~50

\$\endgroup\$
  • \$\begingroup\$ Since this is basically the same as your other answer, I consider this to be invalid as well. \$\endgroup\$ – user12205 Jun 14 '15 at 22:33
0
\$\begingroup\$

Groovy 87 Chars

Okay, now for a serious answer that actually follows the guidelines

def x=0;while(true){println ' '*(20*Math.sin(x)+20)+'|';x+=0.1;Thread.sleep(50)}
\$\endgroup\$
  • 2
    \$\begingroup\$ Do you think a suitable decimal approximation for Math.PI/30 can save you a few chars? \$\endgroup\$ – user12205 Jan 17 '14 at 16:53
0
\$\begingroup\$

CJam, 42 bytes

CJam is a bit younger than this challenge, but since this submission doesn't beat APL anyway, I don't see a problem with that.

es{es1$m50>{500d/ms21*K+S41*\i'|tN+oes}*}h

This won't work in the online interpreter, so you'll have to use the Java one.

The basic idea is to keep a timestamp on the stack, and whenever 50 ms have passed since the last timestamp, a new line is printed (where the sine is computed from that last time), and a new timestamp left on the stack.

\$\endgroup\$
0
\$\begingroup\$

Pascal: 99 characters

uses Crt;var i:Real;begin
repeat
Writeln('|':Round(Sin(i)*20+21));i:=i+0.1;Delay(500)until 0=1;end.

Another task where Pascal's output formatting comes handy and everything else is pain.

\$\endgroup\$
0
\$\begingroup\$

Julia, 60 chars

Unlike CCP’s solution, mine uses a recursive function:

a(n)=(print(" "^int(20sin(.1n)+20)*"|\n");sleep(.05);a(n+1))

Example:

julia> a(1)
                      |
                        |
                          |
                            |
                              |
                               |
                                 |
                                  |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                  |
                                |
                              |
                             |
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 44 bytes

@L
A=A+.1?" "*(COS(A)*20+20);"|
WAIT 3GOTO@L
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 57 bytes

for(;;sleep -m 50){' '*(20*[math]::Sin(($i+=.1))+20)+'|'}

Try it online!

\$\endgroup\$
1 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.