Task 1
int x = 0;
int speed = 5; // Setup does not change void setup() { size(500,500); smooth(); } void draw() { background(255); moveBall(); bounce(); displayBall(); } void moveBall(){ x += speed; } void bounce(){ if(x >= width || x < 0){ speed *= -1; } } void displayBall(){ fill(255,255,0); ellipse(x,height/2,50,50); } |
Task 2
float legDif;
void setup() { size(1000,1000); } void draw() { background(255); stickman(150,150); } void stickman(int size, int shade){ fill(shade); legDif = size/2; stroke(0); ellipse(mouseX,mouseY,size,size); line(mouseX,mouseY+ size/2,mouseX,mouseY+ 2 * size); line(mouseX,mouseY +2 * size,mouseX-legDif, mouseY +3 * size); line(mouseX,mouseY +2 * size,mouseX+legDif, mouseY +3 * size); line(mouseX,mouseY+size,mouseX - size, mouseY+1.5* size); line(mouseX,mouseY+size,mouseX + size, mouseY+1.5* size); } |
Task 3
The program works by designating a point on each square which is the corner of the program window and calculating the displacement between that point and the mouse using Pythagoras theorem. The program finds the x and y distance by taking the mouseX or mouseY and subtracting the x or y component of the point where the square starts. The function returns the displacement value which is used in conjunction with the fill command where the displacement is the colour value meaning the close the mouse is to each respective point the darker that square appears.
Final Task
float x = 500;
float y = 500;
float dX;
float dY;
boolean contact = false;
int size = 50;
int rep = 1;
int repPower = 1;
void setup() {
size(1000,1000);
}
void draw() {
background(255);
rectMode(CENTER);
square(size);
distanceX(mouseX,x);
distanceY(mouseY,y);
touch();
fill(0);
text(repPower,25,25);
text("Power of repulsion(w = increase, s = decrease)", 50,25);
text("Press the mouse to repel", 25,50);
if(contact){
fill(255,0,0);
}
else if(!contact){
fill(0,255,0);
}
if(x >= mouseX){
x -= dX/20 * rep;
}
else{
x += dX/20 * rep;
}
if(y >= mouseY){
y -= dY/20 * rep ;
}
else{
y += dY/20 * rep;
}
}
void square(int size){
rect(x,y,size,size);
}
float distanceX(int x1,float x2)
{
dX = abs(x1 - x2);
return dX;
}
float distanceY(int y1,float y2)
{
dY = abs(y1 - y2);
return dY;
}
boolean touch(){
if( x-size/2 <= mouseX && x+size/2 >= mouseX && y-size/2 <= mouseY && y+size/2 >= mouseY){
contact = true;
return contact;
}
else{
contact = false;
return contact;
}
}
void mousePressed() {
rep = -1 * repPower;
}
void mouseReleased() {
rep = 1;
}
void keyPressed() {
if(key == 'w'){
repPower++;
}
if(key =='s'){
repPower--;
}
}
float y = 500;
float dX;
float dY;
boolean contact = false;
int size = 50;
int rep = 1;
int repPower = 1;
void setup() {
size(1000,1000);
}
void draw() {
background(255);
rectMode(CENTER);
square(size);
distanceX(mouseX,x);
distanceY(mouseY,y);
touch();
fill(0);
text(repPower,25,25);
text("Power of repulsion(w = increase, s = decrease)", 50,25);
text("Press the mouse to repel", 25,50);
if(contact){
fill(255,0,0);
}
else if(!contact){
fill(0,255,0);
}
if(x >= mouseX){
x -= dX/20 * rep;
}
else{
x += dX/20 * rep;
}
if(y >= mouseY){
y -= dY/20 * rep ;
}
else{
y += dY/20 * rep;
}
}
void square(int size){
rect(x,y,size,size);
}
float distanceX(int x1,float x2)
{
dX = abs(x1 - x2);
return dX;
}
float distanceY(int y1,float y2)
{
dY = abs(y1 - y2);
return dY;
}
boolean touch(){
if( x-size/2 <= mouseX && x+size/2 >= mouseX && y-size/2 <= mouseY && y+size/2 >= mouseY){
contact = true;
return contact;
}
else{
contact = false;
return contact;
}
}
void mousePressed() {
rep = -1 * repPower;
}
void mouseReleased() {
rep = 1;
}
void keyPressed() {
if(key == 'w'){
repPower++;
}
if(key =='s'){
repPower--;
}
}