java - 将自相交 Path2D 拆分为多个非自相交路径的算法?

标签 java algorithm geometry pathgeometry

我需要摆脱形状中的自相交。形状由一组点构成,因此该形状的所有线段都是线。 (只有直线,没有曲线和圆弧)

以前,我尝试从这些点创建 Path2D,从中构建一个 Area,然后使用它的 PathIterator 我创建了几个 Path2D,它们不知何故是先前路径的子路径,因此自插入走了。但这对某些路径不起作用 - 自相交仍然存在。

那么你能告诉我一些我可以找到好的算法来做类似事情的地方吗?

编辑:我在任何地方都找不到有用的东西,所以我编写了自己的算法。查看答案。

最佳答案

因此,由于我无法在网上找到类似的东西,所以我编写了自己的算法。

它可能非常低效,但对我来说效果足够快。

这里是:

/**
 * Takes a polygon, defined by a list of lines, and splits it into several
 * paths on points of intersection. If non-self-intersected path is passed in,
 * the same path is returned.
 * @param path
 * @return
 */
public static List<List<Line2D>> splitPath(List<Line2D> lines) {
    List<List<Line2D>> splitted = new ArrayList<List<Line2D>>();
    // find intersections.
    loop1:
    for (Line2D l1 : lines) {
        for (Line2D l2 : lines) {
            if (l1 == l2) continue;
            Point2D intr;
            if ((intr = linesIntersect(l1, l2)) != null) {
                // creating two splitted subpaths
                int i1 = lines.indexOf(l1);
                int i2 = lines.indexOf(l2);

                List<Line2D> subpath1 = new ArrayList<Line2D>();
                subpath1.addAll(lines.subList(0, i1));
                subpath1.add(new Line2D.Double(l1.getP1(), intr));
                subpath1.add(new Line2D.Double(intr, l2.getP2()));
                subpath1.addAll(lines.subList(i2 + 1, lines.size()));
                splitted.addAll(splitPath(subpath1));

                List<Line2D> subpath2 = new ArrayList<Line2D>();
                subpath2.add(new Line2D.Double(intr, l1.getP2()));
                subpath2.addAll(lines.subList(i1 + 1, i2));
                subpath2.add(new Line2D.Double(l2.getP1(), intr));
                splitted.addAll(splitPath(subpath2));
                break loop1;
            }
        }
    }
    if (splitted.size() > 0) {
        return splitted;
    } else {
        return Collections.singletonList(lines);
    }
}

/**
 * Returns point of intersection of this line segments.
 * @param l1
 * @param l2
 * @return
 */
public static Point2D linesIntersect(Line2D l1, Line2D l2) {
    if (l1.getP1().equals(l2.getP2()) || l1.getP2().equals(l2.getP1())) return null;
    Point2D inter = lineIntersection(l1, l2);
    if (inter == null) return null;
    double infS = HEADER.infS;
    double x = inter.getX();
    if (((l1.getX1() > l1.getX2()) ? (x + infS > l1.getX2() && x - infS < l1.getX1()) : (x - infS < l1.getX2() && x + infS > l1.getX1())) &&
           ((l2.getX1() > l2.getX2()) ? (x + infS > l2.getX2() && x - infS < l2.getX1()) : (x - infS < l2.getX2() && x + infS > l2.getX1()))) {
        return inter;
    } else {
        return null;
    }
}

/**
 * Returns point of lines intersection, or null if they are parallel.
 * @param l1
 * @param l2
 * @return
 */
public static Point2D lineIntersection(Line2D l1, Line2D l2) {
    double a1 = l1.getY2() - l1.getY1();
    double b1 = l1.getX1() - l1.getX2();
    double c1 = a1*l1.getX1() + b1*l1.getY1();
    double a2 = l2.getY2() - l2.getY1();
    double b2 = l2.getX1() - l2.getX2();
    double c2 = a2*l2.getX1() + b2*l2.getY1();
    double det = a1*b2 - a2*b1;
    if (det != 0) {
        double x = (b2*c1 - b1*c2)/det;
        double y = (a1*c2 - a2*c1)/det;
        return new Point2D.Double(x, y);
    } else {
        // lines are parallel
        return null;
    }
}

关于java - 将自相交 Path2D 拆分为多个非自相交路径的算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4849645/

相关文章:

java - 使用 Jackson 和 Spring Boot 的条件 JsonProperty

algorithm - 存储100万个电话号码

c - 我的背包有问题吗

algorithm - 文件.TSP(旅行推销员)的含义

GL_QUAD_STRIP 上的 OpenGL 照明和着色

java - 如何从 java.net 下载 OpenJDK 8 源代码

java - Gitlab ci selenium testing with docker not connecting to RemoteWebDriver

java - 断言或mockito验证可重用性-错误行

npm - 在 CircleCI 上使用 2FA 发布 npm 包?

C++ 圆形对象/getArea()