Tut­o­r­i­a­l Week 2

by David Wiggins
  • Show Sketch
/** @peep sketchcode */
float x = 0;
float y = 0;
float squareRoot = 0;
float xMinusA = 0;
float aParameter = 250;
float bParameter = -250;
float speed = 1.0;
float radius = 15.0;
boolean reverseDirection = false;
 
float SINX = 0;
float SINY = 0;
void setup() {
  size(500, 500);
  background(0);
  noStroke();
  ellipseMode(RADIUS);
}
 
void draw() {
  fill(0,6);
  rect(0,0,width,height); //drawing rectangle over entire canvas of low opacity
  fill(255);
  quad(0,251,500,251,500,249,0,249);//Debugging purposes, treating this line as a pseudo x-axis
  translate(SINX, SINY); //Moves ellipse by a factor of x = SINX and y = SINY
  ellipse(x,y,radius,radius); //Ellipse created every call of draw() with position x and y
 
  if(!reverseDirection){
    SINX += radians(speed); //for radian calculations
    x += 1; //for normal calculations
    xMinusA = x - aParameter; //Calculation part of the definition of a circle
    squareRoot = pow(250,2) - pow(xMinusA,2); //The square root part of the mathematical semicircle function
    y = -sqrt(squareRoot) - bParameter; //The function of x which defines the negative part of the semicircle (negative as the y axis is backwards)
  } else {
    SINX -= radians(speed);
    x -= 1; //x now reduces so ellipse stays on screen
    xMinusA = x - aParameter;
    squareRoot = pow(250,2) - pow(xMinusA,2);
    y = sqrt(squareRoot) - bParameter; //The function of x which defines the positive part of the semicircle
  }
  SINY = -50*sin(SINX*10); //The function of x which defines the SINY value based on a sine function
 
  if(x > 500 && !reverseDirection){ //If x > 500 the ellipse cannot exist so we reverse to stop glitching and keep ellipse on screen
    reverseDirection = true; //This boolean is the variable which defines whether the ellipse needs to turn around or not
  }
  if(x <= 0 && reverseDirection){
    reverseDirection = false;
  }
  //println("Value of x: " + x);//Debugging purposes
  //println("Value of y: " + y);//Debugging purposes
}

Comments

Nobody has said anything yet.