返回

02.谈谈如何封装向量类和矩阵类,轻松驾驭3D数学

前端

导语:向量类和矩阵类的重要性

在三维数学中,向量和矩阵是不可或缺的基础概念。向量可以用来表示空间中的点、线段和方向,而矩阵则可以用来表示空间中的变换、旋转和缩放。为了方便进行几何计算和线性代数运算,我们可以封装向量类和矩阵类,以简化代码编写并提高运算效率。

向量类的封装

向量类可以用来表示空间中的点、线段和方向。它的基本组成元素是分量,分量可以是实数或复数。向量的长度是其分量平方和的开方。向量之间可以进行加减、数乘、点乘和叉乘等运算。

封装向量类的步骤如下:

  1. 定义向量类的头文件和源文件。
  2. 在头文件中声明向量类的成员变量和成员函数。
  3. 在源文件中实现向量类的成员函数。

下面是一个简单的向量类示例:

#include <iostream>
#include <cmath>

class Vector3
{
public:
    Vector3() {}
    Vector3(float x, float y, float z) : x(x), y(y), z(z) {}

    float x, y, z;

    float length()
    {
        return sqrt(x * x + y * y + z * z);
    }

    void normalize()
    {
        float len = length();
        x /= len;
        y /= len;
        z /= len;
    }

    Vector3 operator+(const Vector3& other)
    {
        return Vector3(x + other.x, y + other.y, z + other.z);
    }

    Vector3 operator-(const Vector3& other)
    {
        return Vector3(x - other.x, y - other.y, z - other.z);
    }

    Vector3 operator*(float scalar)
    {
        return Vector3(x * scalar, y * scalar, z * scalar);
    }

    float dot(const Vector3& other)
    {
        return x * other.x + y * other.y + z * other.z;
    }

    Vector3 cross(const Vector3& other)
    {
        return Vector3(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x);
    }

    void print()
    {
        std::cout << "(" << x << ", " << y << ", " << z << ")\n";
    }
};

矩阵类的封装

矩阵类可以用来表示空间中的变换、旋转和缩放。它的基本组成元素是元素,元素可以是实数或复数。矩阵的行列数可以是任意正整数。矩阵之间可以进行加减、数乘、矩阵乘法和转置等运算。

封装矩阵类的步骤如下:

  1. 定义矩阵类的头文件和源文件。
  2. 在头文件中声明矩阵类的成员变量和成员函数。
  3. 在源文件中实现矩阵类的成员函数。

下面是一个简单的矩阵类示例:

#include <iostream>

class Matrix4x4
{
public:
    Matrix4x4() {}
    Matrix4x4(float m00, float m01, float m02, float m03,
               float m10, float m11, float m12, float m13,
               float m20, float m21, float m22, float m23,
               float m30, float m31, float m32, float m33)
    {
        m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
        m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
        m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
        m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
    }

    float m[4][4];

    Matrix4x4 operator+(const Matrix4x4& other)
    {
        Matrix4x4 result;
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                result.m[i][j] = m[i][j] + other.m[i][j];
            }
        }
        return result;
    }

    Matrix4x4 operator-(const Matrix4x4& other)
    {
        Matrix4x4 result;
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                result.m[i][j] = m[i][j] - other.m[i][j];
            }
        }
        return result;
    }

    Matrix4x4 operator*(float scalar)
    {
        Matrix4x4 result;
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                result.m[i][j] = m[i][j] * scalar;
            }
        }
        return result;
    }

    Matrix4x4 operator*(const Matrix4x4& other)
    {
        Matrix4x4 result;
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                result.m[i][j] = 0;
                for (int k = 0; k < 4; k++)
                {
                    result.m[i][j] += m[i][k] * other.m[k][j];
                }
            }
        }
        return result;
    }

    Matrix4x4 transpose()
    {
        Matrix4x4 result;
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                result.m[i][j] = m[j][i];
            }
        }
        return result;
    }

    void print()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                std::cout << m[i][j] << " ";
            }
            std::cout << "\n";
        }
    }
};

使用示例

下面是一个使用向量类和矩阵类进行几何计算的示例:

#include "Vector3.h"
#include "Matrix4x4.h"

int main()
{
    // 创建向量
    Vector3 v1(1, 2, 3);
    Vector3 v2(4, 5, 6);

    // 向量加法
    Vector3 v3 = v1 + v2;

    // 向量减法
    Vector3 v4 = v1 - v2;

    // 向量数乘
    Vector3 v5 = v1 * 2;

    // 向量点乘
    float dot = v1.dot(v2);

    // 向量叉乘
    Vector3 cross = v1.cross(v2);

    // 创建矩阵
    Matrix4x4 m1(
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,