-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvec3d.h
85 lines (70 loc) · 1.48 KB
/
vec3d.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef VEC3D_H
#define VEC3D_H
#include <cmath>
//--三次元ベクトル--
struct vec3d
{
double x, y, z;
vec3d() : x(0.0), y(0.0), z(0.0) {}
vec3d(double x_, double y_, double z_) : x(x_), y(y_), z(z_) {}
//長さを求める
double length(){ return sqrt(x*x + y*y + z*z); }
//二乗長さを求める
double lengthsq(){ return x*x + y*y + z*z; }
//正規化ベクトルを求める
vec3d norm(){ double L = length(); return vec3d(x / L, y / L, z / L); }
//正規化
void normalize(){ *this /= this->length(); }
//演算子のオーバーロード(四則演算を可能にする)
vec3d operator+(const vec3d& v)
{
return vec3d(x + v.x, y + v.y, z + v.z);
}
vec3d operator-(const vec3d& v)
{
return vec3d(x - v.x, y - v.y, z - v.z);
}
vec3d operator*(const double& a)
{
return vec3d(a*x, a*y, a*z);
}
vec3d operator/(const double& a)
{
return vec3d(x / a, y / a, z / a);
}
vec3d& operator+=(const vec3d& v)
{
*this = *this + v;
return *this;
}
vec3d& operator-=(const vec3d& v)
{
*this = *this - v;
return *this;
}
vec3d& operator*=(const double& a)
{
*this = *this * a;
return *this;
}
vec3d& operator/=(const double& a)
{
*this = *this / a;
return *this;
}
//vec3d同士の*は内積
double operator*(const vec3d& v)
{
return x * v.x + y * v.y, z * v.z;
}
};
inline vec3d cross_vec3d(const vec3d& v1, const vec3d& v2)
{
return vec3d(
-v1.y*v2.z + v1.z*v1.y,
-v1.z*v2.x + v1.x*v1.z,
-v1.x*v2.y + v1.y*v1.x
);
}
#endif