旋转矩形并计算角

时间:2014-05-20 20:03:13

标签: java math user-interface graphics

我正在旋转一个矩形,现在想要计算左上角的新位置。

我目前的计算是:

Point upLeft = new Point(
        // x-coordinate
                (int) Math.round((oldx * Math.cos(objectAngleRad))
                        - (oldy * Math.sin(objectAngleRad))),
                // y-coordinate
                (int) Math.round((oldx * Math.sin(objectAngleRad))
                        + (oldy * Math.cos(objectAngleRad))));

计算不起作用。有人能看到错误吗?

1 个答案:

答案 0 :(得分:3)

您需要在旋转之前减去矩形的中点,然后再将其添加回来,否则您将围绕原点旋转角落(0,0)

Point upLeft = new Point(
    // x-coordinate
        (int) Math.round(midx + ((oldx-midx) * Math.cos(objectAngleRad))
                              - ((oldy-midy) * Math.sin(objectAngleRad))),
    // y-coordinate
        (int) Math.round(midy + ((oldx-midx) * Math.sin(objectAngleRad))
                              + ((oldy-midy) * Math.cos(objectAngleRad))));
相关问题