-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.hh
140 lines (109 loc) · 3 KB
/
Vector.hh
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
/*
Author: dok
Desc: Basic Vector class
*/
#ifndef Vector_hh
#define Vector_hh
#include <math.h>
#include <iostream>
using std::ostream;
class Vector
{
private:
float c[3];
public:
friend class Color;
/*---------------- Constructors -----------------*/
Vector(float x, float y, float z){ // parameter coordinate constructor
c[0] = x; c[1] = y; c[2] = z;
}
Vector(const Vector &v){ // copy construtor
c[0] = v.x(); c[1] = v.y(); c[2] = v.z();
}
Vector(){ // default constructor
c[0] = 0; c[1] = 0; c[2] = 0;
}
/*---------------- Accessors --------------------*/
float x()const{
return c[0];
}
float y() const{
return c[1];
}
float z() const{
return c[2];
}
/*-------------- Operators ---------------------*/
float operator[](int i) const{ // [] const operator
return c[i%3];
}
float& operator[](int i){ // [] operator
return c[i%3];
}
Vector operator-()const{ // - unary operator
return Vector(-c[0], -c[1], -c[2]);
}
Vector operator+(const Vector& v)const{ // + binary operator
return Vector(c[0]+v.x(), c[1]+v.y(), c[2]+v.z());
}
Vector operator-(const Vector& v)const{ // - binary operator
return Vector(c[0]-v.x(), c[1]-v.y(), c[2]-v.z());
}
Vector operator*(const Vector& v)const{ // * operator (wtf) not dot product
return Vector(c[0]*v.x(), c[1]*v.y(), c[2]*v.z());
}
Vector operator*(float d)const{ // * operator (scalar)
return Vector(c[0]*d, c[1]*d, c[2]*d);
}
Vector& operator+=(const Vector& v){
c[0] += v.x();
c[1] += v.y();
c[2] += v.z();
return *this;
}
Vector& operator-=(const Vector& v){
c[0] -= v.x();
c[1] -= v.y();
c[2] -= v.z();
return *this;
}
friend Vector operator*(float x, const Vector& v){
return Vector(v.x()*x, v.y()*x, v.z()*x);
}
friend Vector operator/(float x, const Vector& v){
return Vector(v.x()/x, v.y()/x, v.z()/x);
}
/*---------------- Vector Functions -------------*/
float Length()const{
return sqrt(this->Length2());
}
float Length2()const{
return c[0]*c[0] + c[1]*c[1] + c[2]*c[2];
}
Vector Normal()const{
float length = this->Length();
float div = 1/length;
return Vector(c[0]*div, c[1]*div, c[2]*div);
}
float Normalize(){
float length = this->Length();
float div = 1/length;
c[0] *= div; c[1] *= div; c[2] *= div;
return length;
}
friend float Dot(const Vector& v1, const Vector& v2){
return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z();
}
friend Vector Cross(const Vector& v1, const Vector& v2){
return Vector(
v1.y()*v2.z() - v1.z()*v2.y(),
v1.z()*v2.x() - v1.x()*v2.z(),
v1.x()*v2.y() - v1.y()*v2.x()
);
}
friend ostream& operator<<(ostream& os, const Vector& v){
os << "<" << v.x() << "," << v.y() << "," << v.z() << ">";
return os;
}
};
#endif