Home
向量类
OpenGL
向量类
姜睿
姜睿
May 22, 2023
1 min

Table Of Contents

01
加法 (Plus)
02
减法 & 负号 (Minus & Negative)
03
乘法 (Mutiply)
04
除法 (Divide)
05
点乘 (Dot Product)
06
叉乘 (Cross Product)
07
长度 (Length)
08
归一化 (Normalize)
09
距离 (Distance)
10
总体代码

加法 (Plus)

1
2Vector3 operator+(const Vector3 &vec)
3{
4return Vector3(x + vec.x, y + vec.y, z + vec.z);
5}
6Vector3 operator+=(const Vector3 &vec)
7{
8return *this = *this + vec;
9}
10

减法 & 负号 (Minus & Negative)

1
2Vector3 operator-(const Vector3 &vec)
3{
4return Vector3(x - vec.x, y - vec.y, z - vec.z);
5}
6Vector3 operator-=(const Vector3 &vec)
7{
8return *this = *this - vec;
9}
10Vector3 operator-(void)
11{
12return Vector3(-x, -y, -z);
13}
14

乘法 (Mutiply)

1
2Vector3 operator*(float num)
3{
4return Vector3(x * num, y * num, z * num);
5}
6Vector3 operator*=(float num)
7{
8return *this = *this * num;
9}
10

除法 (Divide)

1
2Vector3 operator/(float num)
3{
4return Vector3(x / num, y / num, z / num);
5}
6Vector3 operator/=(float num)
7{
8return *this = *this / num;
9}
10

点乘 (Dot Product)

1
2float Dot(const Vector3 &vec)
3{
4return x * vec.x + y * vec.y + z * vec.z;
5}
6

叉乘 (Cross Product)

1
2Vector3 operator*(const Vector3 &vec)
3{
4return Vector3(y * vec.z - z * vec.y,
5   -(x * vec.z - z * vec.x),
6   x * vec.y - y * vec.x);
7}
8

长度 (Length)

1
2float Length(void)
3{
4return sqrt(x * x + y * y + z * z);
5}
6

归一化 (Normalize)

1
2Vector3 Normalize(void)
3{
4float length = Length();
5
6x /= length;
7y /= length;
8z /= length;
9
10return *this;
11}
12

距离 (Distance)

1float Distance(const Vector3 &vec)
2{
3float disX = vec.x - x;
4float disY = vec.y - y;
5float disZ = vec.z - z;
6
7return sqrt(disX * disX + disY * disY + disZ * disZ);
8}
9

总体代码

  • 那么我们现在就可以写代码啦!
1
2#ifndef VECTOR3_H
3
4#define VECTOR3_H
5
6#include <math.h>
7
8class Vector3
9{
10// fields
11public:
12float x, y, z;
13
14// methods
15public:
16Vector3(float X = 0.0f, float Y = 0.0f, float Z = 0.0f)
17{
18x = X;
19y = Y;
20z = Z;
21}
22
23Vector3 operator+(const Vector3 &vec)
24{
25return Vector3(x + vec.x, y + vec.y, z + vec.z);
26}
27Vector3 operator+=(const Vector3 &vec)
28{
29return *this = *this + vec;
30}
31
32Vector3 operator-(const Vector3 &vec)
33{
34return Vector3(x - vec.x, y - vec.y, z - vec.z);
35}
36Vector3 operator-=(const Vector3 &vec)
37{
38return *this = *this - vec;
39}
40Vector3 operator-(void)
41{
42return Vector3(-x, -y, -z);
43}
44
45Vector3 operator*(float num)
46{
47return Vector3(x * num, y * num, z * num);
48}
49Vector3 operator*=(float num)
50{
51return *this = *this * num;
52}
53
54Vector3 operator/(float num)
55{
56return Vector3(x / num, y / num, z / num);
57}
58Vector3 operator/=(float num)
59{
60return *this = *this / num;
61}
62
63// dot product
64float Dot(const Vector3 &vec)
65{
66return x * vec.x + y * vec.y + z * vec.z;
67}
68// cross product
69Vector3 operator*(const Vector3 &vec)
70{
71return Vector3(y * vec.z - z * vec.y,
72   -(x * vec.z - z * vec.x),
73   x * vec.y - y * vec.x);
74}
75
76float Length(void)
77{
78return sqrt(x * x + y * y + z * z);
79}
80
81Vector3 Normalize(void)
82{
83float length = Length();
84
85x /= length;
86y /= length;
87z /= length;
88
89return *this;
90}
91
92float Distance(const Vector3 &vec)
93{
94float disX = vec.x - x;
95float disY = vec.y - y;
96float disZ = vec.z - z;
97
98return sqrt(disX * disX + disY * disY + disZ * disZ);
99}
100
101bool operator==(const Vector3 &vec)
102{
103return x == vec.x && y == vec.y && z == vec.z;
104}
105bool operator!=(const Vector3 &vec)
106{
107return !(*this == vec);
108}
109};
110
111#endif
112
1
2#include <iostream>
3#include "Vector3.h"
4using namespace std;
5
6void Print(const Vector3 &vec)
7{
8cout << "(" << vec.x << ", " << vec.y << ", " << vec.z << ")" << endl
9 << endl;
10}
11
12void Print(float num)
13{
14cout << num << endl
15 << endl;
16}
17
18void Print(bool boolean)
19{
20cout << boolalpha << boolean << endl
21 << endl;
22}
23
24int main()
25{
26cout << "Plus" << endl;
27Print(Vector3(1, 2, 3) + (Vector3(1, 2, 3)));
28cout << "Minus" << endl;
29Print(Vector3(1, 2, 3) - (Vector3(1, 2, 3)));
30cout << "Times" << endl;
31Print(Vector3(1, 2, 3) * 2);
32cout << "Divide" << endl;
33Print(Vector3(2, 4, 6) / 2);
34
35cout << "Dot Product" << endl;
36Print(Vector3(1, 2, 3).Dot(Vector3(1, 2, 3)));
37cout << "Cross Product" << endl;
38Print(Vector3(1, 2, 3) * (Vector3(4, 5, 6)));
39
40cout << "Negative" << endl;
41Print(-Vector3(1, 2, 3));
42
43cout << "Length" << endl;
44Print(Vector3(3, 4, 5).Length());
45cout << "Normalize" << endl;
46Print(Vector3(3, 4, 5).Normalize());
47cout << "Distance" << endl;
48Print(Vector3(1, 2, 3).Distance(Vector3(4, 5, 6)));
49
50cout << "Equal" << endl;
51Print(Vector3(1, 2, 3) == Vector3(1, 2, 3));
52
53cout << "Not Equal" << endl;
54Print(Vector3(1, 2, 3) != Vector3(1, 2, 3));
55
56return 0;
57}
58

姜睿

姜睿

学生

游戏设计学生

Expertise

游戏开发
平面设计

Related Posts

简单三角形绘制
简单三角形绘制
April 29, 2023
1 min

Legal Stuff

Privacy NoticeCookie PolicyTerms Of Use