Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print numerals in order in a sine wave

Tags:

java

Background:

I've successfully written code that generates a sine wave from 0 to 2pi. Adjusting the constants xPrecision and yPrecision, you can stretch the graph horizontally or vertically.

I gain this neat output (in Eclipse), when xPrecision = yPrecision = 10:

enter image description here

My query:

I now wish to display digits 0 to 9 instead of the stars. So, the leftmost star is replaced by 0, the second left-most star is replaced by 1, and so on. When you reach 9, the next digit is again zero.

I am clueless as to how to do this. I have looked at wave patterns like this, but they are fixed width patterns, while mine is scalable.

The only way I can think of is converting my output to a 2D character array, then scraping the *s manually from left to right, and replacing them with the digits, and then printing it. However, this is extremely memory consuming at bigger values of x/yPrecision.

What is the most optimized way to achieve this output?


Code to print sine wave:

class sine {
  static final double xPrecision = 10.0; // (1/xPrecision) is the precision on x-values
  static final double yPrecision = 10.0; // (1/yPrecision) is the precision on y-values
  static final int PI = (int) (3.1415 * xPrecision);
  static final int TPI = 2 * PI; // twice PI
  static final int HPI = PI / 2; // half PI

  public static void main(String[] args) {
    double xd;

    for(int start = (int) (1 * yPrecision), y = start; y >= -start; y--){       
      double x0 = Math.asin(y / yPrecision),
            x1 = bringXValueWithinPrecision(x0),
            x2 = bringXValueWithinPrecision(x0 + TPI / xPrecision),
            x3 = bringXValueWithinPrecision(PI/xPrecision - x0);

      // for debug
      //System.out.println(y + " " + x0 + " " + x1 + " " + x2 + " " + x3);

      for(int x = 0; x <= TPI; x++){
        xd = (x / xPrecision);

        if(x1 == xd || x2 == xd || x3 == xd)
          System.out.print("*");
        else System.out.print(" ");      
      }

      System.out.println();
    }
  }

  public static double bringXValueWithinPrecision(double num){
      // obviously num has 16 floating points
      // we need to get num within our precision
      return Math.round(num * xPrecision) / xPrecision;
  }
}
like image 328
Gaurang Tandon Avatar asked Jan 11 '18 13:01

Gaurang Tandon


People also ask

How do you print a wave form?

To get the desired waveform for a given matrix, first, print the elements of the first column of the matrix in the downward direction and then print the elements of the 2nd column in the upward direction, then print the elements in the third column in the downward direction and so on.


1 Answers

If you replace:

System.out.print("*");

with

System.out.print(""+(x%10));

it seems to nearly work.

               56                                              
           1        0                                          
         9            2                                        
        8              3                                       
      6                  5                                     
     5                    6                                    
    4                      7                                   
   3                        8                                  
  2                          9                                 
 1                            0                                
0                              1                              2
                                2                            1 
                                 3                          0  
                                  4                        9   
                                   5                      8    
                                    6                    7     
                                     7                  6      
                                       9              4        
                                        0            3         
                                          2        1           
                                              67               

Perhaps some further adjustments might get it perfect.

like image 55
OldCurmudgeon Avatar answered Oct 24 '22 00:10

OldCurmudgeon