Android: How to check if a path contains touched point?

android path get points
android path arcto
android check if point inside polygon
android rect contains
android region
android path reset
check if point is inside rectangle
android path length

i would try to develop an application in which i can draw a planimetry. So, each room has got its own ID or name and, if i touch a room, i want to show a Toast Message with that ID or name. The problem is how check if and which path is touched!!

I saw a lot of topic discussions that talked about this problem. Someone says to use getBounds method and, after, contains method for checking if touched point is in Rect. But, i guess getBounds method returns the smallest Rect that contains path, right?

So, rooms have different custom geometric forms and, for this reason, if i get bounds about 2 close rooms, method could return a shared set of points. Bad! Each room has got only their area points. How can i solve this problem ?

In iOS i could use PathContainsPoint method, but, unfortunally, Android Path doesn't have something similar.

I hope someone can help me Thx in advance


Ok i solved my problem. I post the example code:

Path p;
Region r;

@Override
public void onDraw(Canvas canvas) {

    p = new Path();

    p.moveTo(50, 50);
    p.lineTo(100, 50);
    p.lineTo(100, 100);
    p.lineTo(80, 100);
    p.close();      

    canvas.drawPath(p, paint);

    RectF rectF = new RectF();
    p.computeBounds(rectF, true);
    r = new Region();
    r.setPath(p, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));

}   

public boolean onTouch(View view, MotionEvent event) {

    Point point = new Point();
    point.x = event.getX();
    point.y = event.getY();
    points.add(point);
    invalidate();
    Log.d(TAG, "point: " + point);

    if(r.contains((int)point.x,(int) point.y))
        Log.d(TAG, "Touch IN");
    else
        Log.d(TAG, "Touch OUT");

    return true;
}

How can I tell if a closed path contains a given point?, The android.graphics.Path class doesn't have such a method. The Canvas class does have a clipping region that can be set to a path, there is no way to test it  i would try to develop an application in which i can draw a planimetry. So, each room has got its own ID or name and, if i touch a room, i want to show a Toast Message with that ID or name.


As Edward Falk say, best way is to use path.op() becouse Region is square. And one point can be in 2 or 3 Regions. For example: enter image description here All regions will contain blue point, but on fact only path4 contains this point.

int x, y;
Path tempPath = new Path(); // Create temp Path
tempPath.moveTo(x,y); // Move cursor to point
RectF rectangle = new RectF(x-1, y-1, x+1, y+1); // create rectangle with size 2xp
tempPath.addRect(rectangle, Path.Direction.CW); // add rect to temp path
tempPath.op(pathToDetect, Path.Op.DIFFERENCE); // get difference with our PathToCheck
if (tempPath.isEmpty()) // if out path cover temp path we get empty path in result
{ 
    Log.d(TAG, "Path contains this point");
    return true;
}
else
{
    Log.d(TAG, "Path don't contains this point");
    return false;
}

How can I tell if a closed path contains a given point?, I don't know if that would really check against the path or just the enclosing rectangle, toPixels(geoPoint, point); if (region.contains(point.x, point.y)) { // Within the path. } So i cant do it by checking the coordinates where user have touched. As Edward Falk say, best way is to use path.op() becouse Region is square. And one point can be in 2 or 3 Regions. For example: enter image description here All regions will contain blue point, but on fact only path4 contains this point.


Here's a thought: create a new path which is a tiny square around the point that was touched, and then intersect that path with your path to be tested using path.op() and see if the result is empty.

Path.Op, Android: How to check if a path contains touched p getBounds method and, after, contains method for checking if touched point is in Rect. To react to touch events. you override the onTouchEvent () method. The MotionEvent class contains the touch related information, e.g., the number of pointers, the X/Y coordinates and size and pressure of each pointer. This method returns true if the touch event has been handled by the view. Android tries to find the deepest view which returns true to handles the touch event.


How to check if a given point lies inside or outside a polygon , A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and  This tutorial shows you how to add a Google map to your Android app, and use polylines and polygons to represent routes and areas on a map. Follow the tutorial to build an Android app using the Maps SDK for Android. The recommended development environment is Android Studio. Get the code


Check if two given circles touch or intersect each other , It contains well written, well thought and well explained computer science and Task is to check both circles A and B touch each other or not. If C1C2 == R1 + R2 Circle A and B are touch to each other. C++ program to check if two the distance between the centers of the circles and the point of intersection of two direct  This lesson shows you how to listen for touch events to let users rotate an OpenGL ES object. Setup a touch listener. In order to make your OpenGL ES application respond to touch events, you must implement the onTouchEvent() method in your GLSurfaceView class.


How to check if a given point is inside some closed path in MetaPost , Based on Get all intersection points in MetaPost and implementing the even-odd rule: vardef is_inside(expr p,q) = begingroup; save cut, inside;  To detect when the user touch the sprite you would need to: Get touch position; Convert that touch position from screen to world point; Then use Physics2D.OverlapPoint to check if your touching any collider( so you need to have 2d collider attached to the sprite gameobject) Final code: