본문 바로가기

DirectX( 다이렉트 X)

C++ 커스텀 행렬 (Custom Vector3 For D3D)

전치행렬 (Transpose Matrix)

행렬의 곱 (Multiply Matrix)

/**
	Custom Matrix 0.1v
	2019/05/02 Direct 수업용 Matrix 제작
	xtar.tistory.com
*/

#include<iostream>
using namespace std;

// 행렬별 구조체
struct Matrix3X3
{
	float entry[3][3];
};

struct Matrix4X4
{
	float entry[4][4];
};

struct Matrix1X4
{
	float entry[1][4];
};
struct Matrix1X3
{
	float entry[1][3];
};

//4x4 전치행렬
Matrix4X4 TransposeMatrix4X4(Matrix4X4 _matrix)
{
	Matrix4X4 transposeM;

	//전치행렬 구하기
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{

			transposeM.entry[i][j] = _matrix.entry[j][i];

		}
	}

	return transposeM;
}
//3x3 전치행렬
Matrix3X3 TransposeMatrix3X3(Matrix3X3 _matrix)
{
	Matrix3X3 transposeM;

	//전치행렬 구하기
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			transposeM.entry[i][j] = _matrix.entry[j][i];
		}
	}

	return transposeM;
}
Matrix1X4 Multiply(Matrix1X4 _matrixF, Matrix4X4 _matrixS)
{
	Matrix1X4 mulMatrix;

	//초기화 부분
	for (int i = 0; i < 4; i++)
	{
		mulMatrix.entry[0][i] = 0;
	}

	// mul행렬 +=두 행렬의 곱
	for (int j = 0; j < 4; j++)
	{
		for (int k = 0; k < 4; k++)
		{
			mulMatrix.entry[0][j] += (_matrixF.entry[0][k] * _matrixS.entry[k][j]);
		}
	}

	return mulMatrix;
}
Matrix1X3 Multiply(Matrix1X3 _matrixF, Matrix3X3 _matrixS)
{
	Matrix1X3 mulMatrix;

	//초기화 부분
	for (int i = 0; i < 3; i++)
	{
		mulMatrix.entry[0][i] = 0;
	}
	// mul행렬 +=두 행렬의 곱
	for (int j = 0; j < 3; j++)
	{
		for (int k = 0; k < 3; k++)
		{
			mulMatrix.entry[0][j] += (_matrixF.entry[0][k] * _matrixS.entry[k][j]);
		}
	}


	return mulMatrix;
}

#pragma region Show Function
// 출력 함수
void show(Matrix3X3 _matrix)
{
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << _matrix.entry[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}
void show(Matrix4X4 _matrix)
{
	for (auto &i : _matrix.entry)
	{
		for (auto &j : i)
		{
			cout << j << " ";
		}
		cout << endl;
	}
	cout << endl;
}
void show(Matrix1X3 _matrix)
{
	for (auto &i : _matrix.entry)
	{
		for (auto j : i)
		{
			cout << j << " ";
		}
		cout << endl;
	}
	cout << endl;
}
void show(Matrix1X4 _matrix)
{
	for (auto &i : _matrix.entry)
	{
		for (auto j : i)
		{
			cout << j << " ";
		}
		cout << endl;
	}
	cout << endl;
}

#pragma endregion

int main()
{
	Matrix3X3 matrixFirst = { 1,2,3,4,5,6,7,8,9 };
	Matrix3X3 trasnsposeMatrix;

	Matrix4X4 matrixSecond = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
	Matrix4X4 trasnsposeMatrix4;

	Matrix1X4 matrixThird = { 1,2,3,4 };
	Matrix1X4 mul1X4;

	Matrix1X3 matrixFourth = { 1,2,3 };
	Matrix1X3 mul1X3;

	cout << "--전치 행렬로 변환 과정--" << endl << endl;
	trasnsposeMatrix = TransposeMatrix3X3(matrixFirst);
	show(matrixFirst);
	show(trasnsposeMatrix);

	trasnsposeMatrix4 = TransposeMatrix4X4(matrixSecond);
	show(matrixSecond);
	show(trasnsposeMatrix4);

	cout << "----------" << endl;
	cout << "1x4 행렬 * 4x4 행렬" << endl << endl;
	show(matrixThird);
	show(matrixSecond);
	mul1X4 = Multiply(matrixThird, matrixSecond);
	cout << "결과: ";
	show(mul1X4);

	cout << "----------" << endl;
	cout << "1x3 행렬 * 3x3 행렬" << endl << endl;
	show(matrixFourth);
	show(matrixFirst);

	mul1X3 = Multiply(matrixFourth, matrixFirst);
	cout << "결과 : ";
	show(mul1X3);

	return 0;
}

/* 4*4

1	2	3	4
5	6	7	8
9	10	11	12
13	14	15	16

*/

※수업용