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
- Input the co-ordinates of the object.
- Draw the object on the screen.
- Input the 1st and 2nd translation co-ordinates.
- For each translation, add the translation co-ordinates to the object co-ordinates.
- Draw the translated object on the screen.
Consecutive Rotation
- Input the co-ordinates of the object.
- Draw the object on the screen.
- Input the 1st and 2nd rotation angles.
- For each rotation, calculate r = (Ɵ * 3.14) / 180.
- Calculate xn = xi * cos(r) - yi * sin(r).
- Calculate yn = yi * cos(r) + xi * sin(r).
- Draw the rotated object on the screen.
Pivot Point Rotation
- Input the co-ordinates of the object.
- Draw the object on the screen.
- Input the pivot point co-ordinates.
- Input the rotation angle.
- Translate the object to the pivot point co-ordinates.
- Rotate the object with the given angle.
- Translate the rotated object back to the original co-ordinates.
#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