Facebook
From Paride D'Ostilio, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 109
  1.  
  2. String[] getSector(){
  3.   String[] sectors ={"General public services", "Public order and safety",
  4.   "Education", "Health", "Social Security and welfare",
  5.   "Housing and community amenities", "Recreation and culture", "Fuel and energy",
  6.   "Agriculture, forestry, fishing and hunting",
  7.   "Mining and mineral resources other than fuels", "Transport and communications",
  8.   "Other economic affairs", "Other purposes"};
  9.   return sectors;
  10. }
  11. int[] getBudget() {
  12.   int[] budgets = {408, 1752, 4364, 5618, 1437, 1275, 421, 74, 213, 73, 1215, 390, 755};
  13.   return budgets;
  14. }
  15.  
  16. int sumArray(int[] values) {
  17.   int sum = 0;
  18.   for (int i =0; i< values.length; i++){
  19.     sum= sum+values[i];
  20.   }
  21.   return sum;
  22. }
  23.  
  24. void tablePrinter(int[] colors){
  25.   textSize(height/40);
  26.   int margin = 20;
  27.   String[] sectors = getSector();
  28.   int[] budgets = getBudget();
  29.   int sum = sumArray(budgets);
  30.   for (int i =0; i< sectors.length; i++){
  31.     fill(colors[i], colors[1+i], colors[2+i]);
  32.     rect(margin/2, 0 + i*(height -6*margin)/sectors.length, margin, margin);
  33.     text(sectors[i], 2*margin, margin + i*(height -6*margin)/sectors.length);
  34.     text(budgets[i], 600, margin + i*(height -6*margin)/budgets.length);
  35.   }
  36.   fill(0);
  37.   text("sum", margin, height -4*margin);
  38.   text(sum, 600, height -4*margin);
  39.   line(550,0, 550, height-margin);
  40.   line(0,height-6*margin, width/2 -margin, height -6*margin);
  41. }
  42.  
  43.  
  44. void drawPie(int[]colors){
  45.   stroke(0);
  46.   textSize(3);
  47.   strokeWeight(3);
  48.   int margin = 20;
  49.   int tab = 800;
  50.   String[] sectors = getSector();
  51.   int[] budgets = getBudget();
  52.   int sum = sumArray(budgets);
  53.   float cumulatedAngle = 0;
  54.  
  55.   for (int i=0; i < budgets.length; i++){
  56.     fill(colors[i], colors[i+1], colors[i+2]);
  57.     arc(3* width/4, height/2, width/2 - 2*margin, width/2 -2*margin, cumulatedAngle, cumulatedAngle+TWO_PI*budgets[i]/sum, PIE);
  58.     cumulatedAngle = cumulatedAngle+TWO_PI*budgets[i]/sum;
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. void setup(){
  70.   size(1600,1000);
  71.   background(255);
  72.   int[] colors = new int[39];
  73.   for (int i=0; i<39; i++){
  74.     colors[i]= int(random(0,255));
  75.   }
  76.   tablePrinter(colors);
  77.   drawPie(colors);
  78. }
  79.