How to write an intersects for Shapes in android

Posted by Rafael T on Stack Overflow See other posts from Stack Overflow or by Rafael T
Published on 2012-06-06T11:32:07Z Indexed on 2012/06/06 16:40 UTC
Read the original article Hit count: 210

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about android

Related posts about math