# Two-dimensional geometric transformation in computer graphics

By utilizing the procedures for displaying output primitives and their attributes, we can generate various pictures and graphs.

Additionally, in numerous applications, there is a requirement to modify or manipulate displays.

Design applications and facility layouts are developed by arranging the sizes and orientations of the individual components within the scene.

Moreover, animations are created by moving the “camera” or objects within a scene along animation paths.

Changes in orientation, size, and shape are achieved through geometric transformations that modify the coordinate descriptions of objects.

The fundamental geometric transformations encompass translation, rotation, and scaling.

Other transformations commonly applied to objects consist of reflection and shear.

Initially, we will discuss the techniques for performing geometric transformations, followed by an examination of how transformation functions can be integrated into graphics packages.

## Basic Transformations

In computer graphics, there are essential transformations that are used to manipulate objects in a graphical environment.

These transformations enable us to modify the position, size, and orientation of objects.

The primary basic transformations in computer graphics are as follows: Translation: This transformation involves shifting an object from one location to another by adjusting its coordinates.

It allows us to change the position of an object without altering its shape or size.

Rotation: Rotation is the process of changing the orientation of an object by rotating it around a fixed point or axis.

By specifying an angle of rotation, we can rotate the object in a clockwise or counterclockwise direction.

Scaling: Scaling is used to resize an object by either enlarging or reducing its dimensions.

It enables us to adjust the size of an object along the X, Y, and Z axes, either uniformly or independently.

Reflection: Reflection involves creating a mirror image of an object by flipping it across a line, such as the X-axis or Y-axis.

It allows us to change the orientation of the object.

Shearing: Shearing is a transformation that distorts the shape of an object by slanting or skewing it along a particular axis.

It involves shifting points along one axis based on their distance from another axis.

These basic transformations serve as fundamental tools for manipulating objects in computer graphics.

By combining and applying these transformations in different ways, we can achieve a wide variety of visual effects and create complex scenes.

### Translation

Translation in computer graphics involves shifting an object’s position from one location to another. Mathematically, it can be represented as adding or subtracting specific values to the X, Y, and Z coordinates of the object. Let’s consider a 2D case for simplicity. Suppose we have an object with vertices represented by (x, y) coordinates. To translate the object by a displacement of (dx, dy), we can apply the following mathematical operations to each vertex:

New X-coordinate = Old X-coordinate + dx
New Y-coordinate = Old Y-coordinate + dy

For example, if we have a vertex at (2, 3) and we want to translate it by (1, -2), the new coordinates would be:

New X-coordinate = 2 + 1 = 3
New Y-coordinate = 3 – 2 = 1

Thus, the vertex would be translated to (3, 1) in the new position.

This mathematical representation allows us to perform translations on objects by manipulating their coordinate values. By applying these operations to all vertices of an object, we can achieve the desired translation effect in computer graphics.

Translation in computer graphics can be expressed in matrix form, both in 2D and 3D graphics. Let’s start with the 2D translation matrix:

2D Translation Matrix:

[ 1  0  dx ]
[ 0  1  dy ]
[ 0  0  1  ]


Here, dx represents the amount of translation along the x-axis, and dy represents the amount of translation along the y-axis.

To apply the 2D translation matrix to a vertex represented as a column vector [x, y, 1], we perform the matrix multiplication:

