processing workshop 1
Thanks to everyone who came to todays processing session.
The task I set was to make the dude that Daniel Shiffman draws in his example on the learning processing page stick to the mouse, and scale with the mouse’s position.
Not only that, but to do it in a readable way with minimal use of explicit values so that we can use it to make an object for agent simulations next week.
This isn’t easy, so use a lot of sketches, and diagrms on graph paper to help you. A programmers best tool is their brain, and then second best is a notebook!
My example of one way to do it is after the fold. I don’t expect you to solve this all on your own (but extra brownie points if you do) so go through my code and get a handle on it.
This is an example of one way (there are many) that you could have written your code to allow for the dude to be drawn
void setup() { size(400,400); rectMode(CENTER); } void draw() { //redraw the background background(255,255,255); //expose some variables int centreLineX = mouseX; int bodyCentreY = mouseY; //**\*\*\*\***some internal variables****\*\*\***** int headCentreY = bodyCentreY-30; int bodyHeight = 100; int bodyWidth = 20; int headSize = bodyWidth\*3; int eyeWidth = (headSize/3)-2; int legSize = 20; //****\*\*****draw the dude******\*\*\******* //body rect(centreLineX, //centroid X bodyCentreY, //centroid U bodyWidth, bodyHeight); //head ellipse(centreLineX, headCentreY, headSize, headSize); //left eye ellipse(centreLineX-19, headCentreY, eyeWidth, headSize/2); //right eye ellipse(centreLineX+19, headCentreY, eyeWidth, headSize/2); //left leg line(centreLineX - (bodyWidth/2), //startX bodyCentreY + (bodyHeight/2), //startY centreLineX - (bodyWidth/2) - legSize, //endX bodyCentreY + (bodyHeight/2) + legSize);//endY //right leg line(centreLineX + (bodyWidth/2), bodyCentreY + (bodyHeight/2), centreLineX + (bodyWidth/2) + legSize, bodyCentreY + (bodyHeight/2) + legSize); }
I’ve delibearly left out the scaling to keep the code readable in the version above. Here it is with the scaling involved.
void setup() { size(800,800); rectMode(CENTER); } void draw() { //redraw the background background(255,255,255); //expose some variables int centreLineX = mouseX; int bodyCentreY = mouseY; /* This is the scaling part * it is a bit tricky to figure out, but think about * it as a diagram on some graph paper. The mouseX and * mouseY numbers get extremetly large, not very far * away from the origin, so they need to be scaled down * a lot or our man becomes a tiny dot. * * The +1 is so that when mouseX or mouseY = 0 that we * don't get a 'divide by zero' error*/ int scaleX = mouseX/100 + 1; int scaleY = mouseY/100 + 1; //some internal variables int headCentreY = bodyCentreY - (30/scaleY); int bodyHeight = 100; int bodyWidth = 20; int headSize = bodyWidth * 3; int eyeWidth = (headSize/3)-2; int legSize = 20; //draw the dude //body rect(centreLineX, //centroid X bodyCentreY, //centroid U bodyWidth / scaleX, bodyHeight / scaleY); //head ellipse(centreLineX, headCentreY, headSize / scaleX, headSize / scaleY); //left eye ellipse(centreLineX - (19 / scaleX), headCentreY, eyeWidth / scaleX, headSize / 2 / scaleY); //right eye ellipse(centreLineX + (19 / scaleX), headCentreY, eyeWidth / scaleX, headSize / 2 / scaleY); //left leg line(centreLineX - (bodyWidth / 2 / scaleX), //startX bodyCentreY + (bodyHeight / 2 / scaleY), //startY centreLineX - (bodyWidth / 2 / scaleX) - legSize / scaleX, //endX bodyCentreY + (bodyHeight / 2 / scaleY) + legSize / scaleY);//endY //right leg line(centreLineX + (bodyWidth / 2 / scaleX), //startX bodyCentreY + (bodyHeight / 2 / scaleY), //startY centreLineX + (bodyWidth / 2 / scaleX) + legSize / scaleX, //endX bodyCentreY + (bodyHeight / 2 / scaleY) + legSize / scaleY);//endY }
If you’ve got this far, you’ve done very well as this is some tricky stuff!