Showing posts with label Rotation. Show all posts
Showing posts with label Rotation. Show all posts

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();
}

Cohen Sutherland Line Clipping Algorithm in C

Aim

To write a C graphics program to implement Cohen Sutherland line clipping algorithm.

Algorithm

      1. Read 2 end points of line as p1(x1, y1) and p2(x2, y2).

    2.  Read 2 corner points of the clipping window (left-top and right-bottom) as (wx1, wy1) and (wx2, wy2).

      3.     Assign the region codes for 2 endpoints p1 and p2 using following steps:

·        Initialize code with 0000

·        Set bit 1 if x<wx1

·        Set bit 2 if x>wx2

·        Set bit 3 if y<wy2

·        Set bit 4 if y>wy1

     4.     Check for visibility of line

a. If region codes for both endpoints are zero then line is completely visible. Draw the line go to step 9.

b.   If region codes for endpoints are not zero and logical ANDing of them is also nonzero then line is invisible. Discard the line and move to step 9.

c.   If it does not satisfy 4.a and 4.b then line is partially visible.

     5.     Determine the intersecting edge of clipping window as follows:

·      If region codes for both endpoints are nonzero find intersection points p1’ and p2’ with boundary edges.

·    If region codes for any one end point is non zero then find intersection point p1’ or p2’.

     6.     Divide the line segments considering intersection points.

     7.     Reject line segment if any end point of line appears outside of any boundary.

     8.     Draw the clipped line segment.

9.   Stop.

Program Code

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

typedef struct coordinate
{
    int x, y;
    char code[4];
} PT;

void drawwindow();
void drawline(PT p1, PT p2);
PT setcode(PT p);
int visibility(PT p1, PT p2);
PT resetendpt(PT p1, PT p2);

void main()
{
    int gd = DETECT, v, gm;
    PT p1, p2, p3, p4, ptemp;
    clrscr();

    printf("\nEnter x1 and y1: ");
    scanf("%d %d", &p1.x, &p1.y);
    printf("\nEnter x2 and y2: ");
    scanf("%d %d", &p2.x, &p2.y);

    initgraph(&gd, &gm, "c:\\turboc3\\bgi");
    outtextxy(50, 50, "Before Clipping");
    drawwindow();
    drawline(p1, p2);
    getch();
    cleardevice();
    p1 = setcode(p1);
    p2 = setcode(p2);
    v = visibility(p1, p2);
    outtextxy(50, 50, "After Clipping");

    switch (v)
    {
    case 0:
        drawwindow();
        drawline(p1, p2);
        break;
    case 1:
        drawwindow();
        break;
    case 2:
        p3 = resetendpt(p1, p2);
        p4 = resetendpt(p2, p1);
        drawwindow();
        drawline(p3, p4);
        break;
    }

    getch();
    closegraph();
}

void drawwindow()
{
    line(150, 100, 450, 100);
    line(450, 100, 450, 350);
    line(450, 350, 150, 350);
    line(150, 350, 150, 100);
}

void drawline(PT p1, PT p2)
{
    line(p1.x, p1.y, p2.x, p2.y);
}

PT setcode(PT p)
{
    PT ptemp;

    if (p.y < 100)
        ptemp.code[0] = '1';
    else
        ptemp.code[0] = '0';

    if (p.y > 350)
        ptemp.code[1] = '1';
    else
        ptemp.code[1] = '0';

    if (p.x > 450)
        ptemp.code[2] = '1';
    else
        ptemp.code[2] = '0';

    if (p.x < 150)
        ptemp.code[3] = '1';
    else
        ptemp.code[3] = '0';

    ptemp.x = p.x;
    ptemp.y = p.y;

    return (ptemp);
}

int visibility(PT p1, PT p2)
{
    int i, flag = 0;

    for (i = 0; i < 4; i++)
    {
        if ((p1.code[i] != '0') || (p2.code[i] != '0'))
            flag = 1;
    }

    if (flag == 0)
        return (0);

    for (i = 0; i < 4; i++)
    {
        if ((p1.code[i] == p2.code[i]) && (p1.code[i] == '1'))
            flag = '0';
    }

    if (flag == 0)
        return (1);

    return (2);
}

