Explain the basic 2D geometric transformations in detail with a snippet of code for each

3.A] Explain the basic 2D geometric transformations in detail with a snippet of code for each

Answer:-

2D Geometric Transformations

Basic 2D Geometric Transformations


1. Translation

Description: Moves an object from one position to another.

Matrix Representation:

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

Code Snippet (C):

    #include <stdio.h>

    int main() {
        float x = 100.0, y = 100.0;
        float dx = 50.0, dy = 30.0;

        printf("Original point: (%f, %f)\n", x, y);
        x += dx;
        y += dy;
        printf("Translated point: (%f, %f)\n", x, y);

        return 0;
    }
    

2. Rotation


Description: Rotates an object around a point (usually the origin) by a given angle.

Matrix Representation:

    [ cos(θ) -sin(θ) 0 ]
    [ sin(θ)  cos(θ) 0 ]
    [    0       0    1 ]
    

Code Snippet (C):

    #include <stdio.h>
    #include <math.h>

    int main() {
        float x = 100.0, y = 100.0;
        float angle = 45.0; // Angle in degrees

        printf("Original point: (%f, %f)\n", x, y);
        float radians = angle * M_PI / 180.0; // Convert angle to radians
        float x_new = x * cos(radians) - y * sin(radians);
        float y_new = x * sin(radians) + y * cos(radians);
        x = x_new;
        y = y_new;
        printf("Rotated point: (%f, %f)\n", x, y);

        return 0;
    }
    

3. Scaling


Description: Changes the size of an object by scaling it along the x and y axes.

Matrix Representation:

    [ sx  0  0 ]
    [ 0  sy  0 ]
    [ 0   0  1 ]
    

Code Snippet (C):

    #include <stdio.h>

    int main() {
        float x = 100.0, y = 100.0;
        float sx = 2.0, sy = 0.5;

        printf("Original point: (%f, %f)\n", x, y);
        x *= sx;
        y *= sy;
        printf("Scaled point: (%f, %f)\n", x, y);
        return 0;
    }
    

4. Reflection


Description: Flips an object over a specified axis.

Matrix Representations:

Axis Matrix Representation
Over x-axis
                    [ 1  0  0 ]
                    [ 0 -1  0 ]
                    [ 0  0  1 ]
                    
Over y-axis
                    [ -1  0  0 ]
                    [  0  1  0 ]
                    [  0  0  1 ]
                    

Code Snippets (C):

    #include <stdio.h>

    int main() {
        float x = 100.0, y = 100.0;

        printf("Original point: (%f, %f)\n", x, y);

        y = -y;
        printf("Reflected over x-axis: (%f, %f)\n", x, y);

        // Reset point
        x = 100.0, y = 100.0;
        x = -x;
        printf("Reflected over y-axis: (%f, %f)\n", x, y);

        return 0;
    }
    

Leave a Reply

Your email address will not be published. Required fields are marked *