繁体   English   中英

旋转矩形并计算角

[英]Rotating Rectangle and calculating corner

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

我目前的计算是:

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))));

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

您需要在旋转之前减去矩形的中点,然后再将其添加回来,否则您将围绕原点旋转角点(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))));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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