Search code examples
javarotationarea

Workout area of rotated Buffered Image


I'm trying work out the boarder of a bufferedimage after using an affine translation to rotate it as below:

AffineTransform at = new AffineTransform();
at.translate(x, y);
at.translate(0.5*image.getHeight(), 0.5*image.getWidth());
at.rotate(Math.PI/3);
at.translate(-0.5*image.getWidth(), -0.5*image.getHeight());
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); 
BufferedImage anotherImage = op.filter(image, null);
AffineTransform at = new AffineTransform();

       at.translate(x, y);
       at.translate(0.5*image.getHeight(), 0.5*image.getWidth());
       at.rotate(Math.PI/3);
       at.translate(-0.5*image.getWidth(), -0.5*image.getHeight());
        AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); 
       BufferedImage anotherImage = op.filter(image, null);

How would work out the boarder after this translation, so I can draw a rectangle around the BufferedImage? I am trying to create a collision detection system so what to work out the border of the buffered image so I can tell if it has collided with any other objects.


Solution

  • If you want to tilt, then this can be done:

    public static BufferedImage tilt(BufferedImage image, double angle, GraphicsConfiguration gc) {
        double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
        int w = image.getWidth(), h = image.getHeight();
        int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
        int transparency = image.getColorModel().getTransparency();
        BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
        Graphics2D g = result.createGraphics();
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.rotate(angle, w / 2, h / 2);
        g.drawRenderedImage(image, null);
        return result;
    }
    

    And then call the above function using this:

    /**
     * Takes an image and tilts it by angle. Positive angle implies tilt in
     * clock-wise direction. Negative angle implies in anti-clockwise. Returns
     * null if invalid file.
     *
     *
     * @param image image to be tilted. It assumes that the image is of valid
     * image format and not some random file.
     * @param angle Angle to be rotate clockwise. Ex: Math.PI/2, -Math.PI/4
     * @retun file after tilting angle. Null if not an image
     */
    public static File tiltImageByAngle(File image, double angle, BufferedImage original) {
    GraphicsConfiguration gc = getDefaultConfiguration();
        BufferedImage rotated1 = tilt(original, angle, gc);
        try {
            //write iamge
            File ans = new File(System.getProperty("java.io.tmpdir")+"temp"+angle+"."+getFileExtension(image.getName())); 
                    //new File("temp" + (int) (Math.random() * 100000) + "." + getFileExtension(image.getName()));
            ImageIO.write(rotated1, getFileExtension(image.getName()), ans);
            return ans;
        } catch (IOException ex) {
            ImageRename.LOG.log(Level.SEVERE, null, ex);
            return null;
        }
    }
    

    Hopefully this should help