简体   繁体   English

旋转矩形并计算角

[英]Rotating Rectangle and calculating corner

I'm rotating a rectangle and now want to calculate the new position of the upper left corner. 我正在旋转一个矩形,现在想要计算左上角的新位置。

my current calculation is: 我目前的计算是:

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

The calculation doesn't work. 计算不起作用。 Can somebody see the error? 有人能看到错误吗?

You need to subtract the midpoint of the rectangle before you rotate it and then add it back afterwards, otherwise you're rotating the corner about the origin (0,0) 您需要在旋转之前减去矩形的中点,然后再将其添加回来,否则您将围绕原点旋转角点(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