2-D Transformations

Write a program to implement the following 2-D Transformations on a triangle:
• Translation
• Rotation
• Scaling



#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
int points[8],pointsCopy[8];
void triangle();
void reinit();
void main()
{
	int gd=DETECT,gm,choice=0,tx,ty,i,x,y;
	float sx,sy,theta;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter the coordinates of the triangle: ");
scanf("%d %d %d %d %d %d",&points[0],&points[1],&points[2],  
 				&points[3],&points[4],&points[5]);
	points[6]=points[0];
	points[7]=points[1];


	for(i=0;i<8;i++)
	{
		pointsCopy[i]=points[i];
	}
	triangle();

	do
	{
		printf("\n________MENU________");
		printf("\n1.Translate");
		printf("\n2.Rotate");
		printf("\n3.Scale");
		printf("\n4.Redraw the triangle");
		printf("\n5.Exit");
		printf("\nEnter your choice: ");
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:{
					printf("\nEnter the tx and ty: ");
					scanf("%d %d",&tx,&ty);
					for(i=0;i<6;i+=2)
					{
						points[i]+=tx;
						points[i+1]+=ty;
					}
					points[6]=points[0];
					points[7]=points[1];
					triangle();
					break;
				   }
			case 2:{
					printf("\nEnter the angle to be 
rotated: ");
					scanf("%f",&theta);
					for(i=0;i<6;i+=2)
					{   x=points[i];
						y=points[i+1];
						points[i]=x*cos(theta*0.01744)-
							y*sin(theta*0.01744);
						points[i+1]=x*sin(theta*0.01744)+
y*cos(theta*0.01744);
					}
					points[6]=points[0];
					points[7]=points[1];
					triangle();
					break;
				   }
			case 3:{
					printf("\nEnter the scaling factors-
 sx and sy: ");
					scanf("%f %f",&sx,&sy);
					for(i=0;i<6;i+=2)
					{
						points[i]*=sx;
						points[i+1]*=sy;
					}
					points[6]=points[0];
					points[7]=points[1];
					triangle();
					break;
				   }
			case 4:{
					reinit();
					cleardevice();
					clrscr();
					triangle();
					break;
				   }
			default: break;
		}
	}while(choice!=5);
	getch();
}
void triangle()
{
	setviewport(0,0,640,480,0);
	drawpoly(4,points);
}
void reinit()
{
	int i;
	for(i=0;i<8;i++)
	{
		points[i]=pointsCopy[i];
	}
}


Output
1
1
1

Leave a Reply

Your email address will not be published. Required fields are marked *