Rotation.java
Below is the syntax highlighted version of Rotation.java
from §3.1 Using Data Types.
/*************************************************************************
* Compilation: javac Rotation.java
* Execution: java Oil filename angle
*
* Rotate image a given number of degrees counterclockwise.
*
* % java Rotation baboon.jpg 30
*
*
*************************************************************************/
import java.awt.Color;
public class Rotation {
public static void main(String args[]) {
Picture pic1 = new Picture(args[0]);
pic1.show();
int width = pic1.width();
int height = pic1.height();
double angle = Math.toRadians(Double.parseDouble(args[1]));
double sin = Math.sin(angle);
double cos = Math.cos(angle);
double i0 = 0.5 * (width - 1);
double j0 = 0.5 * (height - 1);
Picture pic2 = new Picture(width, height);
// rotation
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int ii = (int) (-(i - i0) * cos + (j - j0) * sin + i0);
int jj = (int) ( (i - j0) * sin + (j - j0) * cos + j0);
// plot pixel (i, j) the same color as (ii, jj) if it's in bounds
if (ii >= 0 && ii < width && jj >= 0 && jj < height)
pic2.setColor(i, j, pic1.getColor(ii, jj));
}
}
pic2.show();
}
}
Last updated: Wed Oct 20 07:05:38 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.