//******************************************************************************************** //Define the setup function, which will be called once in the beginning // to set up some initial settings of the program //******************************************************************************************** void setup() { //Set the canvas size size(800, 800); //Set the background color of the canvas background(192, 64, 0); //Set the stroke color as white stroke(255, 255, 255); } //******************************************************************************************** //Declare two variables to store the X and Y coordinates of a circle and // initilize the contents to represent the point (400, 400) //******************************************************************************************** float circleX=400; float circleY=400; //******************************************************************************************** //Define the draw function, which will be called repeatedly to draw pictures on the screen //******************************************************************************************** void draw() { //Clear the canvas background(192, 64, 0); //********************************************************************* //Set the fill color to the red color, and then draw a 20 x 20 rectangle, // with (mouseX-10, mouseY-10) as the upperleft corner of the rectangle //********************************************************************* fill(255, 0, 0); rect(mouseX-10, mouseY-10, 20, 20); //********************************************************************* //Set the fill color to the green color, and then draw a circle with a radis of 20 // centered at (circleX, circleY). //********************************************************************* fill(0, 255, 0); ellipse(circleX, circleY, 15, 15); //******************************************************************************************** //Update the X and Y coordinates of a circle to move it away from the current location // of the mouse at (mouseX, mouseY), faster when they are closer. //******************************************************************************************** float distance; //Calculate the Manhattann distance where // abs(mouseX-circleX) gives the absolute value of (mouseX-circleX) // abs(mouseY-circleY) gives the absolute value of (mouseY-circleY) distance = abs(mouseX-circleX) + abs(mouseY-circleY)+ 1; circleX= circleX - 5*(mouseX-circleX)/distance; circleY= circleY - 5*(mouseY-circleY)/distance; //******************************************************************************************** //If the circle goes out of the canvas, bring it back to the middle of the canvas. //******************************************************************************************** if (circleX > 800) circleX = 400; if (circleX < 0) circleX = 400; if (circleY > 800) circleY = 400; if (circleY < 0) circleY = 400; }