Wednesday, September 23, 2020

C Program to implement 2D Composite Transformation

Aim

To write a C program to implement 2D composite transformations such as consecutive translation, consecutive rotation, pivot point rotation and consecutive scaling.

Algorithm

Consecutive Translation

  1. Input the co-ordinates of the object.
  2. Draw the object on the screen.
  3. Input the 1st and 2nd translation co-ordinates.
  4. For each translation, add the translation co-ordinates to the object co-ordinates.
  5. Draw the translated object on the screen.


Consecutive Rotation

  1. Input the co-ordinates of the object.
  2. Draw the object on the screen.
  3. Input the 1st and 2nd rotation angles.
  4. For each rotation, calculate r = (Ɵ * 3.14) / 180.
  5. Calculate xn = xi * cos(r) - yi * sin(r).
  6. Calculate yn = yi * cos(r) + xi * sin(r).
  7. Draw the rotated object on the screen.


Pivot Point Rotation

  1. Input the co-ordinates of the object.
  2. Draw the object on the screen.
  3. Input the pivot point co-ordinates.
  4. Input the rotation angle.
  5. Translate the object to the pivot point co-ordinates.
  6. Rotate the object with the given angle.
  7. Translate the rotated object back to the original co-ordinates.


Consecutive Scaling

  1. Input the co-ordinates of the object.
  2. Draw the object on the screen.
  3. Input the scaling factors.
  4. For each scaling transformation, multiply the object co-ordinates by the scaling factors.
  5. Draw the scaled object on the screen.



Program Code

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

void drawTri(int x[], int y[])
{
    int i;
    for (i = 0; i < 3; i++)
    {
        if (i == 2)
            line(x[i], y[i], x[0], y[0]);
        else
            line(x[i], y[i], x[i+1], y[i+1]);
    }
}

void main()
{
    int gd = DETECT, gm;
    int choice, x[3], y[3], i, j, n;
    int tx1, tx2, ty1, ty2, theta1, theta2, px, py, sx[10], sy[10];
    float tx, ty, r;
    clrscr();
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

    do
    {
        printf("1. Consecutive Translation\n2. Consecutive Rotation\n3. Pivot Point Rotation\n4. Consecutive Scaling\n5. Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);

        if (choice != 5)
        {
            printf("\nEnter the co-ordinates of Triangle...\n");
            for (i = 0; i < 3; i++)
            {
                printf("(x%d, y%d): ", i+1, i+1);
                scanf("%d %d", &x[i], &y[i]);
            }
        }

        switch (choice)
        {
        case 1:
            printf("\nEnter the 1st translation (tx1, ty1): ");
            scanf("%d %d", &tx1, &ty1);
            printf("\nEnter the 2nd translation (tx2, ty2): ");
            scanf("%d %d", &tx2, &ty2);
            cleardevice();
            drawTri(x, y);
            for (i = 0; i < 3; i++)
            {
                x[i] += tx1;
                y[i] += ty1;
            }
            setcolor(3);
            drawTri(x, y);
            for (i = 0; i < 3; i++)
            {
                x[i] += tx2;
                y[i] += ty2;
            }
            setcolor(GREEN);
            drawTri(x, y);
            getch();
            break;
        case 2:
            printf("\nEnter the 1st rotation angle: ");
            scanf("%d", &theta1);
            printf("Enter the 2nd rotation angle: ");
            scanf("%d", &theta2);
            cleardevice();
            drawTri(x, y);
            r = M_PI * theta1 / 180;
            for (i = 0; i < 3; i++)
            {
                tx = x[i];
                ty = y[i];
                x[i] = tx * cos(r) - ty * sin(r);
                y[i] = tx * sin(r) + ty * cos(r);
            }
            setcolor(3);
            drawTri(x, y);
            r = M_PI * theta2 / 180;
            for (i = 0; i < 3; i++)
            {
                tx = x[i];
                ty = y[i];
                x[i] = tx * cos(r) - ty * sin(r);
                y[i] = tx * sin(r) + ty * cos(r);
            }
            setcolor(GREEN);
            drawTri(x, y);
            getch();
            break;
        case 3:
            printf("\nEnter the pivot point (px, py): ");
            scanf("%d %d", &px, &py);
            printf("Enter the rotation angle: ");
            scanf("%d", &theta1);
            cleardevice();
            drawTri(x, y);
            for (i = 0; i < 3; i++)
            {
                x[i] = x[i] + px;
                y[i] = y[i] + py;
            }
            setcolor(3);
            drawTri(x, y);
            r = M_PI * theta1 / 180;
            for (i = 0; i < 3; i++)
            {
                tx = x[i];
                ty = y[i];
                x[i] = tx * cos(r) - ty * sin(r);
                y[i] = tx * sin(r) + ty * cos(r);
            }
            setcolor(BLUE);
            drawTri(x, y);
            for (i = 0; i < 3; i++)
            {
                x[i] = x[i] - px;
                y[i] = y[i] - py;
            }
            setcolor(GREEN);
            drawTri(x, y);
            getch();
            break;
        case 4:
            printf("\nEnter the number of scaling transformations: ");
            scanf("%d", &n);
            for (i = 0; i < n; i++)
            {
                printf("Enter the scaling unit (sx%d, sy%d): ", i+1, i+1);
                scanf("%d %d", &sx[i], &sy[i]);
            }
            cleardevice();
            drawTri(x, y);
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    x[j] = x[j] * sx[i];
                    y[j] = y[j] * sy[i];
                }
                setcolor(i + 1);
                drawTri(x, y);
            }
            getch();
            break;
        default:
            break;
        }

        clrscr();
        cleardevice();
        setcolor(WHITE);

    } while (choice != 5);
    closegraph();
}

No comments:

Post a Comment

3D Transformation Graphics Program in C

Aim To write a C program to implement 3D transformations such as translation, rotation and scaling of objects. Algorithm: ·         Tr...