PT resetendpt(PT p1, PT p2)
{
    PT temp;
    int x, y, i;
    float m, k;

    if (p1.code[3] == '1')
        x = 150;

    if (p1.code[2] == '1')
        x = 450;

    if ((p1.code[3] == '1') || (p1.code[2] == '1'))
    {
        m = (float)(p2.y - p1.y) / (p2.x - p1.x);
        k = (p1.y + (m * (x - p1.x)));
        temp.y = k;
        temp.x = x;

        for (i = 0; i < 4; i++)
            temp.code[i] = p1.code[i];

        if (temp.y <= 350 && temp.y >= 100)
            return (temp);
    }

    if (p1.code[0] == '1')
        y = 100;

    if (p1.code[1] == '1')
        y = 350;

    if ((p1.code[0] == '1') || (p1.code[1] == '1'))
    {
        m = (float)(p2.y - p1.y) / (p2.x - p1.x);
        k = (float)p1.x + (float)(y - p1.y) / m;
        temp.x = k;
        temp.y = y;

        for (i = 0; i < 4; i++)
            temp.code[i] = p1.code[i];

        return (temp);
    }
    else
        return (p1);
}

Output




Wednesday, September 16, 2020

2D Transformation Graphics Program in C

Aim

To write a C program to implement two dimensional transformations such as translation, rotation, scaling, shearing and reflection on objects.

Algorithm

Translation

  1. Input the line endpoints (x1, y1) and (x2, y2).
  2. Input the translation co-ordinates (tx, ty).
  3. Draw the line with endpoints (x1, y1) and (x2, y2).
  4. Add tx to the co-ordinates x1 and x2.
  5. Add ty to the co-ordinates y1 and y2.
  6. Draw the translated line with endpoints (x1, y1) and (x2, y2).



Rotation

  1. Input the line endpoints (x1, y1) and (x2, y2).
  2. Input the rotation angle Ɵ.
  3. Draw the line with endpoints (x1, y1) and (x2, y2).
  4. Calculate r = (Ɵ * 3.14) / 180.
  5. Calculate xn = x2 * cos(r) - y2 * sin(r).
  6. Calculate yn = y2 * cos(r) + x2 * sin(r).
  7. Draw the rotated line with endpoints (x1, y1) and (xn, yn).



Scaling

  1. Input the 3 endpoints of the triangle (x1, y1), (x2, y2) and (x3, y3).
  2. Input the scaling factors sx and sy.
  3. Draw the triangle with endpoints (x1, y1), (x2, y2) and (x3, y3).
  4. Update x1, x2 and x3 by multiplying sx to them.
  5. Update y1, y2 and y3 by multiplying sy to them.
  6. Draw the scaled triangle with points (x1, y1), (x2, y2) and (x3, y3).



Shearing

  1. Input the 4 endpoints of the rectangle (x1, y1), (x2, y2), (x3, y3) and (x4, y4).
  2. Input the value for shearing ‘sh’.
  3. Draw the rectangle with endpoints (x1, y1), (x2, y2), (x3, y3) and (x4, y4).
  4. If shearing along X-axis, update x1, x2, x3 and x4 by adding (sh*y1), (sh*y2), (sh*y3) and (sh*y4) to them respectively.
  5. If shearing along Y-axis, update y1, y2, y3 and y4 by adding (sh*x1), (sh*x2), (sh*x3) and (sh*x4) to them respectively.
  6. Draw the sheared rectangle with endpoints (x1,y1), (x2, y2), (x3, y3), (x4, y4).

X axis shear


Y axis Shear



Reflection

  1. Input the 3 endpoints of the triangle (x1, y1), (x2, y2) and (x3, y3).
  2. Calculate middle value of X axis as ‘midx’ and Y axis as ‘midy’.
  3. Draw the triangle with endpoints (x1, y1), (x2, y2) and (x3, y3).
  4. If reflection about X axis, set y1, y2 and y3 as (midy-y1) + midy, (midy-y2) + midy and (midy-y3) + midy respectively.
  5. If reflection about Y axis, set x1, x2 and x3 as (midx-x1) + midx, (midx-x2) + midx and (midx-x3) + midx respectively.
  6. Draw the reflected triangle with endpoints (x1, y1), (x2, y2) and (x3, y3).

Reflection about X axis


Reflection about Y axis


Program Code

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

