Work out the maximum image size?

I am creating a small Java game and I have an image that is rotated.

enter image description here

As you can see in the two images below, there is a giant ship that spins slowly in the game, but when it gets to a certain point, it gets clipped (due to its small BufferedImage).

Here is my rendering code:

public void drawImageRotated(BufferedImage img, double x, double y, double scale,    double angle) {
        x -= xScroll;
        y -= yScroll;  
        BufferedImage image = new BufferedImage((int)(img.getWidth() * 1.5D), (int)(img.getHeight() * 1.5D), 2);
        Graphics2D g = (Graphics2D)image.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.rotate(Math.toRadians(angle), image.getWidth() / 2, image.getHeight() / 2);
        g.drawImage(img, image.getWidth() / 2 - img.getWidth() / 2, image.getHeight() / 2 - image.getHeight() / 2, null);
        g2d.drawImage(image, (int)(x-image.getWidth()*scale/2), (int)(y-image.getHeight()*scale/2), (int)(image.getWidth()*scale), (int)(image.getHeight()*scale), null);
        g.dispose();      
 }

      

Back to the question of how can I determine the maximum x and y image sizes during rotation to compensate for the size of the buffered images?

+3


source to share


4 answers


If you have a mostly rectangular image that rotates around its center, the maximum width and height during rotation will be when the diagonal of the image rectangle is horizontal or vertical. This diagonal distance can be calculated using the Pythagorean theorem and used for width and height BufferedImage

.



    int size = (int) Math.sqrt((img.getWidth() * img.getWidth()) + (img.getHeight() * img.getHeight()));
    BufferedImage image = new BufferedImage(size, size, 2);
    // The rest of your code as before

      

+1


source


how can I determine the maximum x and y image size during rotation to compensate for the size of the buffered images?

double sin = Math.abs(Math.sin(angle));
double cos = Math.abs(Math.cos(angle));
int w = image.getWidth();
int h = image.getHeight();
int neww = (int)Math.floor(w*cos+h*sin);
int newh = (int)Math.floor(h*cos+w*sin);

      



The above code was taken from this example: Java (SWING) working with Rotation

+1


source


An alternative is to rotate the actual object Graphics

, draw the image, and restore the rotation:

AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(angle), x + image.getWidth() / 2, y + image.getWidth() / 2);
g2d.drawImage(image, x, y, null);
g2d.setTransform(old);

      

0


source


Let be the width

width of the original image, height

its original height and the angle

value of the rotation angle in radians.

According to my calculations, the size of the rotated image looks like this:

rotatedWidth = Math.cos(angle) * width + Math.sin(angle) * height;
rotatedHeight = Math.sin(angle) * width + Math.cos(angle) * height;

      

You might also need to take a look at this thread as it might help.

0


source







All Articles