Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

PVector     startPos = new PVector();
PVector     endPos = new PVector();
int         curTime = 0;
int         animSpeed = 25;
int         animTime = 2000;
boolean     pFlag1=false;
boolean     pFlag2=false;
boolean     animDir = true;
  
void setup()
{
  size(640, 480);
  smooth();
}
   
void draw()
{
  background(51);
   
  // calc. the anim time
  if(animDir)
    curTime += animSpeed;
  else
    curTime -= animSpeed;
      
  if(curTime >= animTime)
    animDir = false;
  else if(curTime <= 0)
    animDir = true;
   
  // calc. the current time in the animation
  float normTime = curTime * 1.0 / animTime;
   
  if(pFlag1 && pFlag2)
  {
    stroke(255);
    line(startPos.x,startPos.y,
         endPos.x,endPos.y);
   
    // calculate the position of the circle on the line
    PVector dir = PVector.sub(endPos,startPos);
   
    PVector pos = PVector.add( startPos , PVector.mult(dir,normTime));
    ellipse(pos.x,pos.y, 20,20);
  }
  else if(pFlag1)
  {
    stroke(255,0,0);
    line(startPos.x,startPos.y,
         mouseX,mouseY);
  }
   
}
   
void mousePressed()
{
  if(pFlag1 == false)
  {
    pFlag1 = true;
    startPos.set(mouseX,mouseY,0);
  }
  else if(pFlag2 == false)
  {
    pFlag2 = true;
    endPos.set(mouseX,mouseY,0);
  }
  else
  {
    pFlag1 = pFlag2 = false;
  }
}