int x; int y; int r; // radius int direction_x; int direction_y; int speed; void setup(){ size(500,500); smooth(); x = int(random(r,(width-r))); y = int(random(r,(height-r))); r = 15; speed = 5; direction_x = 1; direction_y = 1; ellipseMode(CENTER); fill(random(0,255),random(0,255),random(0,255)); } void draw(){ background(100,100,100); ellipse(x,y,r*2,r*2); x+=(speed * direction_x); y+=(speed * direction_y); if(x+r > width){ direction_x = -1; }else if(x-r < 0){ direction_x = 1; } if(y+r > height){ direction_y = -1; }else if(y-r < 0){ direction_y = 1; } }
String[] names; // ... then later in our setup names = new String[5];
String[] names; // ... then later in our setup names = new String[5]; // ... later in our code to set a value at index 0 names[0] = "joe"; // ... and to retrieve index 0 pintln(names[0]); // prints "joe" to our console
String[] names; // ... then later in our setup names = new String[5]; names[0] = "joe"; names[1] = "sarah"; names[2] = "jim"; names[3] = "cassandra";
for(int i=0; i < 4; i++){ println(i); } // this will print 0 1 2 3 in our console
// names.length is a convenient way to specify the length // of an array instead of having to hard code the value at 5 for(int i=0; i < names.length; i++){ println(names[i]); } // this will print joe sarah jim cassandra in our console
class Ball{ int x; int y; // equivalent to setup Ball(){ x = 50; y = 50; } void draw(){ ellipse(x,y,50,50); } }
class Ball{ int x; int y; // equivalent to setup Ball(){ x = 50; y = 50; } void draw(){ ellipse(x,y,50,50); } } // create a new variable with the type Ball Ball myNewBall; void setup(){ size(500,500); // assign our variable the value of a new ball // this creates a new unique instance of our ball class myNewBall = new Ball(); } void draw(){ // use the Ball class draw() method myNewBall.draw(); }
class Ball{ int x; int y; int r; // radius int direction_x; int direction_y; int speed; int ballRed, ballGreen, ballBlue; Ball(){ x = int(random(r,(width-r))); y = int(random(r,(height-r))); r = 15; speed = 5; direction_x = 1; direction_y = 1; ballRed = int(random(0,255)); ballGreen = int(random(0,255)); ballBlue = int(random(0,255)); } void draw(){ x+=(speed * direction_x); y+=(speed * direction_y); if(x+r > width){ direction_x = -1; } else if(x-r < 0){ direction_x = 1; } if(y+r > height){ direction_y = -1; } else if(y-r < 0){ direction_y = 1; } fill(ballRed,ballBlue,ballGreen); ellipse(x,y,50,50); } } Ball myNewBall; void setup(){ size(500,500); smooth(); ellipseMode(CENTER); myNewBall = new Ball(); } void draw(){ background(80,80,80); myNewBall.draw(); }
class Ball{ int x; int y; int r; // radius int direction_x; int direction_y; int speed; int ballRed, ballGreen, ballBlue; Ball(){ x = int(random(r,(width-r))); y = int(random(r,(height-r))); r = 15; speed = 5; direction_x = 1; direction_y = 1; ballRed = int(random(0,255)); ballGreen = int(random(0,255)); ballBlue = int(random(0,255)); } void draw(){ x+=(speed * direction_x); y+=(speed * direction_y); if(x+r > width){ direction_x = -1; } else if(x-r < 0){ direction_x = 1; } if(y+r > height){ direction_y = -1; } else if(y-r < 0){ direction_y = 1; } fill(ballRed,ballBlue,ballGreen); ellipse(x,y,50,50); } } // set up a variable that will be where we hold the balls Ball[] ballContainer; void setup(){ size(500,500); smooth(); ellipseMode(CENTER); // create a container with 300 balls in it ballContainer = new Ball[300]; for(int i=0; i < ballContainer.length; i++){ ballContainer[i] = new Ball(); } } void draw(){ background(80,80,80); for(int i=0; i < ballContainer.length; i++){ ballContainer[i].draw(); } }
Ball(){
x = int(random(r,(width-r)));
y = int(random(r,(height-r)));
r = int(random(1,30));
speed = int(random(1,5));
direction_x = round(random(0,1))==0? -1:1;
direction_y = round(random(0,1))==0? -1:1;
ballRed = int(random(0,255));
ballGreen = int(random(0,255));
ballBlue = int(random(0,255));
}
float min_mass = 0.75; float max_mass = 5.0; float gravity = 1.0; // assume y only float drag = -0.03; float velocity = 40; class Ball{ float mass; float x,y; float velx,vely; float accelx,accely; float bounce=1.0; float diameter; int r,g,b; Ball(){ x=random(0,width); y=random(0,height); mass=random(min_mass,max_mass); velx = random(-velocity,velocity); // initial velocity vely = random(-velocity,velocity); diameter = mass * 10; r = int(random(0,255)); g = int(random(0,255)); b = int(random(0,255)); } void update(){ // calculate acceleration float gravity_force = gravity / mass; float accelx = (drag * velx) / mass; float accely = gravity_force + (drag * vely) / mass; // add acceleration to velocity velx += accelx; vely += accely; // update location x += velx; y += vely; // reset acceleration accelx=0.0; accely=0.0; // check borders if((y + diameter/2) > height) { vely *= -bounce; y = height - diameter/2; } if((x + diameter/2) > width) { velx *= -bounce; x = width - diameter/2; }else if((x - diameter/2) < 0){ velx *= -bounce; x = 0 + diameter/2; } } void draw(){ ellipseMode(CENTER); //stroke(0); noStroke(); fill(r,g,b); ellipse(x,y,mass*10,mass*10); } } Ball[] ballCollection = new Ball[100]; void setup(){ size(500,500); smooth(); for(int i=0; i < ballCollection.length; i++){ ballCollection[i] = new Ball(); } } void draw(){ for(int i=0; i < ballCollection.length; i++){ ballCollection[i].update(); ballCollection[i].draw(); } } void mouseClicked() { for(int i=0; i < ballCollection.length; i++){ ballCollection[i].velx = random(-velocity,velocity); ballCollection[i].vely = random(-velocity,velocity); } }