简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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