Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

java - Path intersection in android

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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...