Page 1 of 1

Processing Examples - Drawing Programs

PostPosted: Tue Nov 11, 2014 7:22 pm
by BenChang
Drawing Programs
Code: Select all
void setup()
{
   size(800,600);
}

void draw()
{
   
  if (mousePressed)
  {
    ellipse(mouseX,mouseY,20,20);
    ellipse(800-mouseX,mouseY,20,20);
    ellipse(mouseX,600-mouseY,20,20);
    ellipse(800-mouseX,600-mouseY,20,20);
  }
}

Lines

PostPosted: Tue Nov 11, 2014 7:22 pm
by BenChang
Code: Select all
[code]
void setup()
{
   size(800,600);
}

void draw()
{
   
  if (mousePressed)
  {
    line(pmouseX, pmouseY, mouseX, mouseY);
  }
}

Velocity

PostPosted: Tue Nov 11, 2014 7:22 pm
by BenChang
Code: Select all
void setup()
{
   size(800,600);
}

void draw()
{
   
  float dx;
  float dy;
 
  dx=mouseX-pmouseX;
  dy=mouseY-pmouseY;
 
  if (mousePressed)
  {
      strokeWeight (abs (dy) );
      line (pmouseX, pmouseY, mouseX, mouseY);
  }
}

Velocity, line width, distance function

PostPosted: Tue Nov 11, 2014 7:24 pm
by BenChang
Code: Select all
void setup()
{
   size(800,600);
}


float distance (float x1, float y1, float x2, float y2)
{
   float d;    // final result
   float dx = x2-x1;
   float dy = y2-y1;
   
   d = sqrt(dx*dx + dy*dy);
   return d;
}

void draw()
{
  float d;
  float w;
  if (mousePressed)
  {
    d = distance (mouseX, mouseY, pmouseX, pmouseY);
    w = 20-d;
    if (w<1)
      w=1;
     
    strokeWeight(w);
    stroke(w*10,0,d*10);
    line (pmouseX, pmouseY, mouseX, mouseY);
  }
}