-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
117 lines (87 loc) · 1.78 KB
/
vector.cpp
File metadata and controls
117 lines (87 loc) · 1.78 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
#include <stdio.h>
#include <math.h>
#include "vector.h"
Vector::Vector(double px, double py, double pz,
double pw) : x(px), y(py), z(pz), w(pw) { }
Vector::Vector(const Vector& a) : x(a.x), y(a.y), z(a.z), w(a.w) { }
Vector::Vector(const float* v) : x(v[0]), y(v[1]), z(v[2]), w(v[3]) { }
Vector& Vector::operator=(const Vector& a) {
this->x=a.x;
this->y=a.y;
this->z=a.z;
this->w=a.w;
return *this;
}
Vector& Vector::operator+=(const Vector& a) {
this->x+=a.x;
this->y+=a.y;
this->z+=a.z;
this->w+=a.w;
return *this;
}
Vector& Vector::operator-=(const Vector& a) {
this->x-=a.x;
this->y-=a.y;
this->z-=a.z;
this->w-=a.w;
return *this;
}
Vector Vector::operator+(const Vector& a) const {
Vector res(*this);
res+=a;
return res;
}
Vector Vector::operator-(const Vector& a) const {
Vector res(*this);
res-=a;
return res;
}
Vector& Vector::operator*=(const double a) {
this->x*=a;
this->y*=a;
this->z*=a;
this->w*=a;
return *this;
}
Vector Vector::operator*(const double a) const {
Vector res(*this);
res*=a;
return res;
}
double Vector::operator*(const Vector& a) const {
double dot=0.0;
dot+=this->x*a.x;
dot+=this->y*a.y;
dot+=this->z*a.z;
dot+=this->w*a.w;
return dot;
}
Vector& Vector::operator/=(const double a) {
this->x/=a;
this->y/=a;
this->z/=a;
this->w/=a;
return *this;
}
Vector Vector::cross(const Vector& a) {
Vector res;
res.x = this->y*a.z - a.y*this->z;
res.y = -(this->x*a.z - a.x*this->z);
res.z = this->x*a.y - this->y*a.x;
return res;
}
double Vector::length() {
double dot = (*this)*(*this);
return sqrt(dot);
}
void Vector::normalize() {
double l=this->length();
this->x/=l;
this->y/=l;
this->z/=l;
this->w/=l;
}
void Vector::print() const {
printf("(%lf, %lf, %lf, %lf)\n", this->x,this->y,
this->z,this->w);
}