Facebook
From KAMEL MUFTI, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 57
  1. size(600,600);
  2.  
  3. //Create a vertical bar graph (Assignment 3)
  4. float loffset = width/12;
  5. float hoffset = height/12;
  6. float cptloffset = loffset;
  7. float cpthoffset = hoffset;
  8.  
  9. for (int i = 0; i < 10; i++ ) {
  10.   for (int j = 0; j < 10; j++ ) {
  11.      rect(cptloffset, cpthoffset, loffset, hoffset);
  12.      cpthoffset = cpthoffset + hoffset;
  13.   }
  14.   cptloffset = cptloffset + loffset;
  15.   cpthoffset = hoffset;
  16. }
  17.  
  18. //  Initialize temporary array . used to  scale with data
  19. float[] valuestmp = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0  };
  20.  
  21. // choose 10 values
  22. float[] values = {10, 65, 30, 25, 70, 45, 90, 17,  50, 83  };
  23. //float[] values = {1, 6, 3, 2, 7, 4, 9, 1,  5, 8  };
  24. //float[] values = {111, 666, 333, 222, 777, 444, 555, 999,  555, 888  };
  25. //float[] values = {1000, 5555, 3000, 2255, 7000, 4455, 9000, 995,  4555, 8000  };
  26. //float[] values = {57, 533, 123, 300, 101, 950, 123, 800,  765, 450  };
  27.  
  28. float sum = 0;
  29. for (int i = 0; i < values.length; i++){
  30.   sum  = sum + values[i] ;
  31. }
  32. float meanfinal = (float)sum/values.length;
  33. float mean =  meanfinal;
  34. float meantmp = mean ;
  35.  
  36.  
  37. //graph  scale with the size of the canvas, and   also scale with   data.
  38. //I am able to change one of my variables and regardless of the value chosen
  39.  
  40. // Initialize maximum element
  41. float max = values[0];
  42.  
  43. for (int x = 0; x < values.length; x++)
  44.  if (values[x] > max)
  45.    max = values[x];
  46.  
  47. float maxval  = max * hoffset ;
  48.  
  49. // copy array in  another array
  50.  for (int i = 0; i < values.length; i++){
  51.       valuestmp[i] = values[i] ;
  52. }
  53.  
  54. while(maxval >= height  )
  55. {
  56.    for (int i = 0; i < valuestmp.length; i++){
  57.       valuestmp[i] = valuestmp[i] / 10;
  58.    }
  59.    meantmp = meantmp / 10;
  60.    maxval = maxval / 10 ;
  61. }
  62.  
  63. float value = 0;
  64. int cpt = 0;
  65. int coloroffset = 30;
  66. String msg = "";
  67.  
  68. for(int i = 0; i < valuestmp.length ; i++){
  69.   cpt++;
  70.   value = valuestmp[i] ;
  71.  
  72.   if ((values[i] % 2 ) == 0  ) { // If the value is even, then it should be displayed in a shade of green.
  73.        fill(0,coloroffset * value,0);
  74.        msg = " " + (int)values[i] ;
  75.        rect(cpt * loffset , (height - hoffset) - (value * hoffset), loffset,  (value * hoffset));
  76.        text(msg, cpt * loffset, (height-hoffset) - (value * hoffset));
  77.        
  78.   }
  79.   else { // If it is odd, then it should be displayed as a shade of red.
  80.       fill(coloroffset*value,0,0);
  81.       msg = " " + (int)values[i] ;
  82.       rect(cpt * loffset , (height-hoffset) - (value * hoffset), loffset,  (value * hoffset));
  83.       text(msg, cpt * loffset, (height-hoffset) - (value * hoffset));
  84.   }
  85. }
  86.  
  87. strokeWeight(5);
  88. stroke(0,0,255);
  89. line ( loffset , (height-hoffset)- (meantmp * hoffset)    , (loffset *  valuestmp.length) +loffset   ,  (height-hoffset)- (meantmp * hoffset) );
  90. msg = " " + mean ;
  91. fill(0,0,255);
  92. text(msg,  loffset*0.5 , (height-hoffset)- (meantmp * hoffset) );
  93.  
  94.  
  95.  
  96.  
  97.  
  98.