// draw a shape using vector points // as the corners (veteces) size(500, 500); background(255); // set the colour of the edge // and inside of the shape strokeWeight(5); stroke(0); fill(127); // draw the shape // beginShape and endShape // tells processing that you are drawing // a shape // each vertex call creates a corder point // CLOSED says that it is a close shape // (it joins up) beginShape(); vertex(100, 100); vertex(400, 200); vertex(300, 400); vertex(50, 300); endShape();//CLOSE); // a ball that moves with a given velocity // the position of the ball // this is a position vector float ballx, bally; // the velocity of the ball // this is a direction vector float velx, vely; void setup() { size(500, 500); // randomly setup the initial position // and velocity of the ball ballx = random(width); bally = random(height); velx = random(-1.0, 1.0); vely = random(-1.0, 1.0); } void draw() { // update the position ballx += velx; bally += vely; // draw background(255); strokeWeight(4); ellipseMode(CENTER); ellipse(ballx, bally, 10, 10); } // two balls that move in the same direction // but at different speeds // the direction and ball positions float dirx, diry, ball1x, ball1y, ball2x, ball2y; // the speeds of the two balls float speed1 = 0.2, speed2 = 0.5; void setup() { size(500, 500); // both balls start at the top left ball1x = 0.0; ball1y = 0.0; ball2x = 0.0; ball2y = 0.0; // the direction dirx = 3.0; diry = 2.0; // normalise it (divide it by its length) // to make it a proper direction float len = sqrt(dirx*dirx + diry*diry); dirx = dirx/len; diry = diry/len; } void draw() { background(255); ellipseMode(CENTER); // the velocity of each ball is its speed times // the shared direction // add this to the position of each ball ball1x += speed1*dirx; ball1y += speed1*diry; ball2x += speed2*dirx; ball2y += speed2*diry; // draw the balls ellipse(ball1x, ball1y, 10, 10); ellipse(ball2x, ball2y, 10, 10); } // a ball moves at constant speed between two points // the start and end point, position of the ball and its speed float startx, starty, endx, endy; float ballx, bally; float speed; void setup() { size(500, 500); // initialize the start and end points to random positions // on the screen startx = random(width); starty = random(height); endx = random(width); endy = random(height); // set the ball position to the start point ballx = startx; bally = starty; speed = 0.5; } void draw() { background(255); // find the direction between the start and // end point float xdisp = endx - ballx; float ydisp = endy - bally; float distance_to_end = sqrt(xdisp*xdisp + ydisp*ydisp); if(distance_to_end > 0.1) { // first find the displacement by subtracting // the start from the end float dirx = endx - startx; float diry = endy - starty; // find the length of the displacement float len = sqrt(dirx*dirx + diry*diry); // divide the dispacement by its length to get the direction dirx = dirx/len; diry = diry/len; // multiply the direction by the speed // to get the velocity // add this to the balls position ballx += dirx*speed; bally += diry*speed; } // draw a line between the start and end point line(startx, starty, endx, endy); // draw the ball ellipseMode(CENTER); ellipse(ballx, bally, 10, 10); } // translate, rotate and scale a rectangle size(500, 500); background(255); // make sure that it rotates about // the centre of the rectangle rectMode(CENTER); // try changing the order of transforms translate(200, 300); rotate(radians(23)); // non uniform scale // scale x and y differently scale(2, 0.5); // draw the rectangle rect(0,0, 100, 100); // translate, rotate and scale 2 rectangles // using pushMatrix and popMatrix size(500, 500); background(255); // make sure that it rotates about // the centre of the rectangle rectMode(CENTER); // create a new matrix and put // it on the stack pushMatrix(); translate(200, 300); rotate(radians(23)); // non uniform scale // scale x and y differently scale(2, 0.5); // draw the rectangle rect(0,0, 100, 100); // comment out the next line to see the difference popMatrix(); // the transforms are now gone // the following transforms are // unaffected by the previous ones pushMatrix(); translate(100, 100); rotate(radians(-10)); scale(1.5, 1); // draw the rectangle rect(0,0, 100, 100); popMatrix(); // translate a 3D box // import the opengl library (to do 3D) import processing.opengl.*; // create a 3D screen using OPENGL size(500, 500, OPENGL); background(255); // translate with an x,y and z translate(200, 300, 10); //rotateY(0.5); // draw a box height, width, depth box(50, 50, 100);