Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. /*
  2. Generates a list of width * height integers in order, shuffles the list,
  3. then iterates through the list to fill in all x,y positions on the screen
  4. "randomly" without having to track which points have already been filled.
  5.  
  6. Will be useful technique for selecting "random" points from pixel arrays.
  7.  
  8. To do:
  9. - Draw more than one point per frame.
  10. - Reset the list when screen filled and repeat.
  11. - "Zoom in", reduce list dimensions and use rect() instead of point() to
  12. draw "points" on the screen.
  13. */
  14.  
  15. // Dimensions of the list.
  16. int w = 20;
  17. int h = 20;
  18.  
  19. // Quick lookup for total size of list.
  20. int list_size = w*h;
  21.  
  22. // "Screen muliplier"
  23. int sm = 20;
  24.  
  25. // List index incrementor.
  26. int inc = 0;
  27.  
  28. // Mini-loop (e.g. five at a time) incrementor.
  29. // Careful this mini-loop doesn't case an array out of bounds error.
  30. int miniloop = 20;
  31.  
  32. // Quick lookup for total colour range.
  33. int hue_max = 6;
  34.  
  35. // Color incrementor.
  36. int hue = 0;
  37.  
  38. // Placeholder for current x, y and list index in the loop.
  39. int x, y, index;
  40.  
  41. // List of integers.
  42. IntList pos;
  43.  
  44. void settings() {
  45. size(w * sm, h * sm);
  46. noSmooth();
  47. }
  48.  
  49. void setup() {
  50. // Create, fill and shuffle the contents of the list of integers.
  51. fillList();
  52.  
  53. noStroke();
  54. frameRate(60);
  55. colorMode(HSB,hue_max);
  56. background(0,3,6);
  57. hue++;
  58. incFill();
  59. }
  60.  
  61. void draw() {
  62. // If we've interated through the list, handle it.
  63. if (inc >= list_size) {
  64. inc = 0;
  65. fillList();
  66. incFill();
  67. }
  68.  
  69. // X at a time to speed things up.
  70. for (int i=0; i < miniloop; i++) {
  71. // Offsets!
  72. // Get current index's value from the list.
  73. index = pos.get(inc);
  74. // Extract the x position.
  75. x = index % w;
  76. // Extract the y position.
  77. y = int(index / w);
  78. // Draw the "point", multiplied to "zoom in" on the screen.
  79. rect(x * sm, y * sm, sm, sm);
  80. // Increment the current index.
  81. inc++;
  82. }
  83.  
  84. //saveFrame("frames/randomrgb-####.tga");
  85. if (frameCount >= 120) {
  86. exit();
  87. }
  88. }
  89.  
  90. void fillList() {
  91. pos = new IntList();
  92. for (int i=0; i < list_size; i++) {pos.append(i);}
  93. pos.shuffle();
  94. pos.shuffle();
  95. }
  96.  
  97. void incFill() {
  98. // If we've interated through the hue list, restart it.
  99. if (hue >= hue_max) {
  100. hue = 0;
  101. }
  102. fill(hue,3,6);
  103. hue++;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement