Friday, March 6, 2015

Modified Cats Cradle

On March 5, 2015 I turned in this assignment using the knowledge we had learned in class about  if statements and for loops. Essentially all I had to do was modify a code we had already done in class. My previous blog post, "CSE Cats Cradle" is the code that I was expected to modify. Our assignment was is down below.

To exceed expectations, complete all 3. To meet expectations, complete 2. To approach expectations, complete 1.

  1. Create a yarn art/cat’s cradle project that can color in the randomly created polygons.
  2. Create a yarn art/cat’s cradle project where the points are animated and move around the screen with the lines stillCONNECTED.
  3. Create a yarn art/cat’s cradle project where the user can move the points around theWINDOW by clicking and dragging them (the lines should still be connected).
I attempted to do all three of the assignments but was only able complete two, creating a cat cradle that could color in randomly created polygons and to create a cats cradle that had animated points that moved around the screen. Below is the codes I created.

                                                                Cats Cradle Animation
// cat's cradle
int size;
int movex=(2);
int movey=(3);
float[] xPos;
float[] yPos;

void setup() {
 size(850, 700);
 background(0);
 noFill();

 strokeWeight(2);

 size = 3;
 xPos = new float[size];
 yPos = new float[size];

 for (int i = 0; i < size; i++) {
   xPos[i] = random(width);
   yPos[i] = random(height);
 } // end of for
} // end of setup

void draw() {
 background(0);

 for (int i = 0; i < size - 1; i++) {
   for (int j = i + 1; j < size; j++) {

     strokeWeight(5);
     stroke(random(256), random(256), random(256));
     smooth();
     line(xPos[i], yPos[i], xPos[j], yPos[j]);

     xPos[i] = xPos[i] + movex;
     yPos[j] = yPos[j] + movey;

     // (Too far right)
     if (xPos[i] > width) {
       xPos[i]=width;
       movex=-movex;
     }
     // (Too far left)
     if (xPos[i] < 0) {
       xPos[i]=0 ;
       movex = -movex;
     }
     //(Too far down)
     if (yPos[i] > height) {
       yPos[i]=height;
       movey = -movey;
     }
     //(Too far up)
     if (yPos[i] < 0) {
       yPos[i]=0;
       movey = -movey;

       if (xPos[j] > width) {
         xPos[i]=width;
         movex=-movex;
       }
       // (Too far left)
       if (xPos[j] < 0) {
         xPos[j]=0 ;
         movex = -movex;
       }
     }
   }
 } // end of inner for
 // end of outer for
} // end of draw

                                                            Cats Cradle Colored in Polygons
                                                          
// cat's cradle

int size;
float[] xPos;
float[] yPos;
PShape s;

void setup() {
 size(800, 600, P2D);
 background(0);
 strokeWeight(5);
 stroke(255);
 smooth();
 //strokeWeight(2);

 size = 4;
 xPos = new float[size];
 yPos = new float[size];
 s = createShape( );
 s. beginShape();
 s. fill(#EA0909);
 s. noStroke();

 for (int i = 0; i < size; i++) {
   xPos[i] = random(width);
   yPos[i] = random(height);
   s.vertex(xPos[i], yPos[i]);
 } // end of for
 s.endShape(CLOSE);
} // end of setup

void draw() {
 background(0);
 shape(s, 0, 0);
 for (int i = 0; i < size - 1; i++) {
   for (int j = i + 1; j < size; j++) {
     line(xPos[i], yPos[i], xPos[j], yPos[j]);
   } // end of inner for
 } // end of outer for
} // end of draw




Wednesday, March 4, 2015

CSE Cats Cradle

On February 26, 2015 my teacher showed my class how to make a Cats Cradle so that we could later alter the code to make the cats cradle behave differently. Below is the code that my teacher taught to the class.

// cat's cradle
int size;
float[] xPos;
float[] yPos;

void setup() {
 size(800, 600);
 background(0);
 strokeWeight(5);
 stroke(160, 40, 160);
 smooth();
 //strokeWeight(2);

 size = 5;
 xPos = new float[size];
 yPos = new float[size];

 for (int i = 0; i < size; i++) {
   xPos[i] = random(width);
   yPos[i] = random(height);
 } // end of for
} // end of setup

void draw() {

 for (int i = 0; i < size - 1; i++) {
   for (int j = i + 1; j < size; j++) {
     line(xPos[i], yPos[i], xPos[j], yPos[j]);
   } // end of inner for
 } // end of outer for
} // end of draw


Sunday, March 1, 2015

CSE Stars

In my previous post I mentioned that my class was learning about Arrays and how to use them. In order for us to get a better understanding of how Arrays worked my teacher had us take notes on how to create a series of stars that outward. Below is the notes on how to create  a series of stars that move outward using Arrays.

//Creating stars using arrays
//int = whole number
//float = decimal
//int size = 50;

int size = (int)random(250, 500);
float[] xStars = new float[size];
float[] yStars = new float[size];


void setup() {
 size(200, 200);
 background(0);
 stroke(255, 255, 200);

 for (int i = 0; i < size; i++) {
   xStars[i] = random(width);
   yStars[i] = random(height);
 }
}

void draw() {
 background(0);

 // draw the stars
 for (int i = 0; i < size; i++) {
   // update star position
   if (xStars[i] > width/2) {
     xStars[i]++;
   } else {
     xStars[i]--;
   }

   if (yStars[i] > height/2) {
     yStars[i]++;
   } else {
     yStars[i]--;
   }

   if (xStars[i] > width || xStars[i] < 0  ||
     yStars[i] > height || yStars[i] < 0) {
     xStars[i] = random(width);
     yStars[i] = random(height);
   }

   strokeWeight((int)random(2, 5));
   point(xStars[i], yStars[i]);
 }
}

void mousePressed() {
 for (int i = 0; i < size; i++) {
   xStars[i] = random(width);
   yStars[i] = random(height);
 }
}