个性化阅读
专注于IT技术分析

二维变换的矩阵表示

二维变换的矩阵表示

实施二维转换的程序:

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

输出:

翻译

1:翻译2:缩放3:旋转4:反射5:剪切6:退出输入你的选择4反射菜单1:X平面2:Y平面3:原始4:Y = X平面5:Y = -X平面输入你的选择4 1:翻译2:缩放3:旋转4:反射5:剪切6:退出输入你的选择5剪切菜单1:X-DIR 2:Y-DIR输入你的选择3输入SHX 5输入SHY 5输入5记录数5输入1坐标10 10输入2坐标30 10输入3坐标40 20输入4坐标35 30输入5坐标15 20 1:翻译2:缩放3:旋转4:反射5:剪切6 :退出输入你的选择1输入TX和TY 10 10

二维变换的矩阵表示

赞(0)
未经允许不得转载:srcmini » 二维变换的矩阵表示

评论 抢沙发

评论前必须登录!