Aim
To write a C program to implement two dimensional
transformations such as translation, rotation, scaling, shearing and reflection
on objects.
Algorithm
Translation
- Input the line endpoints (x1, y1) and (x2, y2).
- Input the translation co-ordinates (tx, ty).
- Draw the line with endpoints (x1, y1) and (x2, y2).
- Add tx to the co-ordinates x1 and x2.
- Add ty to the co-ordinates y1 and y2.
- Draw the translated line with endpoints (x1, y1) and (x2, y2).
Rotation
- Input the line endpoints (x1, y1) and (x2, y2).
- Input the rotation angle Ɵ.
- Draw the line with endpoints (x1, y1) and (x2, y2).
- Calculate r = (Ɵ * 3.14) / 180.
- Calculate xn = x2 * cos(r) - y2 * sin(r).
- Calculate yn = y2 * cos(r) + x2 * sin(r).
- Draw the rotated line with endpoints (x1, y1) and (xn, yn).
Scaling
- Input the 3 endpoints of the triangle (x1, y1), (x2, y2) and (x3, y3).
- Input the scaling factors sx and sy.
- Draw the triangle with endpoints (x1, y1), (x2, y2) and (x3, y3).
- Update x1, x2 and x3 by multiplying sx to them.
- Update y1, y2 and y3 by multiplying sy to them.
- Draw the scaled triangle with points (x1, y1), (x2, y2) and (x3, y3).
Shearing
- Input the 4 endpoints of the rectangle (x1, y1), (x2, y2), (x3, y3) and (x4, y4).
- Input the value for shearing ‘sh’.
- Draw the rectangle with endpoints (x1, y1), (x2, y2), (x3, y3) and (x4, y4).
- 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.
- 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.
- Draw the sheared rectangle with endpoints (x1,y1), (x2, y2), (x3, y3), (x4, y4).
X axis shear
Y axis Shear
Reflection
- Input the 3 endpoints of the triangle (x1, y1), (x2, y2) and (x3, y3).
- Calculate middle value of X axis as ‘midx’ and Y axis as ‘midy’.
- Draw the triangle with endpoints (x1, y1), (x2, y2) and (x3, y3).
- If reflection about X axis, set y1, y2 and y3 as (midy-y1) + midy, (midy-y2) + midy and (midy-y3) + midy respectively.
- If reflection about Y axis, set x1, x2 and x3 as (midx-x1) + midx, (midx-x2) + midx and (midx-x3) + midx respectively.
- 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();
}