Advertisement
Guest User

Stack ovrflw

a guest
Apr 16th, 2019
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Global vars:
  2. listLength = 500;
  3. listLimit = 1000;
  4. bars = [];
  5. numbers = [];
  6. bigger = [];
  7. smaller = [];
  8.  
  9. //Setup function (run one time at beginning)
  10. function setup(){
  11.   //Make the canvas fill the whole screen
  12.   createCanvas(windowWidth, windowHeight);
  13.  
  14.   //Fill the [ numbers[] ] array random numbers from 0 to [ listLimit ] of length [ listLength ]
  15.   for(i = 0; i < listLength; i++){
  16.     numbers[i] = Math.round(Math.random() * listLimit);
  17.   }
  18.  
  19.   //Define the relative x and y units
  20.   xunit = windowWidth / listLength;
  21.   yunit = windowHeight / listLimit;
  22.  
  23.   numbers = csort(numbers);
  24. }
  25.  
  26. //Function continually run
  27. function draw(){
  28.   //Clear canvas each frame
  29.   clear();
  30.  
  31.   //Setup colors
  32.   fill(color(255, 204, 0));
  33.   noStroke();
  34.  
  35.   //Create the bars array at the length of [ listLength ] and with the heights of from the [ numbers[] ] array
  36.   for(i = 0; i < listLength; i++){
  37.     height = numbers[i];
  38.     bars[i] = rect((0 + xunit * i), listLimit*yunit, xunit, yunit*-height);
  39.   }
  40.    //throw new Error("Script finished! - I threw an error to stop this from eating up your CPU.")
  41. }
  42.  
  43. //Run every time window is resized
  44. function windowResized() {
  45.   //Resize canvas and redefine the x and y units.
  46.   resizeCanvas(windowWidth, windowHeight);
  47.   xunit = windowWidth / listLength;
  48.   yunit = windowHeight / listLimit;
  49. }
  50.  
  51.  
  52. function csort(array){
  53.  
  54.  
  55.   p = round((array.length-1) /2);
  56.   pvalue = array[p];
  57.   //pvalue = listLimit / 2;
  58.  
  59.  
  60.     for (i = 0; i < array.length; i++){
  61.     current = array[i];
  62.       if (current <= pvalue){
  63.         smaller.push(current);
  64.       } else {
  65.         bigger.push(current);
  66.       }
  67.     }
  68.   if (array.length > 1){
  69.      smaller = csort(smaller);
  70.      bigger = csort(bigger);
  71.   }
  72.  
  73.   array = smaller.concat(bigger);
  74.   return array;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement