Path intersection in android
Asked Answered
D

3

5

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 !

Disesteem answered 25/6, 2012 at 6:31 Comment(0)
L
5

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...

Leyba answered 25/6, 2012 at 7:1 Comment(5)
Region will create a rectangle containing the path right?. My path is having curves.So i wont think creating a region will be effectiveDisesteem
no 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). " "Leyba
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 intersectionSpada
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.Leyba
@Dirk Can use it with a Paint set up for stroking and developer.android.com/reference/android/graphics/… to get the intersection of stroked paths which lack any filled areas otherwise.Portfire
I
7

The answer given by Dheeraj has the answer to your question:

https://mcmap.net/q/1921278/-collision-detection-with-bitmaps-on-surfaceview-39-s-canvas-in-android

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.

Insure answered 19/11, 2012 at 7:53 Comment(0)
E
7

From API 19 onwards, Path now has an op() method.

boolean intersects = path.op(p1,p2,Path.Op.INTERSECT)
Ecosystem answered 26/5, 2014 at 13:44 Comment(3)
The 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.Isidora
@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.Portfire
I tested the Path.Op and found it crashes easily on sdk <= 23 !Arrearage
L
5

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...

Leyba answered 25/6, 2012 at 7:1 Comment(5)
Region will create a rectangle containing the path right?. My path is having curves.So i wont think creating a region will be effectiveDisesteem
no 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). " "Leyba
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 intersectionSpada
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.Leyba
@Dirk Can use it with a Paint set up for stroking and developer.android.com/reference/android/graphics/… to get the intersection of stroked paths which lack any filled areas otherwise.Portfire

© 2022 - 2024 — McMap. All rights reserved.