-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3d.cpp
More file actions
74 lines (61 loc) · 1.48 KB
/
vector3d.cpp
File metadata and controls
74 lines (61 loc) · 1.48 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
#include "vector3d.h"
#include <cmath>
QVector3D Vector3D::QVec() {
return QVector3D(mx, my, mz);
}
double Vector3D::length() const
{
return sqrt(lengthSquared());
}
double Vector3D::lengthSquared() const
{
return mx * mx + my * my + mz * mz;
}
Vector3D Vector3D::normalized() const
{
double l = length();
return Vector3D(mx / l, my / l, mz / l);
}
void Vector3D::normalize() {
double l = length();
mx = mx / l;
my = my / l;
mz = mz / l;
}
Vector3D Vector3D::operator-(const Vector3D &b) const
{
return Vector3D(mx - b.mx, my - b.my, mz - b.mz);
}
Vector3D Vector3D::operator+(const Vector3D &b) const
{
return Vector3D(mx + b.mx, my + b.my, mz + b.mz);
}
Vector3D Vector3D::operator*(double c) const
{
return Vector3D(mx * c, my * c, mz * c);
}
Vector3D Vector3D::operator/(double c) const
{
return Vector3D(mx / c, my / c, mz / c);
}
double Vector3D::dotProduct(Vector3D const &a, Vector3D const &b)
{
return a.mx * b.mx + a.my * b.my + a.mz * b.mz;
}
Vector3D Vector3D::crossProduct(Vector3D const &a, Vector3D const &b)
{
Vector3D result;
result.mx = a.my*b.mz - a.mz*b.my;
result.my = -(a.mx*b.mz - a.mz*b.mx);
result.mz = a.mx*b.my - a.my*b.mx;
return result;
}
Vector3D Vector3D::orthognalVector() const
{
// https://math.stackexchange.com/a/211195
Vector3D a = Vector3D(mz, mz, -mx - my);
Vector3D b = Vector3D(-my - mz, mx, mx);
if (b.lengthSquared() > a.lengthSquared())
return b;
return a;
}