[ x' ]   [ 1  0  dx ]   [ x ]
[ y' ] = [ 0  1  dy ] * [ y ]
[ 1  ]   [ 0  0  1  ]   [ 1 ]


The resulting column vector [x’, y’, 1] represents the translated vertex in 2D.

Now, let’s move on to the 3D translation matrix:

3D Translation Matrix:

[ 1  0  0  dx ]
[ 0  1  0  dy ]
[ 0  0  1  dz ]
[ 0  0  0  1  ]


Here, dx represents the amount of translation along the x-axis, dy represents the amount of translation along the y-axis, and dz represents the amount of translation along the z-axis.

To apply the 3D translation matrix to a vertex represented as a column vector [x, y, z, 1], we perform the matrix multiplication:

[ x' ]   [ 1  0  0  dx ]   [ x ]
[ y' ] = [ 0   1  0  dy ] * [ y ]
[ z' ]   [ 0   0  1  dz ]   [ z ]
[ 1  ]   [ 0   0  0  1  ]   [ 1 ]


The resulting column vector [x’, y’, z’, 1] represents the translated vertex in 3D.

By using these translation matrices, we can efficiently apply translations to multiple vertices or objects in both 2D and 3D graphics.

### Rotation

Rotation is a fundamental transformation used in computer graphics to change the orientation of objects. It involves rotating an object around a fixed point or axis.

In 2D graphics, rotation is typically performed around the origin (0, 0) unless a different pivot point is specified. To rotate a point in 2D, we use the following mathematical equations:

New X-coordinate = (Old X-coordinate * cosθ) – (Old Y-coordinate * sinθ)
New Y-coordinate = (Old X-coordinate * sinθ) + (Old Y-coordinate * cosθ)

Here, θ represents the angle of rotation in radians. The cosine (cos) and sine (sin) functions are used to calculate the new coordinates based on the old coordinates.

For example, let’s consider a point P with coordinates (2, 3). If we want to rotate this point by an angle of 45 degrees (π/4 radians) counterclockwise, the rotation equations become:

New X-coordinate = (2 * cos(π/4)) – (3 * sin(π/4))
New Y-coordinate = (2 * sin(π/4)) + (3 * cos(π/4))

Evaluating these equations gives us the new coordinates of the rotated point P’.

New X-coordinate ≈ -0.707
New Y-coordinate ≈ 3.536

Thus, after the rotation, the point P is transformed to approximately (-0.707, 3.536).

In 3D graphics, rotation can be performed around various axes, such as the X-axis, Y-axis, or Z-axis. The rotation equations become more complex and involve matrices or quaternions to represent the rotation.

Rotation is a powerful transformation that allows objects to be reoriented in a graphical scene, enabling various visual effects and animations.

Rotation in computer graphics can be expressed in matrix form to efficiently apply the transformation to multiple points or objects. Let’s focus on 2D rotation using a matrix representation.

The 2D rotation matrix is as follows:

[ cosθ  -sinθ ]
[ sinθ   cosθ ]


Here, θ represents the angle of rotation in radians. The cosine (cos) and sine (sin) functions are used to calculate the values in the matrix based on the angle.

To apply the 2D rotation matrix to a point represented as a column vector [x, y], we perform the matrix multiplication:

[ x' ]   [ cosθ  -sinθ ]   [ x ]
[ y' ] = [ sinθ   cosθ ] * [ y ]


The resulting column vector [x’, y’] represents the rotated point.

For example, let’s consider a point P with coordinates (2, 3) and we want to rotate it by an angle of 45 degrees (π/4 radians) counterclockwise. The rotation matrix becomes:

[ cos(π/4)  -sin(π/4) ]
[ sin(π/4)   cos(π/4) ]


Substituting the values and performing the matrix multiplication:

[ x' ]   [ 0.707  -0.707 ]   [ 2 ]
[ y' ] = [ 0.707   0.707 ] * [ 3 ]


The resulting column vector [x’, y’] represents the rotated point P’.

By using the rotation matrix, we can apply the same rotation to multiple points or objects by performing the matrix multiplication for each point. This matrix representation allows for efficient and concise application of rotation in computer graphics.

### COMPOSITE TRANSFORMATIONS

Composite transformations in computer graphics refer to the combination of multiple basic transformations to achieve complex transformations on objects. By applying multiple transformations sequentially or simultaneously, we can create more intricate changes in position, size, and orientation.

There are two common methods for combining transformations: matrix multiplication and transformation concatenation.

1. Matrix multiplication: In this method, each basic transformation is represented by a matrix. To apply multiple transformations to an object, we multiply the matrices representing those transformations. The resulting matrix represents the composite transformation, which can be applied to the object’s vertices or coordinates.

For example, suppose we want to apply translation followed by rotation to an object. We represent the translation as a translation matrix and the rotation as a rotation matrix. By multiplying these matrices together, we obtain a composite transformation matrix that combines both translation and rotation. Applying this composite matrix to the object’s vertices results in the desired transformed object.

1. Transformation concatenation: In this method, each transformation is represented by a set of equations or instructions. To create a composite transformation, we concatenate the instructions for each individual transformation in the desired order.

For instance, if we want to perform rotation followed by scaling, we concatenate the rotation instructions with the scaling instructions. By sequentially applying these instructions to the object’s vertices, we achieve the composite transformation.

By utilizing composite transformations, we can create more sophisticated effects and animations in computer graphics. Objects can be moved, rotated, scaled, and subjected to various transformations in a coordinated manner, resulting in visually appealing and dynamic scenes.

### Reflection

Reflection is a transformation used in computer graphics to create mirror images of objects or scenes. It involves flipping an object or scene across a line, plane, or axis. Reflections can be applied in both 2D and 3D graphics.

In 2D graphics, a reflection can be performed across a line by reversing the coordinates of the object or scene with respect to that line. The line across which the reflection occurs is called the axis of reflection. Each point on the object is mirrored across the axis, resulting in a reflected object.

To express reflection in matrix form, we use the following matrix:

[ cos(2θ)  sin(2θ) ]
[ sin(2θ) -cos(2θ) ]


Here, θ represents the angle of the axis of reflection with respect to the x-axis.

To apply a reflection matrix to a 2D point represented as a column vector [x, y], we perform the matrix multiplication:

[ x' ]   [ cos(2θ)  sin(2θ) ]   [ x ]
[ y' ] = [ sin(2θ) -cos(2θ) ] * [ y ]


The resulting column vector [x’, y’] represents the reflected point.

In 3D graphics, reflection can be performed across a plane or axis. The reflection matrix is more complex and typically involves a combination of rotations and scaling. The specific matrix representation depends on the plane or axis of reflection.

Reflections are useful in creating symmetrical patterns, simulating mirrors, or generating interesting visual effects. By applying reflections, we can transform objects or scenes to their mirrored counterparts, providing variety and visual appeal in computer graphics applications.

### TRANSFORMATIONS BETWEEN COORDINATE SYSTEMS

Transformations between coordinate systems are used in computer graphics to convert points or objects from one coordinate system to another. This process allows for seamless integration of different coordinate systems and facilitates accurate positioning and rendering of objects in a graphical scene.

There are two primary types of transformations between coordinate systems: translation and scaling.

1. Translation: Translation involves shifting the origin of one coordinate system to a different location. This is done by adding or subtracting specific values from the coordinates of points or objects.

For example, if we have a point (x, y) in one coordinate system and we want to translate it to a new coordinate system with a different origin located at (a, b), we can apply the following translation equations:

New X-coordinate = Old X-coordinate + a
New Y-coordinate = Old Y-coordinate + b

By applying these equations to each point, we can effectively translate objects between coordinate systems.

1. Scaling: Scaling involves changing the size or dimensions of objects in a coordinate system. It can be done uniformly or non-uniformly along the x and y axes.

To scale a point or object, we multiply the coordinates by scaling factors. If we have a point (x, y) in one coordinate system and we want to scale it by factors sx and sy along the x and y axes, respectively, the scaling equations are:

New X-coordinate = Old X-coordinate * sx
New Y-coordinate = Old Y-coordinate * sy

These equations scale the object relative to the origin. If scaling relative to a different point is desired, translation can be applied first to reposition the object’s origin.

By combining translation and scaling transformations, objects can be accurately positioned and resized in different coordinate systems. This capability is vital for various applications in computer graphics, including rendering, animation, and simulation, where objects may need to be positioned and manipulated in different coordinate systems or coordinate systems with different scales or origins.

### TRANSFORMATION FUNCTIONS

Transformation functions in computer graphics are mathematical functions that define the relationship between the input coordinates of a point or object and its corresponding transformed coordinates. These functions enable the application of various geometric transformations, such as translation, rotation, scaling, shearing, and reflection.

Each transformation function takes input parameters that determine the specific transformation to be applied. For example, a translation function takes the translation distances along the x and y axes, while a rotation function takes the angle of rotation. These parameters define how the coordinates of the object or point will be modified.

Transformation functions typically operate on homogeneous coordinates, which allow for efficient matrix operations. Homogeneous coordinates include an additional coordinate value, often denoted as w, which helps represent translations and perspective transformations.

The transformation functions take the input coordinates (x, y, w) and perform mathematical operations to obtain the transformed coordinates (x’, y’, w’). The specific equations or algorithms used in these functions depend on the type of transformation being applied.

For example, a translation function would add the translation distances to the x and y coordinates:

New x-coordinate = Old x-coordinate + translation distance in x-axis
New y-coordinate = Old y-coordinate + translation distance in y-axis
New w-coordinate = Old w-coordinate (remains unchanged for translations)

A rotation function would apply trigonometric functions to rotate the coordinates based on the angle of rotation:

New x-coordinate = Old x-coordinate * cos(angle) – Old y-coordinate * sin(angle)
New y-coordinate = Old x-coordinate * sin(angle) + Old y-coordinate * cos(angle)
New w-coordinate = Old w-coordinate (remains unchanged for rotations)

Similar equations or algorithms exist for other types of transformations, each with its own specific formulas.

By applying these transformation functions to points or objects, we can achieve the desired geometric transformations and manipulate their position, orientation, size, or shape. These functions form the basis of graphics packages and applications where transformations are crucial for creating and manipulating graphical content.

### RASTER METHODS FOR TRANSFORMATIONS

Raster methods for transformations in computer graphics are techniques used to apply geometric transformations to raster images or pixel-based representations of objects or scenes. These methods are employed when dealing with discrete data, such as images composed of pixels, as opposed to continuous geometric primitives.

Raster methods involve iterating over each pixel in the original image and determining its transformed position in the transformed image. The transformation is typically defined by a set of mathematical equations or algorithms.

Here are some common raster methods for transformations:

1. Nearest Neighbor: The nearest neighbor method is the simplest raster transformation technique. It involves mapping each pixel in the transformed image to the nearest corresponding pixel in the original image. This method is fast but can result in aliasing or jagged edges, especially when scaling an image up or down.
2. Bilinear Interpolation: Bilinear interpolation is a more advanced method that calculates the intensity or color values of transformed pixels based on the weighted average of the surrounding pixels in the original image. This technique produces smoother results than nearest neighbor, reducing the aliasing artifacts.
3. Barycentric Coordinates: Barycentric coordinates can be used for more complex transformations, such as affine transformations. This method involves dividing the transformed image into triangles and calculating the barycentric coordinates of each pixel within the triangles. The barycentric coordinates determine the contributions of the surrounding vertices in the original image to calculate the color or intensity values of the transformed pixel.
4. Transformations with Matrices: When working with homogeneous coordinates, matrix operations can be used for efficient raster transformations. By representing the transformation matrix, such as translation, rotation, or scaling, the original image can be multiplied by the matrix to obtain the transformed image.

These raster methods are essential for applying geometric transformations to raster images or pixel-based data. They enable the manipulation of images in terms of translation, rotation, scaling, and other transformations, allowing for a wide range of visual effects and modifications in computer graphics applications.