fork download
  1. // http://d...content-available-to-author-only...o.jp/qa/question_detail/q1078453585
  2. // questioner: tobe_scorpionさん
  3. // answer: shira3
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <string.h>
  9.  
  10. #define Y_ORIGIN (10)
  11. #define X_ORIGIN (5)
  12. #define Y_WIDTH (10)
  13. #define X_WIDTH (72)
  14. #define Y_SIZE (Y_ORIGIN + Y_WIDTH + 1)
  15. #define X_SIZE (X_ORIGIN + X_WIDTH + 1)
  16. char graph[Y_SIZE][X_SIZE + 1]; /* +1 for '\0' */
  17.  
  18. void graph_init() {
  19. int y;
  20. memset(graph, ' ', sizeof(graph));
  21. for (y = 0; y < Y_SIZE; y++) {
  22. graph[y][X_ORIGIN] = '|';
  23. graph[y][X_SIZE ] = '\0';
  24. }
  25. memset( &graph[Y_ORIGIN ][X_ORIGIN + 1], '-', X_WIDTH);
  26. strncpy(&graph[Y_ORIGIN - Y_WIDTH][X_ORIGIN - 5], "+1.0 +", 6);
  27. strncpy(&graph[Y_ORIGIN ][X_ORIGIN - 5], " 0.0 +", 6);
  28. strncpy(&graph[Y_ORIGIN + Y_WIDTH][X_ORIGIN - 5], "-1.0 +", 6);
  29. strncpy(&graph[Y_ORIGIN + 1 ][X_ORIGIN + 1 ], "0" , 1);
  30. strncpy(&graph[Y_ORIGIN + 1 ][X_ORIGIN + X_WIDTH / 2 - 3], "180", 3);
  31. strncpy(&graph[Y_ORIGIN - 1 ][X_ORIGIN + X_WIDTH - 3], "360", 3);
  32. }
  33.  
  34. void graph_plot(double x, double y) {
  35. graph[Y_ORIGIN - lround(y * Y_WIDTH)][X_ORIGIN + lround(x * X_WIDTH / 360)] = '*';
  36. }
  37.  
  38. void graph_print() {
  39. int y;
  40. for (y = 0; y < Y_SIZE; y++) {
  41. printf("%s\n", graph[y]);
  42. }
  43. }
  44.  
  45. int main(int argc, char** argv) {
  46. double angle;
  47. graph_init();
  48. for (angle = 0; angle <= 360; angle += 360 / X_WIDTH) {
  49. graph_plot(angle, sin(2 * M_PI / 360 * angle));
  50. }
  51. graph_print();
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
+1.0 +              *******                                                   
     |           ***       ***                                                
     |         **             **                                              
     |        *                 *                                             
     |      **                   **                                           
     |     *                       *                                          
     |    *                         *                                         
     |  **                           **                                       
     | *                               *                                      
     |*                                 *                                 360 
 0.0 *-----------------------------------*-----------------------------------*
     |0                               180 *                                 * 
     |                                     *                               *  
     |                                      **                           **   
     |                                        *                         *     
     |                                         *                       *      
     |                                          **                   **       
     |                                            *                 *         
     |                                             **             **          
     |                                               ***       ***            
-1.0 +                                                  *******