wrldwzrd89

Nov 14, 2010, 12:58 PM

Here's my rotation code, as it stands now:

private static BufferedImage rotateImage(BufferedImage inputImage,

double[] transformData) {

BufferedImage outputImage = new BufferedImage(inputImage.getWidth(),

inputImage.getHeight(), inputImage.getType());

int degrees = (int) transformData[0];

int xPrime, yPrime;

int xTrans = (int) (inputImage.getWidth() * Math.sin(degrees));

int yTrans = (int) (inputImage.getHeight() * Math.cos(degrees));

for (int x = 0; x < inputImage.getWidth(); x++) {

for (int y = 0; y < inputImage.getHeight(); y++) {

xPrime = (int) (x * Math.cos(degrees) - y * Math.sin(degrees) + xTrans);

yPrime = (int) (x * Math.sin(degrees) + y * Math.cos(degrees) + yTrans);

outputImage.setRGB(xPrime, yPrime, inputImage.getRGB(x, y));

}

}

return outputImage;

}

The transformData array contains the degrees to rotate as the first (and only) element. However, this is not working - the problem appears to be one of coordinates being out of bounds for the image after rotation. Obviously they need to be adjusted after the formula is applied (which I found on Wikipedia - here (http://en.wikipedia.org/wiki/Rotation_%28mathematics%29)). I cannot figure out the right offset.

private static BufferedImage rotateImage(BufferedImage inputImage,

double[] transformData) {

BufferedImage outputImage = new BufferedImage(inputImage.getWidth(),

inputImage.getHeight(), inputImage.getType());

int degrees = (int) transformData[0];

int xPrime, yPrime;

int xTrans = (int) (inputImage.getWidth() * Math.sin(degrees));

int yTrans = (int) (inputImage.getHeight() * Math.cos(degrees));

for (int x = 0; x < inputImage.getWidth(); x++) {

for (int y = 0; y < inputImage.getHeight(); y++) {

xPrime = (int) (x * Math.cos(degrees) - y * Math.sin(degrees) + xTrans);

yPrime = (int) (x * Math.sin(degrees) + y * Math.cos(degrees) + yTrans);

outputImage.setRGB(xPrime, yPrime, inputImage.getRGB(x, y));

}

}

return outputImage;

}

The transformData array contains the degrees to rotate as the first (and only) element. However, this is not working - the problem appears to be one of coordinates being out of bounds for the image after rotation. Obviously they need to be adjusted after the formula is applied (which I found on Wikipedia - here (http://en.wikipedia.org/wiki/Rotation_%28mathematics%29)). I cannot figure out the right offset.