forked from uyras/partsEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vect.cpp
247 lines (214 loc) · 5.08 KB
/
Vect.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* File: Vect.cpp
* Author: uyras
*
* Created on 20 Ноябрь 2012 г., 15:55
*/
#include "config.h"
#include "Vect.h"
#include "Part.h"
#include <locale.h>
using namespace std;
Vect::Vect() {
this->x = 0;
this->y = 0;
this->z = 0;
}
Vect::Vect(double x, double y, double z) {
this->x = x;
this->y = y;
this->z = z;
}
void Vect::setUnitary(){
double length = this->length();
this->x /= length;
this->y /= length;
if (config::Instance()->dimensions()==3)
this->z /= length;
}
Vect Vect::normalize()
{
double length = this->length();
return Vect(this->x / length, this->y / length, this->z / length);
}
double Vect::scalar(Vect b) {
return (this->x * b.x) + (this->y * b.y) + (this->z * b.z);
}
/**
* Находит расстояние между двумя векторами
* @param a
* @param b
* @return
*/
double Vect::space(const Vect &b) const {
if (config::Instance()->dimensions()==2)
return sqrt(
(this->x - b.x)*(this->x - b.x) +
(this->y - b.y)*(this->y - b.y)
);
else
return sqrt(
(this->x - b.x)*(this->x - b.x) +
(this->y - b.y)*(this->y - b.y) +
(this->z - b.z)*(this->z - b.z)
);
}
double Vect::space_2(const Vect &b) const
{
if (config::Instance()->dimensions()==2)
return
(this->x - b.x)*(this->x - b.x) +
(this->y - b.y)*(this->y - b.y)
;
else
return
(this->x - b.x)*(this->x - b.x) +
(this->y - b.y)*(this->y - b.y) +
(this->z - b.z)*(this->z - b.z)
;
}
Vect Vect::radius(const Vect &b) const {
return Vect(b.x - this->x, b.y - this->y, b.z - this->z);
}
double Vect::length() const {
if (config::Instance()->dimensions()==2)
return sqrt(
this->x * this->x +
this->y * this->y
);
else
return sqrt(
this->x * this->x +
this->y * this->y +
this->z * this->z
);
}
void Vect::setXYZ(double x, double y, double z = 0) {
this->x = x;
this->y = y;
this->z = z;
//this->_length = NULL;
}
void Vect::rotate() {
this->x *= -1;
this->y *= -1;
this->z *= -1;
}
Vect Vect::operator=(const Vect& a) {
this->x = a.x;
this->y = a.y;
this->z = a.z;
return Vect(this->x,this->y,this->z);
}
Vect Vect::operator+=(const Vect& a)
{
this->x += a.x;
this->y += a.y;
this->z += a.z;
return *this;
}
Vect Vect::operator +(const Vect& a) const
{
Vect res;
res.x = this->x+a.x;
res.y = this->y+a.y;
res.z = this->z+a.z;
return res;
}
Vect Vect::operator-(const Vect & a) const
{
Vect res;
res.x = this->x-a.x;
res.y = this->y-a.y;
res.z = this->z-a.z;
return res;
}
Vect Vect::operator*(const double num) const
{
return Vect(
this->x*num,
this->y*num,
this->z*num
);
}
Vect Vect::operator*=(const double num)
{
this->x*=num;
this->y*=num;
this->z*=num;
return *this;
}
Vect Vect::operator/(const double num) const
{
return Vect(
this->x/num,
this->y/num,
this->z/num
);
}
Vect Vect::operator/=(const double num)
{
this->x/=num;
this->y/=num;
this->z/=num;
return *this;
}
bool Vect::operator==(const Vect & a) const
{
if (config::Instance()->dimensions()==2){
return (this->x==a.x && this->y==a.y);
} else {
return (this->x==a.x && this->y==a.y && this->z==a.z);
}
}
Vect Vect::crossProduct(const Vect &vect1, const Vect &vect2)
{
return Vect(
vect1.y*vect2.z-vect1.z*vect2.y,
vect1.z*vect2.x-vect1.x*vect2.z,
vect1.x*vect2.y-vect1.y*vect2.x
);
}
Vect Vect::normal(const Vect &vect1, const Vect &vect2)
{
return Vect::crossProduct(vect1,vect2).normalize();
}
void Vect::toAbs(){
this->x *= config::Instance()->vol;
this->y *= config::Instance()->vol;
this->z *= config::Instance()->vol;
}
void Vect::toRel(){
this->x /= config::Instance()->vol;
this->y /= config::Instance()->vol;
this->z /= config::Instance()->vol;
}
void Vect::draw() const{
cout<<"x="<<this->x<<"; y="<<this->y<<"; z="<<this->z<<"; l="<<this->length()<<endl;
}
double Vect::angle(){
if(this->x>0&&this->y>=0)
return atan(this->y/this->x);
if (this->x>0&&this->y<0)
return atan(this->y/this->x)+M_PI*2;
if(this->x<0)
return atan(this->y/this->x)+M_PI;
if(this->x==0&&this->y>0)
return M_PI_2;
if (this->x==0&&this->y<0)
return M_PI_2*3;
if(this->x==0&&this->y==0)
return 0.;
return 0.;
}
double Vect::grade(){
return this->angle()*180/M_PI;
}
ostream& operator<<(ostream & os, const Vect& p){
os<<"("<<p.x<<";"<<p.y;
if (config::Instance()->dimensions()==3){
os<<";"<<p.z;
}
os<<")";
return os;
}