little algorithm needed

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Hi All,

Hope you all doing great :)

I am trying to improve part of my code as the current one causing memory leaks in some of the device (older ones), not only that but I did not make use of code best practice.

Basically I have a method that tracks the location (x,y) of your finger on the screen and it works fine. The x and y coordinates are float numbers in the range [-1,1].

I am currently drawing the circle at the finger location as follows:

Code:
Circle circle;

@Override
public void onDrawFrame(GL10 gl)
{
  ...
  circle = new Circle(CurrentFingerPosition.x, CurrentFingerPosition.y);
  circle.draw();
  ...
}
This 'onDrawFrame' method is constantly called by the system and the result on the screen is a circle following your finger BUT there's lot going in the constructor and calling it several times in a loop like this can only cause problems.

What I have done now is to only instantiate this circle at initialization and use a translate matrix to project/move this circle around.

Here's what I have now:

Code:
Circle circle;

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
  //...
  circle = new Circle();
  //...
}

@Override
public void onDrawFrame(GL10 gl)
{
  //...
  positionCircle(CurrentFingerPosition.x, CurrentFingerPosition.y); // this returns 'modelMatrix'
  circle.draw(modelMatrix);
  //...
}
This new approach works well and seems to solve the memory leak issue but I have another proplem I am trying to solve now. It's an algorithmic problem. First to make things simple I have not posted the definition of the constructor and the positionCircle method. If you don't mind let's assume the above method works fine.

Here's the problem:
The circle is constantly translated given that the 'onDrawFrame' is continuously called (not sure maybe 60fps or less). The initial position of the circle is the center of the screen (0.0f, 0.0f).

I need help deriving an algorithm solving this. It should be pretty simple but as you know the smallest thing can mess up you mind sometimes.

Let's say you drag your finger/circle from (0,0) to (3,5). I am using integers here to make the reasoning simple.
The 'CurrentFingerPosition.x' will have values: 0,1,2,3 and CurrentFingerPosition.y values: 0,1,2,3,4,5.
That means when the 'positionCircle' method is called the circle will be translated by those value and will keep translating them by (3,5) till a new (x,y) value of 'CurrentFingerPosition' is detected when I drag the circle again.

What I would want in the above example is once the circle is in the new location (3,5) it should stay there until I move it again in a new location. The circle should follow the location of your finger (CurrentFingerPosition.x, CurrentFingerPosition.y).

The first approach was simple as I was re-setting the circle location before a new translation.
I guess I need to use a similar one.


I have talked enough! I can give more details if needed.

Any help/comment will be appreciated!

Eric
 
Last edited:

MrChips

Joined Oct 2, 2009
30,824
I don't know enough in order to offer concrete solutions but here are two things that come to mind.

1. Why are you using floats and not integers? Float puts a huge hit on time and memory resources.

2. It is generally prudent to make a distinction between absolute coordinates and relative positions. If instead you use relative values with respect to a frame origin this might solve your problem.
 

WBahn

Joined Mar 31, 2012
30,082
Why would your x coordinate values be fewer than your y coordinate values? Why wouldn't you have repeats in the x coordinate list so that the total number is the same?

What language is this? Java? C++? Something else? It sounds like C++ since you are saying you have a memory leak. If so, then the fix is just to delete the previous circuit object before creating a new one. This may not be the best solution due to destructor/constructor overhead.

What happens to the old circle that was previously drawn on the screen?
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Why would your x coordinate values be fewer than your y coordinate values?
That was just an example to explain the whole thing. x and y can be anything in the range [-1, 1]. Let me put it this way then: You move from A(X0, Y0) to B(Xk, Yk).

Why wouldn't you have repeats in the x coordinate list so that the total number is the same?
I am not sure I understand the question. Will answer once I get it :)

What language is this? Java? C++? Something else? It sounds like C++ since you are saying you have a memory leak. If so, then the fix is just to delete the previous circuit object before creating a new one. This may not be the best solution due to destructor/constructor overhead.
I am using C# (Xamarin). I like this "then the fix is just to delete the previous circuit object before creating a new one" I think I tried that not sure though. Will try again this could solve the issue.

What happens to the old circle that was previously drawn on the screen?
The garbage collection takes care of it but I think this is not really working well...will try destroying the previous one before creating the new one and see how this works. Hope this solves the problem as I don't want to spend lots of time on this but I would also be happy to solve this by having only once instance of the circle and using the translate matrix to move it around. Well as explained above I can move it around using the matrix BUT I cannot control it as it flying because of the continuous call of the method... there should be a if statement somewhere and...

Thanks so much! this a good starting point.


Eric
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
I don't know enough in order to offer concrete solutions but here are two things that come to mind.
1. Why are you using floats and not integers? Float puts a huge hit on time and memory resources.
Well it has to do with OpenGl coordinates system...

2. It is generally prudent to make a distinction between absolute coordinates and relative positions. If instead you use relative values with respect to a frame origin this might solve your problem.
Will think about it but the problem is more algorithmic to me. Will try to post a little algo as soon as I can

Eric
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
@WBahn I think I will go for deriving an algorithm... using the destructor is not fun + have destructor/constructor calls thousands of time is not cool.
 
Top