0

I am working with JavaFX to intersect some very irregular polygons. Most of the time it works. However, there are some instances where calling

Polygon.intersect(polygon1, polygon2)

results in an empty Path object even when the polygons intersect. Here is a picture of this happening: enter image description here

The translucent polygon at the top does intersect the large brown polygon, but after calling Polygon.intersect on both of them, I get an empty Path. Here is the code that is generating the smaller polygon and attempting to intersect it with the large one:

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;
    }
}

}

I can post the rest of my code if necessary.

Are there any obvious reasons why this would be?

  • Have you tried making the smaller polygon non-translucent? I remember to once have problems in that area although the documentation clearly states that this should not matter. – mipa Apr 08 '17 at 18:36
  • Yes, I am doing the intersection before I am coloring the polygon. I'll keep that in mind for the future though. – homosapien97 Apr 08 '17 at 18:52
  • See if this answer makes a difference. http://stackoverflow.com/questions/31148690/get-real-position-of-a-node-in-javafx – SedJ601 Apr 08 '17 at 19:34
  • That's what I meant. If your polygons (or one of them) does not have a fill color the intersection may be empty and I remember that on some systems this was even true for some too translucent colors. – mipa Apr 08 '17 at 20:25

0 Answers0