문제

I've searched everywhere and I just can't seem to figure it out. I find a lot of articles on the decomposition of rectilinear/orthogonal polygons, but nothing on how to construct one.

I'm working with Slick2D, and I have a tile map. I want to minimize the amount of collisions I have to calculate by making single polygons out of any touching rectangles (i.e. combining side by side tiles).

Examples: https://www.dropbox.com/s/2kf8olw5701e1xn/rectilinear_polygon.png

What I'm working with is a 2D-array of Rectangles. I can traverse and figure out which are touching and all that, but for some reason I cannot figure out why .union() is not working (or maybe I don't fully understand it?). In slick2D it returns a Shape[], and in awt, there seems to be a method .createUnion(), which I tried outside of my program, but it didn't work either for whatever reason. I'd rather stick with slick2D classes though, for obvious reasons of compatibility.

Slick2D Rectangle class - http://www.slick2d.org/javadoc/org/newdawn/slick/geom/Rectangle.html

My method is simple right now. Just using two squares to test, then will fully expand once I get it to work by looping through them all.

Polygon p = new Polygon();

//Calculate polygons
public void calcPoly(){
    //The blocking array is all rectangles, [0,0] and [1,0] are known to be touching.
    p = RectangleToPolygon(blocking[0][0]);
    p.union(RectangleToPolygon(blocking[1][0]));
}

And I have a rectangle to polygon method as well, to make sure I'm working with polygons all the way through.

public Polygon RectangleToPolygon(Rectangle rect) {
    Polygon result = new Polygon();
    result.addPoint(rect.getX(), rect.getY());
    result.addPoint(rect.getX() + rect.getWidth(), rect.getY());
    result.addPoint(rect.getX() + rect.getWidth(), rect.getY() + rect.getHeight());
    result.addPoint(rect.getX(), rect.getY() + rect.getHeight());
    return result;
}

And it's not that I'm getting an error. It's the fact that I'm getting NO CHANGE whatsoever. No output, no error, no unification. It's seemingly easy stuff, yet, it doesn't wanna budge.

도움이 되었습니까?

해결책

I discovered an answer to what I was trying to accomplish (partially; I still get some weird outcomes with it, but overall, it returns what I want in the general sense).

There is a class Geom Utility in the Slick2D library which also has a .union() method. This returns an array of Shape with size 1 if the union worked and 2 if the shapes remain separate. Which is unlike the .union() that is in the Rectangle and Shape classes (For some odd reason....)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top