user3658206
user3658206

Reputation: 33

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?

Upvotes: 1

Views: 581

Answers (1)

thus spake a.k.
thus spake a.k.

Reputation: 1637

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

Upvotes: 3

Related Questions