Check android.graphics.path intersection with itself

廉价感情. 提交于 2019-12-10 03:57:45

问题


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.imgur.com/JrEmN.png


回答1:


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.



来源:https://stackoverflow.com/questions/7319486/check-android-graphics-path-intersection-with-itself

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