Facebook
From Adjowa Asamoah, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 151
  1. size(800, 600); // Adjusted height to 600
  2. background(255);
  3.  
  4. // Define variables
  5. float numBars = 10.0;
  6. int gridSize = 10;
  7. float sum = 0.0;
  8. float mean = 0.0; // Adjusted mean value
  9. float xOrigin = width * 0.1;
  10. float yOrigin = height * 0.2;
  11. float xEnd = width * 0.9;
  12. float yEnd = height * 0.9;
  13. float yLabelOffset = height * 0.04;
  14. float xLabelOffset = width * 0.04;
  15. float titleX = width * 0.5;
  16. float titleY = height * 0.1;
  17. float smallOffset = height * 0.03;
  18. float titleFontSize = width * 0.025;
  19. color barColor = color(100, 200, 100); // Greenish color
  20.  
  21. // Define grid and bar dimensions
  22. float areaWidth = xEnd - xOrigin;
  23. float areaHeight = yEnd - yOrigin;
  24. float gridWidth = areaWidth / gridSize;
  25. float gridHeight = areaHeight / gridSize;
  26. float maxBarHeight = yEnd - yOrigin;
  27. float xValue;
  28. float barStart = yOrigin;
  29. float barHeight = maxBarHeight / numBars;
  30. float barLength;
  31.  
  32. // Generate random data for bars
  33. int[] bars = {5, 7, 2, 9, 4, 8, 3, 6, 10, 1};
  34.  
  35. // Add title
  36. fill(0);
  37. String title = "Graph Visualization";
  38. textSize(titleFontSize);
  39. textAlign(CENTER);
  40. text(title, titleX, titleY);
  41.  
  42. // Draw grid lines
  43. noFill();
  44. strokeWeight(1);
  45. for (float x = xOrigin; x < xEnd; x += gridWidth) {
  46.   for (float y = yOrigin; y < yEnd; y += gridHeight) {
  47.     rect(x, y, gridWidth, gridHeight);
  48.   }
  49. }
  50.  
  51. // Draw x and y axes
  52. strokeWeight(5);
  53. strokeCap(ROUND);
  54. line(xOrigin, yEnd, xEnd, yEnd); // x axis
  55. line(xOrigin, yOrigin, xOrigin, yEnd); // y axis
  56.  
  57. // Add x-axis labels
  58. for (int i = 0; i <= gridSize; i++) {
  59.   String label = str(i);
  60.   text(label, xOrigin + i * gridWidth, yEnd + xLabelOffset);
  61. }
  62.  
  63. // Draw bars and labels
  64. strokeWeight(1);
  65. for (int i = 0; i < numBars; i++) {
  66.   fill(barColor); // Adjusted color
  67.   barLength = (bars[i] * gridWidth) / 2; // Adjusted width of the rectangles
  68.   rect(xOrigin, barStart + (i * barHeight), barLength, barHeight);
  69.  
  70.   // Add bar value labels
  71.   fill(255);
  72.   String label = str(bars[i]);  
  73.   text(label, xOrigin + barLength - smallOffset,
  74.        barStart + (i * barHeight) + (barHeight / 1.5 ));
  75. }
  76.  
  77. // Calculate and draw mean line
  78. for (int i = 0; i < numBars; i++) {
  79.   sum += bars[i];
  80. }
  81. mean = sum / numBars; // Adjusted mean calculation
  82. stroke(235, 16, 13);
  83. strokeWeight(3);
  84. xValue = xOrigin + (mean * gridWidth);
  85. line(xValue, yOrigin, xValue, yEnd);
  86.  
  87. // Add mean value label
  88. fill(235, 16, 13);
  89. String meanLabel = "Mean: " + mean;
  90. textSize(titleFontSize);
  91. textAlign(CENTER);
  92. text(meanLabel, xValue, height * 0.18);
  93.  
  94. save("Assignment2.png");
  95.