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 › Solved:My code is too slow
Page Index Toggle Pages: 1
Solved:My code is too slow (Read 448 times)
Solved:My code is too slow
Jun 18th, 2008, 2:46pm
 
hello there
i am making a simple code which displays rectangles (filled with text) in a screen, allows for their drag drop, resize, zooming paning etc.
But it is getting very slow if number of rectangles increases to say 50 etc.

Can anyone help/advice me? I don't know much about double buffering or culling etc. Do I need to use such techniques for such a simple code?

In the code there is O(n²)complexity arising as you can see in the dragcheck function(it is called for each rect, and for each rect it checks all other rectangles)..It is being done to lock other rects from moving when one is being resized or moved...

http://turing.lecolededesign.com:16080/~rshukla/applet/

Re: my code is too slow
Reply #1 - Jun 18th, 2008, 2:56pm
 
One possible large speedup is to store a boolean saying whether mouseDragged is true currently.

If it's not, but then the mouseDragged function gets called, you find the nearest box as currently and store this information somewhere.

If it is true, and mouseDragged is happening then only send the movement info to the already selected box, without testing every box every time.

When mouseDragged is no longer happening, set the boolean to false, and clear the "selected box" information.
Re: my code is too slow
Reply #2 - Jun 18th, 2008, 5:33pm
 
There is a Handle example, I think. I tried to improve it, although it is currently just a bunch of circles that you can drag (only one at a time).
Core of the code:
Code:
Handle[] g_h = new Handle[HANDLE_NB]; // They are initialized in setup()
boolean g_bDragging;

void draw()
{
background(#AADDFF);
boolean bDragging = false;
for (int i = 0; i < HANDLE_NB; i++)
{
g_h[i].Update(g_bDragging);
if (g_h[i].IsDragged())
{
bDragging = true;
g_h[i].Move();
}
g_h[i].Draw();
}
g_bDragging = bDragging;
}
Code:
class Handle
{
float m_x, m_y; // Position of handle
int m_size; // Diameter of handle
// Stuff...

private boolean m_bIsHovered, m_bDragged;
private float m_clickDX, m_clickDY;

boolean IsDragged()
{
return m_bDragged;
}

void Update(boolean bAlreadyDragging)
{
m_bIsHovered = dist(mouseX, mouseY, m_x, m_y) <= m_size / 2;
if (!bAlreadyDragging && mousePressed && mouseButton == LEFT && m_bIsHovered)
{
m_bDragged = true;
m_clickDX = mouseX - m_x;
m_clickDY = mouseY - m_y;
}
if (!mousePressed)
{
m_bDragged = false;
}
}

void Move()
{
if (m_bDragged)
{
m_x = mouseX - m_clickDX;
m_y = mouseY - m_clickDY;
}
}
}

HTH.
Re: Solved:My code is too slow
Reply #3 - Jun 24th, 2008, 5:12pm
 
I tried putting lots of constraints and to stop non-picked objects in the scene from being even checked in the mouseDragged function.
But that did not work and code was still slow.
I then just substituted circles in place of rectangles for defining the boundry of objects.
and they work a lot more fluid although the code for handling objects using only rectangles was better in terms of complexity.

The working code that uses circles: http://turing.lecolededesign.com:16080/~rshukla/applet/

the code with better algorithn which did not work well because the rectangles were used.

http://turing.lecolededesign.com:16080/~rshukla/mapplet/

can someone explain the reason circles were better than rectangles for defining object boundry
Page Index Toggle Pages: 1