-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cpp
150 lines (123 loc) · 3.52 KB
/
Vector.cpp
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
146
147
148
149
150
#include "Vector.h"
#include <iostream>
#include<cmath>
#include<exception>
#include "VectorLengthException.h"
Vector::Vector():Point(){}
Vector::Vector(double x, double y, double z):Point(x,y,z) {
}
Vector::Vector(const Vector& obj) : Vector(obj) {}
Vector::Vector(const Point& a, const Point& b) {
this->x = b.getX() - a.getX();
this->y = b.getY() - a.getY();
this->z = b.getZ() - a.getZ();
}
Vector::~Vector()
{
}
std::istream& operator>>(std::istream & in,
Vector & rhs) {
double x, y, z;
std::cout << "Please enter x: ";
in >> x;
std::cout << "Please enter y: ";
in >> y;
std::cout << "Please enter z: ";
in >> z;
rhs.setX(x);
rhs.setY(y);
rhs.setZ(z);
return in;
}
double Vector::lengthOfVec()const {
// sqrt(x^2 + y^2 + z^2)
double res = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
return res;
}
Vector Vector::dirOfVec()const {
try {
if (lengthOfVec() == 0) {
throw VectorLengthException();
} //throws VectorLengthException if length is equal to zero
else {
return Vector(x / lengthOfVec(), y / lengthOfVec(), z / lengthOfVec());
}
}
catch (VectorLengthException& e) {
std::cout << e.what() << std::endl;
}
}
Vector Vector::proj(const Vector& obj)const {
//projv u = (u * v / |v|^2) * v
double upSide = (this->x * obj.x) + (this->y * obj.y) + (this->z * obj.z);
double downSide = pow(obj.lengthOfVec(), 2);
double all = upSide / downSide;
return Vector(all * obj.x, all * obj.y, all * obj.z);
}
bool Vector::checkIfNull()const {
if (x == y == z == 0) {
return true;
}
else {
return false;
}
}
bool Vector::checkifParallel(const Vector& v)const {
try {
if (lengthOfVec() == 0 || v.lengthOfVec() == 0) {
throw VectorLengthException();
} //throws VectorLengthException if length is equal to zero
}
catch (VectorLengthException& e) {
std::cout << e.what() << std::endl;
}
if ((this->x / v.x) == (this->y / v.y) == (this->z / v.z)) { // x:v1 = y:v2 = z:v3
return true;
}
else
return false;
}
bool Vector::checkIfPerpend(const Vector& obj)const {
try {
if (lengthOfVec() == 0 || obj.lengthOfVec() == 0) {
throw VectorLengthException();
}
if (((this->x * obj.x) + (this->y * obj.y) + (this->z * obj.z)) == 0) { // x.v1 + y.v2 + z.v3 = 0
return true;
}
else
return false;
}
catch (VectorLengthException& e) {
std::cout << e.what() << std::endl;
}
}
Vector Vector::operator+(const Vector&obj)const {
return Vector(this->x + obj.x, this->y + obj.y, this->z + obj.z);
}
Vector Vector::operator-(const Vector& obj)const {
return Vector(this->x - obj.x, this->y - obj.y, this->z - obj.z);
}
Vector Vector::operator*(double num)const {
return Vector(this->x * num, this->y * num, this->z * num);
}
double Vector::operator*(const Vector& obj)const {
//skalarno proizvedenie
return((this->x*obj.x) + (this->y*obj.y) + (this->z*obj.z));
}
Vector Vector::operator^(const Vector& obj)const {
//a ^ v = (y.v3 − z.v2, −x.v3 + z.v1, x.v2 − y.v1)
double x = (this->y * obj.z) - (this->z * obj.y);
double y = (-this->x * obj.z) + (this->z * obj.x);
double z = (this->x * obj.y) - (this->y * obj.x);
return Vector(x, y, z);
}
double Vector::operator()(const Vector& v1, const Vector& v2) {
double res = (this->x + v1.x + v2.x) + (this->y + v1.y + v2.y) + (this->z + v1.z + v2.z);
return res;
}
std::ostream& operator<<(std::ostream & out,
const Vector & rhs) {
out << rhs.x << ", " << rhs.y << ", " << rhs.z;
return out;
}