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 & HelpSyntax Questions › The performance of processing
Page Index Toggle Pages: 1
The performance of processing? (Read 481 times)
The performance of processing?
Sep 11th, 2006, 12:16am
 
Hi,

I'm relatively new to deeper programming (only scripting before) and I just realised an idea of mine. It's a little thing that adds vertexpoints to the canvas on mouseclick which are also dragable.

But the point one is dragging around is behind the mouse if one moves it to fast. I would like that the point is under the mouse while draging.

So is this my antisuperior programmingskill or does it have to do with the framerate or that my laptop runs java to slow or ...?

I love info and processing :)

--------->

int[] coordinatesX = new int[0];
int[] coordinatesY = new int[0];
int arrayX, arrayY;
int zaehler = 0;
boolean tracker = false;

void setup()
{
 size(300, 300);
 framerate(30);
 colorMode(RGB, 100);
 stroke(0);
 background(100);
 fill(0);
}

void draw()
{
 background(100);
 
 if(tracker == true)
 {
   coordinatesX[arrayX] = pmouseX;
   coordinatesY[arrayY] = pmouseY;
 }
 
 for(int i = 0; i < zaehler; i++)
 {
   fill(0);
   rectMode(CENTER);
   rect(coordinatesX[i], coordinatesY[i], 2, 2);
 }
 
 for(int i = 0; i < zaehler - 1; i++)
 {
   line(coordinatesX[i], coordinatesY[i], coordinatesX[i + 1], coordinatesY[i + 1]);
 }
}

void mousePressed()
{
 for(int i = 0; i < zaehler; i++)
 {
   if((mouseX > (coordinatesX[i] - 2)) && (mouseX < (coordinatesX[i] + 6))
   && (mouseY > (coordinatesY[i] - 2)) && (mouseY < (coordinatesY[i] + 6)))
   {
     arrayX = i;
     arrayY = i;
     tracker = true;
   }
 }
}

void mouseReleased()
{
 if(!tracker)
 {
   coordinatesX = expand(coordinatesX, (coordinatesX.length + 1));
   coordinatesY = expand(coordinatesY, (coordinatesY.length + 1));
   coordinatesX[zaehler] = mouseX;
   coordinatesY[zaehler] = mouseY;
   zaehler++;
 }
 tracker = false;
}
Re: The performance of processing?
Reply #1 - Sep 11th, 2006, 5:18am
 
Instead of using "pmouseX,pmouseY" (the p means previous mouse position),
you might just use "mouseX,mouseY" to get the current mouse position.

-- djones
Re: The performance of processing?
Reply #2 - Sep 11th, 2006, 10:58am
 
I realised to late that I posted it like that. But it's still not what I meant.

I really don't know to much about programming, but in the end I guess it's that java is not that system near as a prog writen in C.

Thank you
Re: The performance of processing?
Reply #3 - Sep 11th, 2006, 2:36pm
 
Quite true. A program I made, that worked perfectly fine on a Mac, worked slow on XP although the XP machine was faster than the Mac. I think its a Java problem. Could one of the more experienced persons in processing help us out with this problem please..... Smiley
Does Processing execute the draw function faster on a Mac, than on a windows machine (no matter what the framerate is)?
Re: The performance of processing?
Reply #4 - Sep 11th, 2006, 11:11pm
 
You need adopt the idea of "locking". When you press the mouse down over your marker have it locked to the mouse - wherever the mouse may go until you release the mousebutton.
Code:

Draggable thing;
void setup(){
size(200, 200);
smooth();
thing = new Draggable(100, 100);
}
void draw(){
background(130);
thing.draw();
}
class Draggable{
float x, y;
boolean locked;
Draggable(float x, float y){
this.x = x;
this.y = y;
}
void draw(){
ellipse(x, y, 20, 20);
if(mousePressed && over()){
locked = true;
}
if(!mousePressed){
locked = false;
}
if(locked){
x = mouseX;
y = mouseY;
}
}
boolean over(){
if(dist(mouseX, mouseY, x, y) < 20){
return true;
}else{
return false;
}
}
}

This has nothing to do with processor speed. It's just method. On a really crap laptop you can click and hold on the browser scroll bar and it will still follow the mouse when you move the pointer off of it.

Yes, XP does run Java slow. I've also had OPENGL mode issues on a PC that are munting awful to code around. Ask Santa for a G5 for Christmas. On the flipside a lot of manufacturers of hardware (like micro controllers and webcams) don't bother to support Macs. Then again your Dell laptop could be setting fire to your bedroom right now.

It's like Ataris and Amigas all over again. Despite being crapper than Amiga there were somethings on the Atari that just worked better.

My advice is simply to pick the right machine for the right place. Or change your code to suit your machine. Or write faster code. You can perform simple speed tests by doing the following:
Code:

int timer;
void setup(){
timer = millis();
for(int i = 0; i < 1000; i++){
//insert function to speed test here
}
println(millis() - timer);
}
Re: The performance of processing?
Reply #5 - Sep 12th, 2006, 9:17pm
 
Could i ask you to explain that method to me?

I see no big difference between yours and mine (expect that mine is horrible complicate).

Is it that in yours the checking if mouseover is happening in the class (object itself) and not in the mainprogram?

thanks for the great advise!
Re: The performance of processing?
Reply #6 - Sep 13th, 2006, 4:03pm
 
The reason your code is slow I think is because you're using pmouseX / pmouseY to lock the object to the mouse (as djones pointed out). Which works until you lower the framerate, because your tracking method will always be one frame behind. Drop your framerate() to 2 and you'll see what I mean.

The method I'm using is checking every single frame for the location of the mouse and keeping under the mouse while the button is down. The only problem with this you'll find is that you can drag a Draggable around the screen you end up sweeping them all together so you might want to say which one you're locked to and only allow one Draggable to move at a time.

If you're going to use classes for this you'll need to brush up on Vectors. A Vector is a magic array.
Code:
Vector myVector;
void setup(){
size(200,200);
myVector = new Vector();
smooth();
}
void draw(){
background(150);
for(int i = 0; i < myVector.size(); i++){
// Getting an object out of a Vector is a little weird
// because you can pretty much stick any object in one
// so you have to say what the object is you're retrieving
Spot temp = (Spot)myVector.get(i);
// Because this is Java we haven't made a new object, just
// a reference leading to it, so what ever we do to "temp"
// happens to the object in myVector
temp.draw();
}
}
void mousePressed(){
// Vectors expand far quicker than arrays
myVector.add(new Spot(mouseX, mouseY));
}
class Spot{
float x,y;
Spot(float x, float y){
this.x = x;
this.y = y;
}
void draw(){
ellipse(x, y, 20, 20);
if(over() && keyPressed){
// This next line is really bloody handy,
// saves me from cleaning up outside the class
myVector.remove(myVector.indexOf(this));
}
}
boolean over(){
if(dist(x, y, mouseX, mouseY) < 10){
return true;
}
return false;
}
}

I do a lot of iterative work so right now I think Vectors are the shiznitz (so sayeth the dolt who only figured out how to use them in the past 6 months).
Page Index Toggle Pages: 1