Rotate Rectangle in Java

前端 未结 2 2056
孤城傲影
孤城傲影 2020-12-11 20:18

I need to create rectangles that are rotated around their center (so they don\'t need to be parallel to the axes of the coordinate system). So basicelly each rectangle can b

2条回答
  •  失恋的感觉
    2020-12-11 21:17

    You can use Rectangle2D to check for containment, if instead of rotating your rectangle by an angle, say, counterclockwise, you rotate each of the points you need to check by the same angle clockwise, relative to the center of the rectangle. Something like

    double dx = point.x - rectangleCenter.x;
    double dy = point.y - rectangleCenter.y;
    double newX = rectangleCenter.x - dx*Math.cos(angle) + dy*Math.sin(angle);
    double newY = rectangleCenter.x - dx*Math.sin(angle) - dy*Math.cos(angle);
    

提交回复
热议问题