How would I go about creating a smooth wave of vertical text on different lines(java)?

I’m trying to generate something similar to this as in wavy text. How would I go about doing it? The thing I tried was simply using a for loop to incresae the amount of invisible spaces before each line. the result was linear, and although that is somewhat of what I am looking for, I’m more looking for something smoother, like in the pastebin. I thought about multiplying by 0.75 or 1.25 each time, but with data types and that you can’t have a half of a character and all of that stuff in java it got really confusing really quick. Any ideas on how to do this?

This is what i’ve got so far, the linear examlple

Scanner scan = new Scanner(System.in);
        String space = "u2009";

        System.out.println("type character(s) you want to be wave-ified");
        String characters = scan.nextLine();

        for (int i = 1; i < 50; i++) {
            System.out.println(space.repeat(i) + characters);
        }```

Answer

To do a sine-wave pattern:

int maxIndent = 10;
for (float i = 1; i < 50; i += 0.1) {
    int spaces = (int)((Math.sin(i) + 1) / 2 * maxIndent);
    System.out.println(space.repeat(spaces) + characters);
}

Take the sin(i) which gives a value -1 <= n <= 1. Then add one to that to get a value 0 <= n <= 2. Then “normalize” the value by dividing by 2 which gives 0 <= n <= 1.

Then multiply that by the max and cast to int to get the correct amount of spaces. This gives 0 <= spaces <= maxIndent. Note: you may get slightly better results rounding instead of casting.

Example output with parameters maxIndent = 10 and for loop increment is i += 0.5:

         Hello, world.
         Hello, world.
         Hello, world.
       Hello, world.
     Hello, world.
   Hello, world.
 Hello, world.
Hello, world.
Hello, world.
 Hello, world.
   Hello, world.
      Hello, world.
        Hello, world.
         Hello, world.
         Hello, world.
        Hello, world.
       Hello, world.
    Hello, world.

Leave a Reply

Your email address will not be published. Required fields are marked *