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 › Can someone explain my own code to me
Page Index Toggle Pages: 1
Can someone explain my own code to me? (Read 923 times)
Can someone explain my own code to me?
Feb 15th, 2009, 7:37am
 
Im really confused, i wrote/copied whatever,  this piece of code about 2 years ago and just came across it as it is perfect for some other idea am working on... its working but i just dont understand how...

this is the code :

Quote:
int anzahl=29;
float[] oldX = new float[anzahl];
float[] oldY = new float[anzahl];

void setup(){
 size(400,400);
 background(255);
 frameRate(40);
 for(int i=0; i<anzahl; i++){
   oldX[i] = 0;
   oldY[i] = 0;
 }
}

void draw(){
background(255);

float[] tmpX = new float[anzahl];
float[] tmpY = new float[anzahl];
for(int i=0; i<anzahl-1; i++){
   tmpX[i+1] = oldX[i];
   tmpY[i+1] = oldY[i];
  }
 
  tmpX[0]=mouseX;
  tmpY[0]=mouseY;
 
 oldX=tmpX;
 oldY=tmpY;
   

 for(int i=0; i<anzahl-1; i++){
   line(oldX[i],oldY[i],oldX[i+1],oldY[i+1]);
 }
}



And ive got the following questions...

Why doesnt it work when i declare
float[] tmpX = new float[anzahl];
float[] tmpY = new float[anzahl];
outside of the draw function... its just declared there, why does it behave different when i do this outside the draw function?!

the next thing is, what the crappity smack am i doing here?

oldX=tmpX;
oldY=tmpY;

isnt it an array? what does it mean in this case? just a simple way for putting every single part of one array into another?


Why is there this Line going to the origin(0/0) at the beginning of the sketch? i mean i know there must be an array which is not filled and is still 0 but where is it?
its not:  oldX[i] = 0;oldY[i] = 0; ...

and last but not least...
How should i change my code so that the line stays the same when i dont move the mouse and only erases itself when i keep on moving...

WOW, im feeling stupid... this is for those people who love so solve Problems and reading code :) Thank you Guys!!

Re: Can someone explain my own code to me?
Reply #1 - Feb 15th, 2009, 10:08am
 
The first two questions are related...

oldX=tmpX;
oldY=tmpY;

old arrays no longer points to the data allocated with new float (this old data will be garbage collected) but to tmp arrays' data. Arrays are like objects here, the variables only hold a reference to the data.

So, you need to have the tmp initialization in the draw() to generate a new array each time.
Note it is a very costly (in memory) method, it would be more efficient to shift the data in place in the array. Even more as the first value is just overwritten.

The 0,0 is probably the coordinates of mouseX/mouseY at startup.
Re: Can someone explain my own code to me?
Reply #2 - Feb 15th, 2009, 10:58am
 
Thank you! but you are totally right, its getting really slow and the FPS drop (not in this example, the new one)

You would help me alot of you could maybe explain me how to reduce it and make it more efficent.

Thanks Philho!
Re: Can someone explain my own code to me?
Reply #3 - Feb 15th, 2009, 7:04pm
 
I tried to do what you said, but i have no idea howto shift the data in the array. could you help me somehow?
Thank you alot!
Re: Can someone explain my own code to me?
Reply #4 - Feb 15th, 2009, 8:48pm
 
int anzahl=29;
float[] linesX = new float[anzahl];
float[] linesY = new float[anzahl];

void setup(){
 size(400,400);
 background(255);
 frameRate(40);
}

void draw(){
background(255);

for(int i=anzahl - 2; i>=0; i--){
   linesX[i+1] = linesX[i];
   linesY[i+1] = linesY[i];
}
 
linesX[0]=mouseX;
linesY[0]=mouseY;

 for(int i=0; i<anzahl-1; i++){
   line(linesX[i],linesY[i],linesX[i+1],linesY[i+1]);
 }
}
Re: Can someone explain my own code to me?
Reply #5 - Feb 15th, 2009, 9:04pm
 
Thank you so much for helping me and the rest so often...
Would be much harder without you!
Re: Can someone explain my own code to me?
Reply #6 - Feb 16th, 2009, 11:21am
 
all the copying of data in this strikes me as a bit wasteful. you could try a circular buffer instead which would get rid of the need of it. or, you guessed it, an ArrayList.

(ok, not really a problem with 29 values, but when you have 29,000...)
Re: Can someone explain my own code to me?
Reply #7 - Feb 16th, 2009, 11:42am
 
Although I didn't used them much, a Java Queue is probably better for this job.
As you mention, it is worth only for a large number of values...
Re: Can someone explain my own code to me?
Reply #8 - Feb 16th, 2009, 11:50am
 
Like i said this was just an example i reused it for a different sketch with about 5.000-10.000 of these lines...
so im a bit confused now, 3 more ways to do that?
Re: Can someone explain my own code to me?
Reply #9 - Feb 16th, 2009, 1:46pm
 
Ah bah, 10000 values is probably quickly processed by Java . But for educational purposes (my education as well!), I might redo the sketch using some queue.
Re: Can someone explain my own code to me?
Reply #10 - Feb 16th, 2009, 3:31pm
 
Code:
int queueSize = 50;

ArrayDeque lines;

void setup()
{
size(700, 700);
frameRate(40);
lines = new ArrayDeque();
}

void draw()
{
background(255);

PVector p = new PVector(mouseX, mouseY);
lines.addFirst(p);
if (lines.size() > queueSize)
{
lines.removeLast();
}

Iterator it = lines.iterator();
PVector pp = null;
while (it.hasNext())
{
PVector cp = (PVector) it.next();
if (pp != null)
{
line(pp.x, pp.y, cp.x, cp.y);
}
pp = cp;
}
}
Re: Can someone explain my own code to me?
Reply #11 - Feb 16th, 2009, 4:32pm
 
ha, i didn't even know deque existed. it's not in my copy of Java In A Nutshell (3rd Edition (Covers Java 1.3) (C)1996)
Re: Can someone explain my own code to me?
Reply #12 - Feb 17th, 2009, 10:24am
 
Smiley
Yes, beyond the improvements to the syntax, Java 1.5 introduced a number of useful new classes.
Actually, even 1.4 had new stuff, I fear your copy of the book is badly outdated...
I have this book (probably the 1.4 edition) and I admit I rarely open it, going straight to the online JavaDoc!
Page Index Toggle Pages: 1