void main()
{
    int gd = DETECT, gm;
    int x1, y1, x2, y2, x3, y3, x4, y4, xn, yn, choice;
    int tx, ty, sx, sy, theta, axis, maxx, maxy, midx, midy;
    float rad, sh;
    clrscr();
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
    maxx = getmaxx();
    maxy = getmaxy();
    midx = maxx / 2;
    midy = maxy / 2;
    
    do
    {
        printf("1. Translation\n2. Rotation\n3. Scaling\n4. Shearing\n5. Reflection\n6. Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);
        
        switch (choice)
        {
        case 1:
            printf("\nEnter two line endpoints (x1, y1), (x2, y2): ");
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            printf("Enter translation co-ordinates (tx, ty): ");
            scanf("%d %d", &tx, &ty);
            cleardevice();
            line(x1, y1, x2, y2);
            x1 += tx;
            y1 += ty;
            x2 += tx;
            y2 += ty;
            setcolor(GREEN);
            line(x1, y1, x2, y2);
            getch();
            break;
        case 2:
            printf("\nEnter two line endpoints (x1, y1), (x2, y2): ");
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            printf("Enter the angle for rotation: ");
            scanf("%d", &theta);
            cleardevice();
            line(x1, y1, x2, y2);
            rad = (theta * 3.14) / 180;
            xn = x2 * cos(rad) - y2 * sin(rad);
            yn = y2 * cos(rad) + x2 * sin(rad);
            setcolor(GREEN);
            line(x1, y1, xn, yn);
            getch();
            break;
         case 3:
            printf("\nEnter the 1st point for the triangle (x1, y1): ");
            scanf("%d %d", &x1, &y1);
            printf("Enter the 2nd point for the triangle (x2, y2): ");
            scanf("%d %d", &x2, &y2);
            printf("Enter the 3rd point for the triangle (x3, y3): ");
            scanf("%d %d", &x3, &y3);
            printf("Enter the scaling factor (sx, sy): ");
            scanf("%d %d", &sx, &sy);
            cleardevice();
            line(x1, y1, x2, y2);
            line(x2, y2, x3, y3);
            line(x3, y3, x1, y1);
            x1 *= sx;
            y1 *= sy;
            x2 *= sx;
            y2 *= sy;
            x3 *= sx;
            y3 *= sy;
            setcolor(GREEN);
            line(x1, y1, x2, y2);
            line(x2, y2, x3, y3);
            line(x3, y3, x1, y1);
            getch();
            break;
        case 4:
            printf("\nEnter the 1st point for the rectangle (x1, y1): ");
            scanf("%d %d", &x1, &y1);
            printf("Enter the 2nd point for the rectangle (x2, y2): ");
            scanf("%d %d", &x2, &y2);
            printf("Enter the 3rd point for the rectangle (x3, y3): ");
            scanf("%d %d", &x3, &y3);
            printf("Enter the 4th point for the rectangle (x4, y4): ");
            scanf("%d %d", &x4, &y4);
            printf("1. X axis shearing\n2. Y axis shearing\n");
            printf("Enter your choice: ");
            scanf("%d", &axis);
            printf("Enter the value for shearing: ");
            scanf("%f", &sh);
            cleardevice();
            line(midx+x1, midy-y1, midx+x2, midy-y2);
            line(midx+x2, midy-y2, midx+x3, midy-y3);
            line(midx+x3, midy-y3, midx+x4, midy-y4);
            line(midx+x4, midy-y4, midx+x1, midy-y1);
            setcolor(3);
            line(0, midy, maxx, midy);
            line(midx, 0, midx, maxy);
            if (axis == 1) {
            x1 = x1 + (sh * y1);
            x2 = x2 + (sh * y2);
            x3 = x3 + (sh * y3);
            x4 = x4 + (sh * y4);
            }
            else {
            y1 = y1 + (sh * x1);
            y2 = y2 + (sh * x2);
            y3 = y3 + (sh * x3);
            y4 = y4 + (sh * x4);
            }
            setcolor(GREEN);
            line(midx+x1, midy-y1, midx+x2, midy-y2);
            line(midx+x2, midy-y2, midx+x3, midy-y3);
            line(midx+x3, midy-y3, midx+x4, midy-y4);
            line(midx+x4, midy-y4, midx+x1, midy-y1);
            getch();
            break;
        case 5:
            printf("\nEnter the 1st point for the triangle (x1, y1): ");
            scanf("%d %d", &x1, &y1);
            printf("Enter the 2nd point for the triangle (x2, y2): ");
            scanf("%d %d", &x2, &y2);
            printf("Enter the 3rd point for the triangle (x3, y3): ");
            scanf("%d %d", &x3, &y3);
            printf("1. Reflection about X axis\n2. Reflection about Y axis\n");
            printf("Enter your choice: ");
            scanf("%d", &axis);
            cleardevice();
            line(x1, y1, x2, y2);
            line(x2, y2, x3, y3);
            line(x3, y3, x1, y1);
            setcolor(3);
            line(0, midy, maxx, midy);
            line(midx, 0, midx, maxy);
            if (axis == 1) {
            y1 = (midy - y1) + midy;
            y2 = (midy - y2) + midy;
            y3 = (midy - y3) + midy;
            }
            else {
            x1 = (midx - x1) + midx;
            x2 = (midx - x2) + midx;
            x3 = (midx - x3) + midx;
            }
            setcolor(GREEN);
            line(x1, y1, x2, y2);
            line(x2, y2, x3, y3);
            line(x3, y3, x1, y1);
            getch();
            break;
        }
        
        clrscr();
        cleardevice();
        setcolor(WHITE);
    
    } while (choice != 6);
    closegraph();
}

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