We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
store old value (Read 531 times)
store old value
Feb 13th, 2010, 5:36am
 
hey! this is probably a dumb question but I seem to not be getting it!

im making something with random x and Y values and i want to make a line connecting the new XY value to the old one ( that was randomly generated in the previous frame), how do I store this value?

here's my code Code:
Point[] Points = new Point[100];
float x;
float y;
float prevx;
float prevy;
void setup(){
size(screen.width,screen.height);
// smooth();
background(0);
stroke(255);
noFill();

for (int i = 0; i < Points.length; i++) {

Points[i] = new Point();
}
}

void draw(){


for (int i = 0; i < Points.length; i++) {
Points[i].desenhar();

}

}
class Point{

float x= random(width);
float y= random(height);


void desenhar(){

ellipse(x,y,10,10);
line(x,y,prevx,prevy);
}
}
Re: store old value
Reply #1 - Feb 13th, 2010, 8:26am
 
Quote:
Point[] Points = new Point[100];

// You don't need any of these global variables!
// float x;
// float y;
// float prevx;
// float prevy;
// All your data for a point should be stored in the Point class.

void setup(){
  size(screen.width,screen.height);
  // smooth();
  background(0);
  stroke(255);
  noFill();
  for (int i = 0; i < Points.length; i++) {
    Points[i] = new Point();
  } 
}

void draw(){
  background(0); // I also added this so you can see them clearly.
  for (int i = 0; i < Points.length; i++) {
    Points[i].desenhar();
  }
}

class Point{
  // These are the data members of your Point class.
  float x;
  float y;
  float prevx;
  float prevy;
  // Your class also needs a constructor.
  Point(){
    x = random(width);
    y = random(height);
    prevx = x;
    prevy = y;
  }
  // This is now the draw and update function for the Point class.
  void desenhar(){
    ellipse(x,y,10,10);
    line(x,y,prevx,prevy);
    // I made your points move about randomly a bit, so you can see their line tails.
    prevx = x;
    prevy = y;
    x += random(-25,25);
    y += random(-25,25);
   if( int(random(2000)) == 267){ // And jump wildly every once in a while.
    x += random(-100,100);
    y += random(-100,100);
   } 
   // Of course, how these things really move is up to you.
  }
}

Page Index Toggle Pages: 1