简体   繁体   中英

Rectangle intersection shared edge

I'm trying to determine whether two rectangles border each other. If they share an edge or part of an edge, then I want to include them, if they only share a vertice then I don't.

I've tried using android android.graphics.Rect, I was hoping that the intersect method would return true giving me a rectangle, with 0 width but the points of the intersecting edge. I'm using andEngine and also tried the collideswith method of org.andengine.entity.primitive.Rectangle however that returns true, even if the rectangle only share one corner vertice.

Is there a nice way of doing this? The only other way I can think of is to try and create a collection of all the edges then see if they're equal or are in someway partly equal.

Here's an image to demonstrate what I want. If I click on rect 1 then I want to return rects 2,3 and 4, but not 5.

"Map":

It sounds like you need a new class to do this. I would take the coordinates of each corner of the rectangles. Then, when you are selecting a rectangle, you can get those adjacent to it by finding them one side at a time. Starting with the top for an example, you check which other rectangles have corners at the same height. From that list, you check to see which ones exist on at least one point between the two top corners. So, if top left is 0,3 and top right is 4,3 then you would look for the list of corners at y=3. From that list you find all corners where 0<=x<=4 and anything that fits will be adjacent. You then do the same thing for each additional side. It should be an easy class to make, but I am not going to write any code as I do not know anything about how you stored your data or how you would reference this in your code. If you need help with that, write a comment.

Write a function to find which rectangles share edges with rectangles within all considered rectangles.

Then, map these rectangles which share edges to one another. An Adjacency List is just a way of representing a graph in code.

Sometimes code is easier to understand, so here's code. I have not tested this, but it should get you most the way there.

Also, I'm not sure what you're end goal is here but here's a question I answered that deals with rectangular compression.

List<Rectangle> allRectangles;

public boolean shareAnEdge(Rectangle r1, Rectangle r2){
    int y1 = r1.y + r1.height;
    int y2 = r2.y+r2.height;
    int x1 = r1.x+r1.width;
    int x2 = r2.x+r2.width;
    boolean topShared = (y1 == r2.y && r2.x == r1.x);
    boolean bottomShared = (y2 == r2.y && r2.x==r1.x);
    boolean rightShared = (x1 == r2.x && r2.y==r1.y);
    boolean leftShared = (x2 == r1.x && r2.y==r1.y);
    if (topShared || bottomShared || rightShared || leftShared) {
        return true;
    }
    return false;
}

public List<Rectangle> findSharedEdgesFor(Rectangle input){
    List<Rectangle> output = new List<Rectangle>();
    for(Rectangle r : allRectangles){
        if(r!=input && shareAnEdge(r, input)){
            output.add(r);
        }
    }
}

public AdjacencyList createGraph(List<Rectangle> rectangles){
    AdjacencyList graph = new AdjacencyList();
    for(Rectangle r : rectangles){
        List<Rectangle> sharedEdges = findSharedEdgesFor(r);
        for(Rectangle shared : sharedEdges){
            graph.createEdgeBetween(r, shared);
        }
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM