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.
IndexProgramming Questions & HelpPrograms › Odd behavior on Coordinates
Page Index Toggle Pages: 1
Odd behavior on Coordinates (Read 834 times)
Odd behavior on Coordinates
Jul 24th, 2005, 6:46am
 
I'm making a very simple beginner program that draws a random line-graph.  After each frame, x increases by one, and y randomly increases or decreases.  
When the user clicks, a rectangle is drawn as a way to clear the previous frame.
What's odd is that after the rectangle is drawn, the point starts not at x = 0, but some values over.  But when I delete the line with the rect function, the point does start at 0...
Why the difference? Am I missing something?

Code:

float x, y;
void setup(){
size(300, 300);
colorMode(RGB, 100);
background(50);
rectMode(CENTER);
x = 0;
y = height/2;
}

void draw(){
point(x, y);
if(random(100)<50){
y = y+1;
}
else{
y = y-1;
}
x = x+1;

if (mousePressed==true){
fill(50);
rect(width/2, height/2, width, height);
if(x>width){
x = 0;
y = height/2;
}
}
}
Re: Odd behavior on Coordinates
Reply #1 - Jul 24th, 2005, 10:46am
 
Your not setting x to 0 after the mousePressed flag. My understanding is that you want an x reset after button pressed.


Code:

float x, y;
void setup(){
 size(300, 300);
 colorMode(RGB, 100);
 background(50);
 rectMode(CENTER);
 x = 0;
 y = height/2;
}

void draw(){
 point(x, y);
 if(random(100)<50){  
   y = y+1;
 }
 else{  
   y = y-1;
 }
 if ((x+=1) > width) {
  x=0;
 }
 
 if (mousePressed==true){  
   fill(50);
   rect(width/2, height/2, width, height);
   x=0;
   }
   
 
 }

Re: Odd behavior on Coordinates
Reply #2 - Jul 24th, 2005, 9:33pm
 
Thanks.
Hm, I don't know why the original code does not do a similar thing
Re: Odd behavior on Coordinates
Reply #3 - Jul 24th, 2005, 11:38pm
 

You have got nested conditionals

1st you test for the mouse button, and only when this is true do you then perform another conditional for x>width to determine whether x should be reset or not, but as you want to reset when the mouse is pushed it doesn't require any x test, as you'll be setting it to 0 regardless.

The X>width test needs to be outside of this reset function. When the x>width condition is satisifed you can do whatever you like, clear screen etc.

I hope this helps!
Re: Odd behavior on Coordinates
Reply #4 - Jul 25th, 2005, 5:07am
 
Yeah, I was a bit careless on what I wrote for what I originally aimed at. I wasn't too careful, and the result is the messy conditionals.
What really confused me at first was why the original program resets x not at 0, but at some values over.  I assumed that "mousepressed" meant just one click.  The longer I held the mouse button, the bigger x becomes for the 'reset' (Obvious now that I see it, hehe.)
Sorry for the confusion, thanks for the help!
Page Index Toggle Pages: 1