1. 程式人生 > >3D數學 矩陣乘法程式設計

3D數學 矩陣乘法程式設計

矩陣乘法程式設計

設計一個3x3矩陣類,實現如下功能:

  • 3x3矩陣
  • 矩陣與矩陣的乘法
  • 向量與矩陣的乘法

具體的數學知識就不詳述了,直接貼程式碼。這裡利用到上一篇3D數學向量運算的程式碼。

//Matrix3X3.h
#pragma once

#include "Vector3.h"

class Matrix3X3
{
public:
    //矩陣相乘
    Matrix3X3 operator*(Matrix3X3& rhs);
    //矩陣乘等矩陣
    Matrix3X3& operator*=(Matrix3X3& rhs);
public:
    float
m11,m12,m13; float m21,m22,m23; float m31,m32,m33; }; //向量乘以矩陣 Vector3 operator*(Vector3& vec,Matrix3X3& mat); //向量乘等矩陣 Vector3& operator*=(Vector3& vec,Matrix3X3& mat);
//Matrix3X3.cpp
#include "Matrix3X3.h"

Matrix3X3 Matrix3X3::operator*(Matrix3X3& rhs)
{
    Matrix3X3 tempMat;
tempMat.m11 = this->m11 * rhs.m11 + this->m12 * rhs.m21 + this->m13 * rhs.m31; tempMat.m12 = this->m11 * rhs.m12 + this->m12 * rhs.m22 + this->m13 * rhs.m32; tempMat.m13 = this->m11 * rhs.m13 + this->m12 * rhs.m23 + this->m13 * rhs.m33; tempMat.m21 = this->m21 * rhs.m
11 + this->m22 * rhs.m21 + this->m23 * rhs.m31; tempMat.m22 = this->m21 * rhs.m12 + this->m22 * rhs.m22 + this->m23 * rhs.m32; tempMat.m23 = this->m21 * rhs.m13 + this->m22 * rhs.m23 + this->m23 * rhs.m33; tempMat.m31 = this->m31 * rhs.m11 + this->m32 * rhs.m21 + this->m33 * rhs.m31; tempMat.m32 = this->m31 * rhs.m12 + this->m32 * rhs.m22 + this->m33 * rhs.m32; tempMat.m33 = this->m31 * rhs.m13 + this->m32 * rhs.m23 + this->m33 * rhs.m33; return tempMat; } Matrix3X3& Matrix3X3::operator*=(Matrix3X3& rhs) { *this = *this * rhs; return *this; } Vector3 operator*(Vector3& vec,Matrix3X3& mat) { Vector3 tempVec; tempVec.x = vec.x * mat.m11 + vec.y * mat.m21 + vec.z * mat.m31; tempVec.y = vec.x * mat.m12 + vec.y * mat.m22 + vec.z * mat.m32; tempVec.z = vec.x * mat.m13 + vec.y * mat.m23 + vec.z * mat.m33; return tempVec; } Vector3& operator*=(Vector3& vec,Matrix3X3& mat) { vec = vec * mat; return vec; }
//main.cpp
#include <iostream>
#include "Vector3.h"
#include "Matrix3X3.h"

using namespace std;

void print_v(Vector3 v)
{
    cout << "[ " << v.x << ", " << v.y << ", " << v.z << " ]" << endl;
    cout << endl;
}

void print_m(Matrix3X3 m)
{
    cout << m.m11 << "\t" << m.m12 << "\t" << m.m13 << endl;
    cout << m.m21 << "\t" << m.m22 << "\t" << m.m23 << endl;
    cout << m.m31 << "\t" << m.m32 << "\t" << m.m33 << endl;
    cout << endl;
}

int main()
{
    cout << "hello 矩陣" << endl;

    Matrix3X3 a, b, c;

    a.m11 = 1;  a.m12 = -5; a.m13 = 3;
    a.m21 = 0;  a.m22 = -2; a.m23 = 6;
    a.m31 = 7;  a.m32 = 2;  a.m33 = -4;

    b.m11 = -8; b.m12 = 6;  b.m13 = 1;
    b.m21 = 7;  b.m22 = 0;  b.m23 = -3;
    b.m31 = 2;  b.m32 = 4;  b.m33 = 5;

    //矩陣相乘
    c = a * b;
    print_m(c);

    //矩陣乘等於矩陣
    a *= b;
    print_m(a);

    Vector3 v(3,-1,4);

    Matrix3X3 m;
    m.m11 = -2; m.m12 = 0;  m.m13 = 3;
    m.m21 = 5;  m.m22 = 7;  m.m23 = -6;
    m.m31 = 1;  m.m32 = -4; m.m33 = 2;

    //向量乘以矩陣
    Vector3 r = v * m;
    print_v(r);

    //向量乘等於矩陣
    v *= m;
    print_v(v);

    system("pause");
    return 0;
}

程式執行結果為:

hello 矩陣
-37 18 31
-2 24 36
-50 26 -19

-37 18 31
-2 24 36
-50 26 -19

[ -7, -23, 23 ]

[ -7, -23, 23 ]