Search code examples
androidpathcollision-detection

Android Path collision problems/solutions


I have a drawing application in Android that allows the user to draw with their finger, and then stores the resulting shapes as Android Paths. To allow the user to delete individual Paths they have drawn, I have implemented this solution that uses a bounding Rect for each Path, and then uses an inner multi-dimensional binary array to represent the pixels inside the bounding Rect. I then populate the array by taking the Path's control points and track along it using the mathematical equation for a quadratic bezier curve, setting each element in the array that would have a pixel underneath it to 1.

Using the above setup, when in erasing mode, I first check for collision between the users finger and the bounding Rect, and if that collides, I then check to see if the pixel being touched by the user is set to a 1 in the array.

Now, when a user loads a note, I load all of the shapes into an ArrayList of 'stroke' objects so that I can then easily display them and can loop through them checking for collision when in erase mode. I also store the Rect and binary array with the strokes in the custom object. Everything is working as expected, but the memory footprint of storing all of this data, specifically the binary array for each Path's bounding Rect, is getting expensive, and when a user has a large number of strokes I am getting a java.lang.OutOfMemoryError on the portion of my code that is creating the array for each stroke.

Any suggestions on a better way to accomplish this? Essentially, I am trying to determine collision between two Android Paths (the drawing Path, and then a Path that the user creates when in erase mode), and while the above works in theory, in practice it is not feasible.

Thanks,

Paul


Solution

  • What is the actual representation of the "binary array"? I think if you tweak the representation to reflect the actual data you need to store (for example RLE encode the bits: at this y starting at this x and for z pixels) you will be able to store what you need to without excessive size.

    Storing an actual array of bytes with one byte per pixel, or per 8 pixels (if that is what you are doing) isn't necessary for this use.

    Another alternative is not to store a bitmap at all, just the control points and bounding boxes. If a touch intersects a bounding box, you calculate the bitmap on the fly from the control points.