JavaFX多边形交叉点未检测到交叉点

时间:2017-04-08 15:38:26

标签: java javafx javafx-8 polygon intersection

我正在使用JavaFX来交叉一些非常不规则的多边形。大部分时间都有效。但是,有些情况下调用

Polygon.intersect(polygon1, polygon2)
即使多边形相交,

也会产生一个空的Path对象。 以下是这种情况的图片: enter image description here

顶部的半透明多边形与大的棕色多边形相交,但在两个上面调用Polygon.intersect之后,我得到一个空路径。 下面是生成较小多边形并尝试将其与大多边形相交的代码:

public MountainRange(Continent continent, double scale, double jaggedness, int deformations, Random rand) {
super();
this.continent = continent;
boolean done = false;
System.out.println("Generating mountain");
int tries = 0;
while(!done && tries < 20) {
    tries++;
    this.getPoints().addAll((new Blob(2, scale, jaggedness, deformations, rand)).getDoubleList());
    this.setRotate(rand.nextDouble() * 360);

    Bounds continentBounds = continent.getBoundsInParent();
    System.out.println("continent bounds");
    System.out.println(continentBounds.getMinX() + "," + (continentBounds.getMinX() + continentBounds.getWidth()));
    System.out.println(continentBounds.getMinY() + "," + (continentBounds.getMinY() + continentBounds.getHeight()));

    Bounds mountainBounds = this.getBoundsInParent();
    System.out.println("Original mountain bounds");
    System.out.println(mountainBounds.getMinX() + "," + (mountainBounds.getMinX() + mountainBounds.getWidth()));
    System.out.println(mountainBounds.getMinY() + "," + (mountainBounds.getMinY() + mountainBounds.getHeight()));

    this.setTranslateX(-mountainBounds.getMinX() + mountainBounds.getWidth() / 2);
    this.setTranslateY(-mountainBounds.getMinY() + mountainBounds.getHeight() / 2);

    this.setTranslateX(this.getTranslateX() + continentBounds.getMinX() + rand.nextDouble() * continentBounds.getWidth());
    this.setTranslateY(this.getTranslateY() + continentBounds.getMinY() + rand.nextDouble() * continentBounds.getHeight());

    mountainBounds = this.getBoundsInParent();

    System.out.println("mountain bounds");
    System.out.println(mountainBounds.getMinX() + "," + (mountainBounds.getMinX() + mountainBounds.getWidth()));
    System.out.println(mountainBounds.getMinY() + "," + (mountainBounds.getMinY() + mountainBounds.getHeight()));
    Path intersectionPath = (Path) (Polygon.intersect(this, continent));
    ArrayList<Polygon> intersections = new ArrayList<>();
    Random colorRandom = new Random();
    Color color = new Color(colorRandom.nextDouble(), colorRandom.nextDouble(), colorRandom.nextDouble(), 0.5);
    if(intersectionPath.getElements().size() != 0) {
        System.out.println("First element mt? " + (intersectionPath.getElements().get(0) instanceof MoveTo));
    } else {
        System.out.println("intersection is empty for color " + color);
    }
    for (PathElement pe : intersectionPath.getElements()) {
        if (pe instanceof MoveTo) {
            MoveTo mt = (MoveTo) pe;
            intersections.add(new Polygon());
            intersections.get(intersections.size() - 1).getPoints().addAll(mt.getX(), mt.getY());
        } else if (pe instanceof LineTo) {
            LineTo lt = (LineTo) pe;
            intersections.get(intersections.size() - 1).getPoints().addAll(lt.getX(), lt.getY());
        }
    }
    if (intersections.size() > 0) {
        System.out.println("Intersection chunks: " + intersections.size());
        Polygon max = intersections.get(0);
        double maxarea = max.computeAreaInScreen();
        double currarea;
        for (Polygon pol : intersections) {
            currarea = pol.computeAreaInScreen();
            if (currarea > maxarea) {
                maxarea = currarea;
                max = pol;
            }
        }
        this.setTranslateX(0.0);
        this.setTranslateY(0.0);
        this.setRotate(0.0);
        this.getPoints().clear();
        this.getPoints().addAll(max.getPoints());
        done = true;
        System.out.println("success");
    } else {
        System.out.println("failure");
        Polygon copy = new Polygon();
        copy.getPoints().addAll(this.getPoints());
        copy.setFill(color);
        previous.add(copy);
        this.getPoints().clear();
        this.setTranslateX(0.0);
        this.setTranslateY(0.0);
        this.setRotate(0.0);
        done = false;
    }
}

}

如有必要,我可以发布我的其余代码。

这有什么明显的原因吗?

0 个答案:

没有答案