|
The C# Column
Transformation in GDI+ - 1
As
compared to GDI, GDI+ provides greater support for graphics, images
and transformation. GDI+ allows following the operations to be performed.
- Translation: On translating an object, the size of the
object remains fixed but it moves along any one of the axes along which the
translation takes place. Moving object 10 units in x direction is an example
of translation transformation.
- Rotation: On rotating an object, the object moves
about one of the axes.
- Scaling: On scaling an object, its size is increased or
decreased but the shape does not change.
- Shearing: On shearing an object, it moves in some direction
but its base remains fixed.
Transformations are always applied using matrices. It is
a two-step process. The steps involved are:
(a) Creating a transformation matrix and coordinate matrix.
(b) Multiplying the coordinate matrix with transformation
matrix.
Translation
Let us understand this form of transformation with the help of an example.
We plan to translate a triangle shown in this figure by three
units in x direction and two units in y direction. For doing this we need to
change all the three coordinates of the triangle. The new coordinates can be
obtained by multiplying the transformation matrix with the coordinate matrix.
In the transformation matrix the elements 3 and 2 are the units by which we
wish to translate the shape in the x and y directions respectively. The coordinate
matrix has three elements. The first element consists of the x coordinate of
the vertex, which is to be transformed. The second element consists of y coordinate
of the vertex and the last element consists of the z coordinate of the vertex.
Hence the first and the second elements of the coordinate matrix would be 2,
2 respectively and as a triangle does not have any z order, the last element
of the coordinate matrix would contain 1. After the multiplication is performed
the coordinates (2, 2) would get transformed into (5, 4) respectively.
Rotation
Suppose
we want to rotate the triangle shown in the above figure by 90 degrees. To do
so, we need to replace the transformation matrix angle A by 90 degrees. The
coordinate matrix would contain the coordinates of the triangle to be transformed.
The figure shows the transformation of only one of the coordinates of the triangle
i.e. (2, 4). As the triangle does not have any z order the third element of
the coordinate matrix would be 1. The resultant matrix shows that the coordinate
(2, 4) has changed to (-4, 2). Similarly we need to transform other two coordinates
of the triangle.
Scaling
The following figure shows the transformation matrix for
scaling.
Suppose
the triangle is to be scaled by two units in x direction and two units in y
direction. Hence sx and sy in the transformation matrix would be two units each.
The coordinate matrix would contain the particular coordinate of the triangle
to be transformed, which in our case happens to be (4, 1). On multiplying the
coordinate matrix with the transformation matrix we get the resultant matrix
which shows that the coordinate (4, 1) has been transformed into (8, 2). The
effect of scaling is that the figure either enlarges or reduces in size.
Shearing
The following figure shows the shearing transformation.
Suppose
the triangle is to be sheared two units in x direction only. Hence sx in the
transformation matrix would be 2 whereas sy would be 0. The coordinate matrix
would contain the particular coordinate of the triangle to be transformed, which
in our case happens to be (1, 4). On multiplying the coordinate matrix with
the transformation matrix we get the resultant matrix, which shows that the
coordinate (1, 4) has been transformed into (9, 4). On performing similar shearing
operation on all other coordinates we would get the resultant triangle as shown
in the figure.
We will see how to perform all these transformations programmatically
in the next article.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert, speaker
and author with several best-sellers to his credit, including titles like
“Let Us C” and the “Fundas” series. Contact him at kanetkar@dcubesoft.com |
|