M&Ms

eminem_mms

For this week’s assignment, I tried to find a fun data set.. I ended up finding some random set a professor took of the color distribution in packs of M&Ms. I am trying to use the average number as the radius of a circle in the window to visualize the data, but right now I’m running into a null pointer exception that I’m trying to fix.

//Colors of milk chocolate M&Ms in the fun size packages
//collected by Todd Swanson and his statistics students in 2001.
//http://www.math.hope.edu/swanson/data/mms.txt

String [][] data;
float brown, red, yellow, blue, green, orange;
MandM brownM, redM, yellowM, blueM, greenM, orangeM;
float colorXpos, colorYpos;
float brownSum, redSum, yellowSum, blueSum, greenSum, orangeSum;
int xPos, yPos;

void setup() {
  //the basics
  size(640, 280);
  
  String line[] = loadStrings("mms.csv");
  data = new String [21][6];
  for (int row=0; row < 21; row++) {
    String [] temp = new String [6];
    temp= split(line[row], ',');
    for (int column=0; column < 6; column++){
        data[row][column]= temp[column];
    }
   }
 
 //Calculating the sum
  for (int row=0; row < 21; row++) {
      brownSum += parseInt(data[row][0]);
      redSum += parseInt(data[row][1]);
      yellowSum += parseInt(data[row][2]);
      blueSum += parseInt(data[row][3]);
      greenSum += parseInt(data[row][4]);
      orangeSum += parseInt(data[row][5]);
   }

 //To visualize data: average is the radius of ellipse
 //Already know there are 21 total rows
    brown = brownSum/21;
    red = redSum/21;
    yellow = yellowSum/21;
    blue = blueSum/21;
    green = greenSum/21;
    orange = orangeSum/21;
    
    for (int i = 0; i < 6; i ++){
       brownM =new MandM (random(0,width), random(0,height), brown*15);
       redM =new MandM (random(0,width), random(0,height), red*15);
       yellowM =new MandM (random(0,width), random(0,height), yellow*15);
       blueM =new MandM (random(0,width), random(0,height), blue*15);
       greenM =new MandM (random(0,width), random(0,height), green*15);
       orangeM =new MandM (random(0,width), random(0,height), orange*15);
   }
    
 

}
 
void draw () {
    background(255);
   
    MandM [] funSize = new MandM [6];
    funSize[0] = brownM;
    funSize[1] = redM;
    funSize[2] = yellowM;
    funSize[3] = blueM;
    funSize[4] = greenM;
    funSize[5] = orangeM;
   
    for (int i=0; i<6; i++){
           if (i==0){
          fill(110,70,0);
          
        }
        if (i==1){
          fill(255,0,0);
        }
        if (i==2){
          fill(255,255,0);
        }
        if (i==3){
          fill(0,0,255);
        }
        if (i==4){
          fill(0,255,0);
        }
        if (i==5){
          fill(255,150,0);
        }
      funSize[i].drawMandM();
  }
   
}

///////
class MandM {
  
  float xPos, yPos, radius;
 
  MandM(float x, float y, float radius){
   this.xPos = x;
   this.yPos = y;
   this.radius = radius;
    
  }
  
  
  void drawMandM(){
    for(int i = 0; i < 6; i ++){
        noStroke();
        smooth();
        ellipse(xPos, yPos, radius, radius);
        noLoop();
     }  
  }
}

 

Leave a Reply