-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3d.cpp
More file actions
145 lines (118 loc) · 2.75 KB
/
vector3d.cpp
File metadata and controls
145 lines (118 loc) · 2.75 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "vector3d.h"
#include <math.h>
Vector3d::Vector3d()
{
_x = 0;
_y = 0;
_z = 0;
_dx = 0;
_dy = 0;
_dz = 0;
_ox = 0;
_oy = 0;
_oz = 0;
}
Vector3d::~Vector3d()
{
//nothing
}
Vector3d::Vector3d(const float ox, const float oy, const float oz,
const float dx, const float dy, const float dz)
{
set_vector(ox, oy, oz, dx, dy, dz);
}
Vector3d::Vector3d(const float x, const float y, const float z)
{
set_vector(0.f, 0.f, 0.f, x, y, z);
}
Vector3d& Vector3d::operator= (const Vector3d& rhs)
{
if(&rhs != this)
{
_x = rhs._x;
_y = rhs._y;
_z = rhs._z;
_ox = rhs._ox;
_oy = rhs._oy;
_oz = rhs._oz;
_dx = rhs._dx;
_dy = rhs._dy;
_dz = rhs._dz;
}
}
Vector3d Vector3d::operator- (const Vector3d& rhs)
{
Vector3d result;
result._x = this->_x - rhs._x;
result._y = this->_y - rhs._y;
result._z = this->_z - rhs._z;
result._ox = 0;
result._oy = 0;
result._oz = 0;
result._dx = result._x;
result._dy = result._y;
result._dz = result._z;
return result;
}
double Vector3d::operator* (const Vector3d& rhs)
{
//dot product
double result;
result = this->_x*rhs._x +
this->_y*rhs._y +
this->_z*rhs._z;
return result;
}
Vector3d Vector3d::operator* (const float rhs)
{
//scale
Vector3d result;
result = Vector3d(rhs*this->_x, rhs*this->_y, rhs*this->_z);
return result;
}
Vector3d Vector3d::operator^ (const Vector3d& rhs)
{
Vector3d result;
result._x = this->_y*rhs._z - this->_z*rhs._y;
result._y = this->_z*rhs._x - this->_x*rhs._z;
result._z = this->_x*rhs._y - this->_y*rhs._x;
result._ox = 0;
result._oy = 0;
result._oz = 0;
result._dx = result._x;
result._dy = result._y;
result._dz = result._z;
return result;
}
void Vector3d::set_vector(const float ox, const float oy, const float oz,
const float dx, const float dy, const float dz)
{
_ox = ox;
_oy = oy;
_oz = oz;
_dx = dx;
_dy = dy;
_dz = dz;
_x = _dx-_ox;
_y = _dy-_oy;
_z = _dz-_oz;
}
void Vector3d::set_origin(const float ox, const float oy, const float oz)
{
_ox = ox;
_oy = oy;
_oz = oz;
}
void Vector3d::set_destination(const float dx, const float dy, const float dz)
{
_dx = dx;
_dy = dy;
_dz = dz;
}
void Vector3d::normalize_vector()
{
float length = sqrt( (_dx-_ox)*(_dx-_ox)+(_dy-_oy)*(_dy-_oy)+(_dz-_oz)*(_dz-_oz) );
_x = (_dx-_ox)/length;
_y = (_dy-_oy)/length;
_z = (_dz-_oz)/length;
}