开发者

Check android.graphics.path intersection with itself

I'd like to check if (and if yes, where the collission is(x,y) - just for highlighting) a path does intersect itself. It would also be very interesting how i do check if a path does intersect with another path. Here is a screenshot to better explain what i mean:

http://i.stack.im开发者_如何学Cgur.com/JrEmN.png


The simplest way is to check if any line segment intersects with any other line segment. A line segment is made up of pairs of adjacent points in the path. A path with 10 points has 9 line segments.

Here's an example of how one might go about that.

import android.graphics.Point;
import java.util.List;

static Boolean isPathComplex(List<Point> path) {

    if (path == null || path.size() <= 2) {
        return false;
    }

    int len = path.size();  

   for (int i = 1; i < len; i++) {
        Point lineAStart = path.get(i - 1);
        Point lineAEnd = path.get(i);

        for (int j = i + 1; j < len; j++) {
            Point lineBStart = path.get(j - 1);
            Point lineBEnd = path.get(j);
            if (lineSegmentsIntersect(lineAStart, lineAEnd, lineBStart, lineBEnd)) {
                return true;
            }

        } // inner loop

   } // outer loop

}

static Boolean lineSegmentsIntersect(Point aInitial, Point aFinal, Point bInitial, Point bFinal) {
    // left as an exercise to the reader
}

See How do you detect where two line segments intersect? for an example of how to implement the lineSegmentsIntersect function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