-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
85 lines (82 loc) · 1.78 KB
/
vector.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 VECTOR_H
#define VECTOR_H
#include "constants.h"
#include <iostream>
#include <fstream>
#include <cassert>
#include <cmath>
using namespace std;
class Vector{
public:
double x;
double y;
double z;
Vector() {
x = y = z = 0;
}
Vector(double _x, double _y, double _z):x(_x),y(_y),z(_z){}
Vector(const Vector& vec) {
x = vec.x;
y = vec.y;
z = vec.z;
}
Vector operator+(const Vector& other) const {
return {x + other.x, y + other.y, z + other.z};
}
Vector operator-(const Vector& other) const {
return {x - other.x, y - other.y, z - other.z};
}
double operator*(const Vector& other) const {
return x*other.x + y*other.y + z*other.z;
}
Vector& operator+=(const Vector& other) {
x += other.x;
y += other.y;
z += other.z;
return *this;
}
Vector& operator-=(const Vector& other) {
x -= other.x;
y -= other.y;
z -= other.z;
return *this;
}
Vector operator*(double scalar) const {
return {x*scalar, y*scalar, z*scalar};
}
Vector& operator*=(double scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
return *this;
}
Vector operator/(double scalar) const {
assert((abs(scalar) > EPS) && "Division of vector by 0!");
return {x/scalar, y/scalar, z/scalar};
}
Vector& operator/=(double scalar) {
assert((abs(scalar) > EPS) && "Division of vector by 0!");
x /= scalar;
y /= scalar;
z /= scalar;
return *this;
}
double magsq() const {
return (x*x + y*y + z*z);
}
double mag() const {
return sqrt(magsq());
}
void normalize() {
double m = mag();
assert(m > EPS);
x /= m;
y /= m;
z /= m;
}
};
ostream& operator<<(ostream& out, const Vector& vec) {
out << "<" << vec.x << "," << vec.y << "," << vec.z << ">";
return out;
}
#endif