GameServer
Vector3D.h
[詳解]
1 #ifndef __VECTOR2D_H__
2 #define __VECTOR2D_H__
3 
4 #include <math.h>
5 #include <float.h>
6 
7 // ベクトルクラス
8 class Vector3D
9 {
10 
11 public:
12 
13  // コンストラクタ
15  : X(0.0f)
16  , Y(0.0f)
17  , Z(0.0f) {}
18 
19  Vector3D(float InX, float InY, float InZ)
20  : X(InX)
21  , Y(InY)
22  , Z(InZ) {}
23 
24  Vector3D(const Vector3D &Arg)
25  : X(Arg.X)
26  , Y(Arg.Y)
27  , Z(Arg.Z) {}
28 
29  // 座標.
30  float X;
31  float Y;
32  float Z;
33 
34  // サイズを取得.
35  float GetSize() const
36  {
37  return sqrt(GetSizeSq());
38  }
39 
40  // サイズの二乗を取得.
41  float GetSizeSq() const
42  {
43  return ((X * X) + (Y * Y) + (Z * Z));
44  }
45 
46  // 正規化したものを取得.
48  {
49  float Size = GetSize();
50  Vector3D Vec;
51  Vec.X = X / Size;
52  Vec.Y = Y / Size;
53  Vec.Z = Z / Size;
54  return Vec;
55  }
56 
57  // 正規化.
58  void Normalize()
59  {
60  Vector3D Vec = GetNormalized();
61  *this = Vec;
62  }
63 
64  // オペレータオーバーロード
65  Vector3D operator +(const Vector3D &Arg) const
66  {
67  Vector3D Vec;
68  Vec.X = X + Arg.X;
69  Vec.Y = Y + Arg.Y;
70  Vec.Z = Z + Arg.Z;
71  return Vec;
72  }
73 
74  Vector3D operator -(const Vector3D &Arg) const
75  {
76  Vector3D Vec;
77  Vec.X = X - Arg.X;
78  Vec.Y = Y - Arg.Y;
79  Vec.Z = Z - Arg.Z;
80  return Vec;
81  }
82 
83  void operator +=(const Vector3D &Arg)
84  {
85  X += Arg.X;
86  Y += Arg.Y;
87  Z += Arg.Z;
88  }
89 
90  void operator -=(const Vector3D &Arg)
91  {
92  X -= Arg.X;
93  Y -= Arg.Y;
94  Z -= Arg.Z;
95  }
96 
97  Vector3D operator *(float Arg) const
98  {
99  Vector3D Vec;
100  Vec.X = X * Arg;
101  Vec.Y = Y * Arg;
102  Vec.Z = Z * Arg;
103  return Vec;
104  }
105 
106  Vector3D operator /(float Arg) const
107  {
108  Vector3D Vec;
109  Vec.X = X / Arg;
110  Vec.Y = Y / Arg;
111  Vec.Z = Z / Arg;
112  return Vec;
113  }
114 
115  void operator *=(float Arg)
116  {
117  X *= Arg;
118  Y *= Arg;
119  Z *= Arg;
120  }
121 
122  void operator /=(float Arg)
123  {
124  X /= Arg;
125  Y /= Arg;
126  Z /= Arg;
127  }
128 
129  bool operator ==(const Vector3D &Arg) const
130  {
131  return (fabsf(X - Arg.X) < FLT_EPSILON && fabsf(Y - Arg.Y) < FLT_EPSILON && fabsf(Z - Arg.Z) < FLT_EPSILON);
132  }
133 
134  bool operator !=(const Vector3D &Arg) const
135  {
136  return (fabsf(X - Arg.X) >= FLT_EPSILON || fabsf(Y - Arg.Y) >= FLT_EPSILON || fabsf(Z - Arg.Z) >= FLT_EPSILON);
137  }
138 
139 
140  // ゼロベクトル
141  static const Vector3D Zero;
142 
143 };
144 
145 #endif // #ifndef __VECTOR2D_H__
void operator*=(float Arg)
Definition: Vector3D.h:115
Vector3D(const Vector3D &Arg)
Definition: Vector3D.h:24
float Z
Definition: Vector3D.h:32
Vector3D GetNormalized() const
Definition: Vector3D.h:47
float GetSizeSq() const
Definition: Vector3D.h:41
bool operator==(const Vector3D &Arg) const
Definition: Vector3D.h:129
void operator-=(const Vector3D &Arg)
Definition: Vector3D.h:90
Vector3D operator/(float Arg) const
Definition: Vector3D.h:106
Definition: Vector3D.h:8
float X
Definition: Vector3D.h:30
void operator+=(const Vector3D &Arg)
Definition: Vector3D.h:83
float Y
Definition: Vector3D.h:31
Vector3D(float InX, float InY, float InZ)
Definition: Vector3D.h:19
Vector3D operator+(const Vector3D &Arg) const
Definition: Vector3D.h:65
float GetSize() const
Definition: Vector3D.h:35
static const Vector3D Zero
Definition: Vector3D.h:141
Vector3D()
Definition: Vector3D.h:14
Vector3D operator-(const Vector3D &Arg) const
Definition: Vector3D.h:74
bool operator!=(const Vector3D &Arg) const
Definition: Vector3D.h:134
void Normalize()
Definition: Vector3D.h:58
Vector3D operator*(float Arg) const
Definition: Vector3D.h:97
void operator/=(float Arg)
Definition: Vector3D.h:122