1

In my application the user can draw out a shape using a "pen" tool, like in Photoshop. The co-ordinates are stored in an array of points and a path is made from them. The path is what is draw on the Canvas.

I want to be able to detect if the user has clicked on the shape but I don't know how to start. If it were a regular shape e.g. rectangle, I could create a Region and use that but they path can contain many points and be any shape.

Thanks for any help...

Castrohenge
  • 8,525
  • 5
  • 39
  • 66
user1056798
  • 245
  • 6
  • 20
  • you shold chack this ;) http://stackoverflow.com/questions/9588003/android-how-to-check-if-a-path-contains-touched-point – user3095990 Dec 12 '13 at 19:18

1 Answers1

1

Found a solution thanks to this site http://alienryderflex.com/polygon/

Then I edited it to work with Android and java. If anyone is interested, here's the solution (very similar to the original) :

public boolean clicked(MotionEvent e) {
    int j = points.size() -1 ;
    boolean oddNodes = false;

    for (int i = 0; i < points.size(); i++) {
        if ((points.get(i).y < e.getY() && points.get(j).y >= e.getY() || points
                .get(j).y < e.getY() && points.get(i).y >= e.getY())
                && (points.get(i).x <= e.getX() || points.get(j).x <= e
                        .getX())) {
            if (points.get(i).x + (e.getY() - points.get(i).y)
                    / (points.get(j).y - points.get(i).y)
                    * (points.get(j).x - points.get(i).x) < e.getX()) {
                oddNodes = !oddNodes;
            }
        }
        j = i;
    }

    return oddNodes;
}
user1056798
  • 245
  • 6
  • 20