Not logged in.

From instruction: Draw a straight line and follow it

Code: StraightLineAndFollow by benschroeder

Draw a black line; run a red dot along it; fade; repeat.

class Line
{
  private float drawT;
  private float drawFollowPauseT;
  private float followT;
  private float fadeT;
  
  private float x1;
  private float y1;
  private float x2;
  private float y2;
  
  public Line(float x1, float y1, float x2, float y2)
  {
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    
    drawT = 0;
    followT = 0;
    fadeT = 0;
  }
  
  public boolean done()
  {
    return fadeT >= 1.0;
  }
  
  public void draw()
  {
    noFill();
    stroke(0, 255 * (1 - fadeT));
    strokeWeight(3);
    
    line(x1, y1, x1 + (x2 - x1) * drawT, y1 + (y2 - y1) * drawT);
    
    if (drawFollowPauseT >= 1.0)
    {
      fill(color(192, 0, 0, 255 * (1 - fadeT)));
      noStroke();
      ellipse(x1 + (x2 - x1) * followT, y1 + (y2 - y1) * followT, 12, 12);
    }
  }
  
  public void step()
  {
    if (drawT < 1.0)
      drawT += 0.01;
    else if (drawFollowPauseT < 1.0)
      drawFollowPauseT += 0.05;
    else if (followT < 1.0)
      followT += 0.01;
    else if (fadeT < 1.0)
      fadeT += 0.01;
  }
}

Line activeLine;

void setup()
{
  size(400, 400);
  smooth();
  frameRate(30);
  
  activeLine = randomLine();
}

void draw()
{
  background(255);
  
  activeLine.draw();
  activeLine.step();
  
  if (activeLine.done())
    activeLine = randomLine();
}

Line randomLine()
{
  return new Line(random(width), random(height), random(width), random(height));
}

[download]

Written in Processing. Released under the MIT License license