fork download
  1. import static java.lang.Math.sin;
  2. import static java.lang.Math.PI;
  3. import static java.lang.Math.abs;
  4.  
  5. class Sine {
  6.  
  7. private static Integer points = 30; // points on x and y axis
  8.  
  9. public static void main(String[] args) {
  10.  
  11. // contains graph points
  12. Boolean[][] graph = new Boolean[points + 1][points + 1];
  13.  
  14. for (Double x = 0.0; x <= points; x++) {
  15. // x axis pi value
  16. Double pi = (x / points) * 2 * PI;
  17. // sin(x) plot for x
  18. Integer sinx = (int) Math.round((sin(pi) * points / 2) + points / 2);
  19. graph[sinx][x.intValue()] = true;
  20. }
  21.  
  22. for (Integer i = 0; i <= points; i++) {
  23. for (Integer j = 0; j <= points; j++) {
  24.  
  25. // space characters on x axis
  26. Integer pt = (int) Math.floor(Math.log10(points) + 1);
  27. String space = String.format("%" + pt + "s", " ");
  28.  
  29. // padding for p
  30. String p = String.format("%0" + (pt) + "d", abs(i - points / 2) * 2);
  31.  
  32. System.out.print(graph[i][j] != null ? p : space);
  33. }
  34. System.out.println();
  35. }
  36.  
  37. }
  38.  
  39. }
  40.  
Success #stdin #stdout 0.2s 32796KB
stdin
Standard input is empty
stdout
                                            3030              
                                          28    28            
                                        26        26          
                                                              
                                      22            22        
                                                              
                                    18                18      
                                                              
                                                              
                                  12                    12    
                                                              
                                                              
                                06                        06  
                                                              
                                                              
00                            00                            00
                                                              
                                                              
  06                        06                                
                                                              
                                                              
    12                    12                                  
                                                              
                                                              
      18                18                                    
                                                              
        22            22                                      
                                                              
          26        26                                        
            28    28                                          
              3030