Friday, October 2, 2020

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:

·        Translation

1.     Read the co-ordinates (x, y, z) of the object.

2.     Display the original object.

3.     Read the translation vector (tx, ty, tz).

4.     Calculate the new co-ordinates as follows:

x’ = x + tx

y’ = y + ty

z’ = z + tz

5.     Display the translated object on the screen.




·        Rotation

1.     Read the co-ordinates (x, y, z) of the object.

2.     Display the original object.

3.     Read the rotation angle Ɵ.

4.     For rotation about z-axis, calculate new co-ordinates as follows:

x’ = x * cos(Ɵ) – y * sin(Ɵ)

y’ = x * sin(Ɵ) + y * cos(Ɵ)

z’ = z

5.     Display the object after rotation about z-axis.

6.     For rotation about x-axis, calculate new co-ordinates as follows:

x’ = x

y’ = y * cos(Ɵ) – z * sin(Ɵ)

z’ = y * sin(Ɵ) + z * cos(Ɵ)

7.     Display the object after rotation about x-axis.

8.     For rotation about y-axis, calculate new co-ordinates as follows:

x’ = z * sin(Ɵ) + x * cos(Ɵ)

y’ = y

z’ = z * cos(Ɵ) – x * sin(Ɵ)

9.     Display the object after rotation about y-axis.

10.  Stop






·        Scaling

1.     Read the co-ordinates (x, y, z) of the object.

2.     Display the original object.

3.     Read the scaling factors (sx, sy, sz).

4.     Calculate the new co-ordinates as follows:

x’ = x * sx

y’ = y * sy

z’ = z * sz

5.     Display the scaled object on the screen.





Program Code

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

int maxx, maxy, midx, midy;

void drawAxis()
{
    setcolor(CYAN);
    line(midx, 0, midx, maxy);
    line(0, midy, maxx, midy);
    setcolor(WHITE);
}

void main()
{
    int gd = DETECT, gm, choice;
    int tx, ty, sx, sy, sz, ang, r, x1, x2, y1, y2;
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
    setfillstyle(3, 25);
    maxx = getmaxx();
    maxy = getmaxy();
    midx = maxx / 2;
    midy = maxy / 2;
    do
    {
        outtextxy(20, 150, "ORIGINAL OBJECT");
        drawAxis();
        bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 1);
        printf("1. Translation\n2. Rotation\n3. Scaling\n4. Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            printf("\nEnter the Translation vector (tx, ty): ");
            scanf("%d%d", &tx, &ty);
            cleardevice();
            outtextxy(100, 50, "TRANSLATION");
            drawAxis();
            bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 1);
            setcolor(GREEN);
            bar3d(midx + (tx+100), midy - (ty+20), midx + (tx+60), midy - (ty+90), 20, 1);
            getch();
            break;
        case 2:
            printf("\nEnter the Rotation angle: ");
            scanf("%d", &ang);
            r = ang * 3.14 / 180;
            x1 = 100 * cos(r) - 20 * sin(r);
            y1 = 100 * sin(r) + 20 * sin(r);
            x2 = 60 * cos(r) - 90 * sin(r);
            y2 = 60 * sin(r) + 90 * sin(r);
            cleardevice();
            drawAxis();
            outtextxy(50, 50, "Rotation about Z axis");
            bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 1);
            setcolor(GREEN);
            bar3d(midx + x1, midy - y1, midx + x2, midy - y2, 20, 1);
            getch();
            cleardevice();
            drawAxis();
            outtextxy(50, 50, "Rotation about X axis");
            bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 1);
            setcolor(GREEN);
            bar3d(midx + 100, midy - x1, midx + 60, midy - x2, 20, 1);
            getch();
            cleardevice();
            drawAxis();
            outtextxy(50, 50, "Rotation about Y axis");
            bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 1);
            setcolor(GREEN);
            bar3d(midx + x1, midy - 20, midx + x2, midy - 90, 20, 1);
            getch();
            break;
        case 3:
            printf("\nEnter the Scaling Factor (sx, sy, sz): ");
            scanf("%d %d %d", &sx, &sy, &sz);
            cleardevice();
            outtextxy(100, 50, "SCALING");
            drawAxis();
            bar3d(midx + 100, midy - 20, midx + 60, midy - 90, 20, 1);
            setcolor(GREEN);
            bar3d(midx+(sx*100), midy-(sy*20), midx+(sx*60), midy-(sy*90), 20*sz, 1);
            getch();
            break;
        default:
            break;
        }

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

    } while (choice != 4);

    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...