Advertisement
B1KMusic

Simple Noise Map Generator (noisemap.c)

Apr 16th, 2018
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. /* simple noise map generator
  2.  * Usage: noisemap <size> <saturation>
  3.  *     size:       1..MAXSIZE (default = 5)
  4.  *     saturation: 1..100     (default = 50%)
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10.  
  11. #define DRAWCHAR "#. "
  12. #define MAXSIZE 50
  13. #define PERCENT_SATURATION 50
  14.  
  15. #define min(a,b) \
  16.     ( (a) < (b) ? (a) : (b) )
  17.  
  18. #define max(a,b) \
  19.     ( (a) > (b) ? (a) : (b) )
  20.  
  21. #define minmax(a,b,c) \
  22.     ( max((a), min((b), (c))) )
  23.  
  24. #define setint(lvalue, arg) \
  25.     ( (lvalue = atoi(argv[arg])), 1 )
  26.  
  27. static int saturation = PERCENT_SATURATION;
  28.  
  29. void
  30. draw_row(int size, int rownum)
  31. {
  32.     printf("%2i ", rownum);
  33.  
  34.     while(size--)
  35.         printf("%c%c", DRAWCHAR[rand() % 100 > saturation], DRAWCHAR[2]);
  36.  
  37.     putchar('\n');
  38. }
  39.  
  40. void
  41. draw_header(int size)
  42. {
  43.     fputs("   ", stdout);
  44.  
  45.     for(int i = 1; i <= size; i++)
  46.         if(i % 10 == 0)
  47.             printf("%i ", i / 10);
  48.         else
  49.             fputs("  ", stdout);
  50.  
  51.     fputs("\n   ", stdout);
  52.  
  53.     for(int i = 1; i <= size; i++)
  54.         printf("%i ", i % 10);
  55.  
  56.     putchar('\n');
  57. }
  58.  
  59. int
  60. main(int argc, char **argv)
  61. {
  62.     int size = 5;
  63.  
  64.     if(argc > 1 && setint(size, 1))
  65.         size = minmax(1, MAXSIZE, size);
  66.  
  67.     if(argc > 2 && setint(saturation, 2))
  68.         saturation = minmax(1, 100, saturation);
  69.  
  70.     srand(time(NULL));
  71.     draw_header(size);
  72.  
  73.     for(int i = 1; i <= size; i++)
  74.         draw_row(size, i);
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement