0

I have an written an Object called Shape which has a Point representing the topLeftCorner and a Dimension with represents its width and height. To get the topRightCorner I can simply add the width to the topLeftPoint.x. I use to rotate them on a certain degree around their center. The problem after the rotation is, that my intersects(Shape) method fails, because it does not honor the rotation of the Shapes. The rotation will be the same for each Shape. My current implementation looks like this inside my Shape Object:

public boolean intersects(Shape s){
    // functions returning a Point of shape s
    return intersects(s.topLeft())
        || intersects(s.topRight())
        || intersects(s.bottomLeft())
        || intersects(s.bottomRight())
        || intersects(s.leftCenter())
        || intersects(s.rightCenter())
        || intersects(s.center());
}

public boolean intersects(Point p){
    return p.x >= leftX()
        && p.x <= rightX()
        && p.y >= topY()
        && p.y <= bottomY();
}

Basically I need functions like rotatedLeftX() or rotatedTopRight() to work properly. Also for that calculation I think it doesn't matter when the topLeft point before a rotation of ie 90 will turn into topRight...

I already read this and this Question here, but do not understand it fully.

Community
  • 1
  • 1
Rafael T
  • 15,401
  • 15
  • 83
  • 144

2 Answers2

1

I modified an algorithm from Stackoverflow to do what you are indicating with rectangles for a battleship game I wrote. Here is the code:

    private boolean overlaid(int [][][] shps, int curr)
    {
        for (int i = curr-1; i>=0; --i)
        {
//              From: http://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other/306332#306332                
//              if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 &&
//                      RectA.Y1 < RectB.Y2 && RectA.Y2 > RectB.Y1)

            if (shps[curr][0][1] <= shps[i][1][1] && 
                shps[curr][1][1] >= shps[i][0][1] &&
                shps[curr][0][0] <= shps[i][1][0] && 
                shps[curr][1][0] >= shps[i][0][0])
                return true;
        }

        return false;
    }


 private int [][][]   shps = {  {{-1,-1},{-1,-1}},
                                 {{-1,-1},{-1,-1}},
                                 {{-1,-1},{-1,-1}}  };

The shps parameter is just a matrix indicating the location of the top-left and bottom-right corners of each rectangle using {x0,y0}, {x1,y1}. For example, shps[curr][0][1] == the current shp's y0. For my purposes, I had to use <= and >=. Also, you have to be mindful of the reverse of y if you are using screen coordinates versus Cartesian coordinates. Also DeMorgan's Law if you want to use NOT overlaid.

0

I have a solution:

lets assume I want to calculate the rotation (90 degrees) of a Shape with x =1, y=1 (topLeft Point) and a width of 4 and a height of 6 around its center (3, 4) == (x1, y2)

rotatedX = x1 + cos(q) * (x - x1) - sin(q) * (y - y1)
rotatedY = y1 + sin(q) * (x - x1) + cos(q) * (y - y1)

in this case:

rotatedX = 3 + cos(90) * (1 - 3) - sin(90) * (1 - 4)
rotatedY = 4 + sin(90) * (1 - 3) + cos(90) * (1 - 4)

this is for a rotation in a cartesian-plane (where positive rotation values means rotation counter-clockwise)

So if you wanna aply a rotation of 90 degrees CLOCKWISE you just have to multiply rotation with -1;

Rafael T
  • 15,401
  • 15
  • 83
  • 144