We'll look at four basic transformations. From these simple operations, more complex functions can be constructed.Rotation
A rotation matrix will allow us to spin the image clockwise or counterclockwise around the origin. This matrix makes use of the trigonometric functions sine and cosine. The trigonometric functions take as an argument an angle of rotation; positive angles will give a counterclockwise rotation, negative values a clockwise one. Angles are commonly measured on two scales. Most people are experienced with units of degrees -- a complete circle has 360 degrees. Computers measure angles in units of radians. To a computer, a circle has radians. We will convert degrees to radians by multiplying the degrees of our angle by pi divided by 180. That will give us the correct values for our matrix. The rotation matrix itself is defined as:
where represents the angle of rotation we desire.
After replacing the identity matrix in the example code with the following and running the code, we see the house is now rotated counterclockwise by 30 degrees.
angle = 30 * pi/180 trans = array([[cos(angle), -sin(angle)], [sin(angle), cos(angle)]])
Using the rotation matrix, any point or collection of points can be easily rotated around the center point (0,0).
Next on the list of transformations is the reflection matrix. A reflection matrix will cause the image to be flipped, or reflected across an axis. An example of a reflection matrix that causes all the y values to be negated (thus flipping the image across the x axis) is:
Switching the rotation matrix with the reflection one generates the following:
Try to figure the reflection matrix that negates only the x coordinates. Is there a matrix that negates both the x and y coordinates at the same time?
Another simple transformation is the scale matrix. The identity matrix can be modified to have values of other than one on the diagonal. As such, it becomes a scale matrix, changing only the size of the drawing. This matrix can be used to both scale up, a>1, and scale down, a<1 :
The last matrix we will explore is the "shear" matrix. This causes a shear motion in one particular dimension.
The result can be seen below for a shear matrix where k=2.