Path intersection in android

此生再无相见时 提交于 2019-11-30 14:43:48

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

Steven McConnon

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.

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

boolean intersects = path.op(p1,p2,Path.Op.INTERSECT)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!