TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Matrix Representation of 2D Transformation

Matrix Representation of 2D Transformation with Computer Graphics Tutorial, Line Generation Algorithm, 2D Transformation, 3D Computer Graphics, Types of Curves, Surfaces, Computer Animation, Animation Techniques, Keyframing, Fractals etc.

<< Back to MATRIX

Matrix Representation of 2D Transformation

Matrix Representation of 2D Transformation

Program to implement 2-D Transformations:

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
class trans
{
	float x[20],y[20],xm,ym,ref[2][2],shx,shy;
	int i,j,k,n;
	float sx,sy,tx,ty,ang;
	int gd,gm;
	float xtmp [20],ytmp[20];
	public:
	void takeinput();
	void menu();
	void graphmode();
	void mapgraph();
	void plotint();
	void translate();
	void scale();
	void rotate();
	void reflect();
	void shear();
	void plotfinal();
};
int ch;
void trans::takeinput()
{
	cout<<"ENTER THE NO OF VERTICES\n";
	cin>>n;
	for (i=0;i<n;i++)
	{
		cout<<"ENTER THE "<<i+1<<"COORDINATES \n";
		cin>>x[i]>>y[i];
	}
	clrscr();
}
void trans::menu()
{
	int kk;
	cout<<"\n1:TRANSLATION";
	cout<<"\n2:SCALING";
	cout<<"\n3:ROTATION";
	cout<<"\n4:REFLECTION";
	cout<<"\n5:SHEARING";
	cout<<"\n6:EXIT";
	cin>>ch;
	switch (ch)
	{
		case1:
			cout<<"\n ENTER TX AND TY";
			cin>>tx>>ty;
			break;
		case2:
			cout<<"\n ENTER SX AND SY";
			cin>>sx>>sy;
			break;
		case3:
			cout<<"\n ENTER ANGLE OF ROTATION";
			cin>>ang;
			break;
		case4:
			cout<<"\n REFLECTION MENU";
			cout<<"\n 1:X-PLANE";
			cout<<"\n 2: Y-PLANE";
			cout<<"\n 3: ORIGIN";
			cout<<"\n 4: Y=X PLANE";
			cout<<"\n 5: Y=-X PLANE";
			cout<<"\n ENTER YOUR CHOICE";
			cin>>kk;
			switch (kk)
			{
				case1:
				         ref [0][0] =1;
					ref [0][1]=0;
					ref [1][0]=0;
					ref [1][1]=1;
					break;
				case2:
					ref [0][0]= -1;
					ref [0][1]=0;
					ref [1][0]=0;
					ref [1][1]=1;
					break;
				case3:
					ref [0][0]=-1;
					ref [0][1]=0;
					ref [1][0]=0;
					ref [1][1]=1;
					break;
				case4:
					ref [0][0]=0;
					ref [0][1]=1;
					ref [1][0] =1;
					ref [1][1]=0;
					break;
				case5:
					ref [0][0]=0;
					ref [0][1]=1;
					ref [1][0]=1;
					ref [1][1]=0;
					break;
		case5:
			cout<< "\n SHEARING MENU";
			cout<<"\n 1:X-DIR\n 2: Y-DIR \n 3: X-Y DIR\n ENTER YOUR 				CHOICE";
			cin>>kk;
			switch (kk)
			{
				case1:
					cout<<"\n ENTER SHX";
					cin>> shx;
					ref[0][0] =1;
					ref [0][1]=0;
					ref [1][0]=shx;
					ref [1][1]=1;
					break;
				case2:
					cout<< "\n ENTER SHY";
					cin>>shy;
					ref [0][0]=1;
					ref [0][1]=shy;
					ref [1][0]=0;
					ref [1][1] =1;
					break;
				case3:
					cout<<"\n ENTER SHX";
					cin >> shx;
					cout<<"\n ENTER SHY";
					cin>> shy;
					ref [0][0] =1;
					ref [0][1] =shy;
					ref [1][0] =shx;
					ref [1][1] =1;
					break;
				}
				break;
			}
		}
	void trans::graphmode()
	{
		gd=DETECT;
		initgraph (&gd, &gm, "");
	}
	void trans::mapgraph()
	{
		xm=getmaxx ()/2;
		ym=getmaxy ()/2;
		line (xm,0,xm,2*ym);
		line (0,ym,2 * xm,ym);
	}
	void trans::plotint()
	{
		for(i=0;i<n-1;i++)
		{
			circle (x[i] +xm,-y[i]+ym,2)
			circle x [n-1]+xm,(-y[n-1]+ym),2;
			line (x[i]+xm,(-y[i]+ym),x[i+1]+xm,(-y[i+1]+ym));
		}
		line (x[n-1]+xm,(-y[n-1]+ym,)x[0]+xm,(-y[0]+ym));
	}
	void trans::translate()
	{
		for(i=0;i<n;i++)
		{
			xtmp[i]=x[i]+tx;
			ytmp[i]=y[i]+ty;
		}
	}
	void trans::plotfinal()
	{
		for (i=0;i<n-1;i++)
		{
			circle (xtmp[i]+xm, (-ytmp[i]+ym,2);
			circle (xtmp[n-1]+xm,(-ytmp[n-1]+ym),2);
			line (xtmp[i]+xm,(-ytmp[i]+ym),xtmp[i+1]+xm,(-ytmp[i+1]+ym));
		}
			line (xtmp[n-1]+xm,(-ytmp[n-1]+ym),xtmp[0]+xm,(-ytmp[0]+ym));
	}
	void trans::scale()
	{
		float s [2][2],mxy[7][2],rxy[7][2];
		s [0][0]=sx;
		s [0][1]=0;
		s [1][0]=0;
		s [1][1]=sy;
		tx=-x[0];
		ty=-y[0];
		translate ();
		k=0;
		for(i=0;i<n;i++)
		{
			j=0;
			mxy[i][j]=xtmp[k];
			mxy[i][j+1]=ytmp[k];
			k++;
		}
		for (i=0;i<n;i++)
		{
			for(j=0;j<2;j++)
			{
				rxy[i][j]=0;
				for(k=0;k<2;k++)
				{
					rxy[i][j]+=mxy[i][k]*s[k][j];
				}
			}
		}
				j=0;
				k=0;
		for(i=0;i<n;i++)
		{
			j=0;
			x[k]=rxy[i][j];
			y[k]=rxy[i][j+1];
			k++;
		}
		tx=-tx;
		ty=-ty;
		translate();
	}
	void trans::rotate()
	{
		float r[2][2],mxy[7][2],rxy[7][2],tmp;
		tmp=22/7;
		tmp=(tmp*ang)/180;
		r[0][0]=cos(tmp);
		r[0][1]=sin(tmp);
		r[1][0]=cos(tmp);
		r[1][1]=sy;
		tx=-x[0];
		ty=-y[0];
		translate ();
		k=0;
		for (i=0;i<n;i++)
	      {
		j=0;
		mxy[i][j]=xtmp[k];
		mxy[i][j+1]=ytmp[k];
		k++;
	      }
	for (i=0;i<n;i++)
	{
		for (j=0;j<2;j++)
		{
			rxy[i][j]=0;
			for (k=0;k<2;k++)
			{
				rxy[i][j]+=mxy[i][k]*r[k][j];
			}
		}
	}
		j=0;
		k=0;
	for(i=0;i<n;i++)
	{
		j=0;
		x[k]=rxy[i][j];
		y[k]=rxy[i][j+1];
		k++;
	}
	tx=-tx;
	ty=-ty;
	translate();
}
void trans::reflect()
{
	float mxy[7][2],rxy[7][2],tmp;
	tx=0;
	ty=0;
	translate();
	k=0;
	for(i=0;i<n;i++)
	{
		j=0;
		mxy[i][j]=xtmp[k];
		mxy[i][j+1]=ytmp[k];
		k++;
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<2;j++)
		{
			rxy[i][j]=0;
			for(k=0;k<2;k++)
			{
				rxy[i][j]|+=mxy[i][k]*r[k][j];
			}
		}
	}
		j=0;
		k=0;
	for(i=0;i<n;i++)
	{
		j=0;
		x[k]=rxy[i][j];
		y[k]=rxy[i][j+1];
		k++;
	}
	tx=-tx;
	ty=-ty;
	translate();
}
void trans::shear()
{
	float mxy[7][2],rxy[7][2],tmp;
	tx=0;
	ty=0;
	translate ();
	k=0;
	for(i=0;i<n;i++)
	{
		j=0;
		mxy[i][j]=xtmp[k];
		mxy[i][j+1]=ytmp[k];
		k++;
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<2;j++)
		{
			rxy[i][j]=0;
			for (k=0;k<2;k++)
			{
				rxy[i][j]|+=mxy[i][k]*r[k][j];
			}
		}
	}
		j=0;
		k=0;
	for(i=0;i<n;i++)
	{
		j=0;
		x[k]=rxy[i][j];
		y[k]=rxy[i][j+1];
		k++;
	}
	tx=-tx;
	ty=-ty;
	translate ();
}
void main()
{
	clrscr ();
	trans t1;
	t1.takeinput ();
	t1.menu ();
	t1.graphmode ();
	t1.mapgraph ();
	t1.plotint ();
	switch (ch)
	{
		case1:
			t1.translate ();
			break;
		case2:
			t1.scale ();
			break ();
		case3:
			t1.rotate ();
			break;
		case4:
			t1.reflect ();
			break;
		case5:
			t1.shear ();
			break;
		case6:
			exit ();
		}
		getch ();
		t1.plotfinal ();
		getch ();
		closegraph ();
}

Output:

Translate

1: TRANSLATION
2: SCALING
3: ROTATION
4: REFLECTION
5: SHEARING
6: EXIT
ENTER YOUR CHOICE 4
REFLECTION MENU
1: X-PLANE
2: Y-PLANE
3: ORIGIN
4: Y=X PLANE
5: Y=-X PLANE
ENTER YOUR CHOICE 4
1: TRANSLATION
2: SCALING
3: ROTATION
4: REFLECTION
5: SHEARING
6: EXIT
ENTER YOUR CHOICE 5
SHEARING MENU
1: X-DIR
2: Y-DIR
ENTER YOUR CHOICE 3

ENTER SHX 5
ENTER SHY 5
ENTER THE NO OF VERTICES
5
ENTER THE 1 COORDINATES
10 10
ENTER THE 2 COORDINATES
30 10
ENTER THE 3 COORDINATES
40 20
ENTER THE 4 COORDINATES
35 30
ENTER THE 5 COORDINATES
15 20
1: TRANSLATION
2: SCALING
3: ROTATION
4: REFLECTION
5: SHEARING
6: EXIT
ENTER YOUR CHOICE 1
ENTER TX AND TY 10 10

Matrix Representation of 2D Transformation




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf