Make a Surfin' Word

6

1

Your goal is to map a piece of text to a sine curve of the form:

a sin ( mx - b )

Where a and m are non-zero rational numbers, b is a rational number, and the calculations are in radians.

It doesn't really matter where the origin of your graph is, as long as you fit all the letters in your output. Also, it must be ASCII art, no graphical output.

You can take the equation input in as a string OR you can have a separate input for each parameter: a, m, and b.

Also

Examples:

Input: "A-well-a", "3sin(x)" or (3,1,0)

Output:

 -w
       a

A  e       
      -
    l
     l

Explanation:

The index of the character is the x-value, and each line represents one y-value. So we take 3sin(x) of the appropriate indices and round them to the nearest integer to get our values:

  'A' : 3sin(0) = 0 => (0,0)
  '-' : 3sin(1) = 2.5244 => (1,3)
  'w' : 3sin(2) = 2.7279 => (2,3)
   etc. , to give you the rest of the word.

Input: "Everybody's", "3.5sin(x-2)" or (3.5,1,2)

Calculations:

3.5sin(x-2), x = 0,1,2,3,...,10 -3.1815,-2.9435,0,2.9435,3.1815,0.4935,-2.6495,-3.3565,-0.9765,2.2995,3.4615,

Output:

   ry     s
         '

  e  b
        y

Ev    od

Input: "heard", "4sin(2x)" or (4,2,0)

4sin(2x) [0-4] = 0,3.636,-3.028,-1.116,3.956,

Output:

 e  d



h
   r

  a

Input: "about","-5sin(x+1)" or (-5,1,-1)

-5sin(x+1) [0-4] : -4.205,-4.545,-0.705,3.785,4.795,

Output:

    t
   u




  o


a
 b

Input: "the word", "-2.4sin(0.5x-1.2)" or (-2.4,0.5,1.2)

-2.4sin(0.5x-1.2) [0-7] : 2.2368,1.5456,0.4776,-0.7104,-1.7208,-2.3136,-2.3376,-1.7904

Output:

th

  e

    word

See also

geokavel

Posted 2016-01-13T17:33:21.647

Reputation: 6 352

1What is the input format? Do we need to parse the expression as a string? – Zgarb – 2016-01-13T17:53:37.513

@Zgarb Actually, if your language supports abstract mathematical functions, you can use that datatype for the expression. – geokavel – 2016-01-13T18:02:18.320

so the input format is flexible? for instance, I could take the function input (in python) as "3*math.sin(x)"? – quintopia – 2016-01-13T18:19:06.103

@quintopia No, only if your language natively supports mathematical expressions as a dataype (like MATLAB or Mathematica). – geokavel – 2016-01-13T18:23:05.827

Otherwise, we have to take the input as a string exactly in the format you described? – quintopia – 2016-01-13T21:34:57.847

@quintopia Ok, it finally hit me what people may have been wondering about, so I have loosened the input rules. – geokavel – 2016-01-13T21:53:17.813

Can we accept the input just separated by spaces? word a m b EDIT: just realized this would break with words that have spaces in them, so just "word" a m b? – JuanPotato – 2016-01-13T23:05:12.643

@Juan yes that'll be alright – geokavel – 2016-01-13T23:09:07.887

Related. – Martin Ender – 2016-07-08T12:51:10.780

Answers

3

Dyalog APL, 37 bytes

{⍉↑v⌽¨⍵↑¨⍨v←(-∘1-⌈/)⌊.5+⎕×1○⎕-⍨⎕×⍳≢⍵}

Takes text as right argument, and then prompts for m, b, and a.

Adám

Posted 2016-01-13T17:33:21.647

Reputation: 37 779

1

Python 2, 190 159 171 bytes

import math
a,b,c,d=input()
q=len(a)
s=""
m=int(abs(b)+.5)
for i in range(2*m*q+q):s+=""if i%q else"\n";s+=a[i%q]if i/q==-int(round(b*math.sin(i%q*c-d)))+m else" "    print s

Example usage:

$ python2 surfin2.py
"about",-5,1,-1

    t
   u




  o


a
 b

This can probably be golfed further, but I can't see how at the moment. I just wanted to see this question get at least one answer. EDIT: Decently golfed once I saw how to do it in one pass. May have extra leading and trailing lines, but such was expressly allowed: "It doesn't really matter where the origin of your graph is, as long as you fit all the letters in your output."

quintopia

Posted 2016-01-13T17:33:21.647

Reputation: 3 899

Yeah, I wanted one too! – geokavel – 2016-01-14T05:26:56.217

I also wrote a version that parses the expression from a string in the format you describe, but it was longer. – quintopia – 2016-01-14T05:29:24.763

You can include it if you want. – geokavel – 2016-01-14T05:29:53.127

Nah, it wasn't particularly well-golfed. I stopped once I realized it would be considerably longer. – quintopia – 2016-01-14T05:45:20.843