matrix.cpp

来自「蒙特卡罗方法可以有效地解决复杂的工程问题」· C++ 代码 · 共 120 行

CPP
120
字号
// Matrix.cpp: implementation of the CMatrix class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RayTrace.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

double Radian(double angle)
{
	return (angle*3.1415926/180.);
}

CMatrix::CMatrix()
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			this->M[i][j]=0.0;
}

CMatrix::CMatrix(CMatrix &mat)
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			this->M[i][j]=mat.M[i][j];

}
CMatrix::~CMatrix()
{}

void CMatrix::Empty() //置零
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			this->M[i][j]=0.0;
}
double CMatrix::GetOffset(int i,int j) const
{
	return M[i][j];
}

void   CMatrix::SetOffset(int i,int j,double x)
{
	M[i][j]=x;
}

CMatrix CMatrix::operator + (const CMatrix &mat) const
{
	CMatrix temp;
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			temp.M[i][j]=this->M[i][j]+mat.M[i][j];
	return temp;
}

CMatrix CMatrix::operator - (const CMatrix &mat) const
{
	CMatrix temp;
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			temp.M[i][j]=this->M[i][j]-mat.M[i][j];
	return temp;

}

CMatrix CMatrix::operator * (const CMatrix &mat) const
{
	CMatrix temp;
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
		{
			double w=0;
			for(int k=0;k<4;k++)
				w+=this->M[i][k]*mat.M[k][j];
			temp.M[i][j]=w;
		}
	return temp;
}

void CMatrix::operator += (const CMatrix &mat)
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			this->M[i][j]+=mat.M[i][j];
}

void CMatrix::operator -= (const CMatrix &mat)
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			this->M[i][j]-=mat.M[i][j];
}

void CMatrix::operator *= (const CMatrix &mat)
{
	double temp[4][4];
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
			temp[i][j]=this->M[i][j];

	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
		{
			double w=0;
			for(int k=0;k<4;k++)
				w+=temp[i][k]*mat.M[k][j];
			this->M[i][j]=w;
		}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?