In my computer class, a rotation of a graphic of Sonic the Hedgehog was attempted. The graphic was in a jpg format, and the method chosen was to write code in Java which read the graphic into a
BufferedImage object. The idea is to copy the graphic into another
BufferedImage object, reversing the order of pixel reading to obtain what amounts to a rotation in the new object. The image object is then written to a new JPG file. After the run, the new file is inspected.
This article is a bare-bones know-nothing introduction to how to think of reading and writing graphics that avoid the implementation details as much as possible. Documentation on the details of
BufferedImage are plentiful on the internet, as well as information on computer graphics generally.
In a learning activity, students in grades 10 and 11 were asked to modify the statements:
int w2 = x; int h2 = y;
(w2, h2) becomes the new placement for the pixel when the graphic is rotated. Students were instructed not to modify any other part of the program, which performed object declarations, file opening and closing, try/catch statements and other concepts considered too advanced for this stage of their course. Students were encouraged to use trial-and error and view the consequences of various general formulas they were to try out.
height were already declared and given values in the code.
The tutorial below was given to the students.
In general terms, for any graphic of dimensions , can have any value in the domain of to , while can have values in the range of to .
Graphics are made of little units of colour information which end up on your computer screen as pixels. Think of each unit of information as belonging to a part of that graphic on a Cartesian coordinate plane. Much like the coordinate plane you learn about in grade 9 and 10, and represent the position of the pixel horizontally and vertically.
If the graphic is 100 units across by 200 units in the vertical direction, then the graphic is said to have dimensions , in terms of pixels. This means all pixels in a graphic will have some location . Because has to start from 0, it can take on values between 0 to 99 for this graphic, while can take on values between 0 and 199.
In our code, that means that the dimensions of the graphic is
width, and while and are fine (for the original graphic), a formula must be applied so that
w2 (the and values for the rotated graphic) become appropriate for a rotation. With the statements written as they are, all you will get are two copies of the same graphic.
It is expected that the formula should be simple, but beware of values going beyond the range of the graphic dimensions. That will result in a crash. The dimensions of the graphic are given as
width, and it shouldn’t matter all that much what their real values are, you might want to insert printf statements where you can trace
w2 against the values of
width to see what is going on.
It is also noteworthy that you can only carry the Cartesian plane analogy so far. represents the pixel in the top left corner of the graphic, while for a graphic of dimensions
w, the coordinate
(w-1, h-1) will represent the pixel on the bottom right. The first difference is that no negative values are possible. The second difference is that the y-axis is
upside-down, because it increases downward. The -axis is still fine, however, increasing from left to right as usual.