I have 2 path objects in my android code.I have tried all the way to check whether these paths are intersected or not, but not able to do it. How can I check whether the paths are intersected or not. Appreciate any good response, Thanks !
3 Answers
The answer given by Dheeraj has the answer to your question:
https://stackoverflow.com/a/9918830/1268168
Here's a copy and paste of his answer:
Another method I can think of will work with simple objects that can be constructed using Paths.
Once you have two objects whose boundaries are represented by paths, you may try this:
Path path1 = new Path();
path1.addCircle(10, 10, 4, Path.Direction.CW);
Path path2 = new Path();
path2.addCircle(15, 15, 8, Path.Direction.CW);
Region region1 = new Region();
region1.setPath(path1, clip);
Region region2 = new Region();
region2.setPath(path2, clip);
if (!region1.quickReject(region2) && region1.op(region2, Region.Op.INTERSECT)) {
// Collision!
}
Once you have your objects as Paths, you can draw them directly using drawPath(). You can also perform movement by transform()ing the path.
From my understanding, the variable "clip" in the above code should be the bounding box of the path. For general purposes I use
Region clip = new Region(0, 0, layoutWidth, layoutHeight);
Where the layout width and height can be the size of your canvas or activity or whatever.

- 1
- 1

- 2,650
- 2
- 16
- 21
From API 19 onwards, Path
now has an op()
method.
boolean intersects = path.op(p1,p2,Path.Op.INTERSECT)

- 29,290
- 3
- 79
- 130
-
5The return value indicates the success of the operation, not whether they intersect or not. To check intersection, you need to see if the result isEmpty(). And it intersects enclosed regions, not paths themselves. Quick test: construct an angle polyline as a path, then a single line path that sits inside the angle, not crossing it. Intersect them and see. – Seva Alekseyev Feb 27 '15 at 21:08
-
3@SevaAlekseyev Combined with Paint.getFillPath (Path src, Path dst) and the paint set up for stroking, this will work with non-filled paths as well. – msand Sep 01 '18 at 12:19
-
I tested the `Path.Op` and found it crashes easily on sdk <= 23 ! – Jeff T. May 28 '19 at 03:40
have a look at Region.op
I haven't tried it but I would suggest to use:
Region.setPath(Path path, Region clip);
to get a region from both of your paths and afterwards you can use:
if (region1.op(region2,Region.Op.INTERSECT)) {
// intersection
}
to check for intersection...

- 5,513
- 1
- 37
- 55
-
1Region will create a rectangle containing the path right?. My path is having curves.So i wont think creating a region will be effective – V I J E S H Jun 25 '12 at 07:11
-
4no it does not only create a rectangle containing the path. Have a look at the documentation: "This produces a region that is identical to the pixels that would be drawn by the path (with no antialiasing). " " – D-rk Jun 25 '12 at 07:34
-
what will the op() return if region1 contains whole region2 and their boundary/outline doesn't intersect? I mean, how to check the two separate cases, 1.Region intersection and 2. Path intersection – M. Usman Khan Nov 27 '13 at 09:54
-
this will only do region intersection. As far as I know there is no direct way to do path intersection. You could fake it by creating a region in a small offset along the path. – D-rk Nov 27 '13 at 21:33
-
@Dirk Can use it with a Paint set up for stroking and https://developer.android.com/reference/android/graphics/Paint#getFillPath(android.graphics.Path,%20android.graphics.Path) to get the intersection of stroked paths which lack any filled areas otherwise. – msand Sep 01 '18 at 12:17