#include <iostream>
#include <cmath>
using namespace std;
class Vector {
private:
double x, y, z;
int id;
static int counter;
public:
Vector() : x(0), y(0), z(0) {
id = ++counter;
cout << "Constructor N" << id << endl;
}
Vector(double x_, double y_, double z_) : x(x_), y(y_), z(z_) {
id = ++counter;
cout << "Constructor N" << id << endl;
}
~Vector() {
cout << "Destructor N" << id << endl;
}
double getX() const { return x; }
double getY() const { return y; }
double getZ() const { return z; }
void setX(double v) { x = v; }
void setY(double v) { y = v; }
void setZ(double v) { z = v; }
double length() const {
return sqrt(x*x + y*y + z*z);
}
Vector copy() const {
return Vector(x, y, z);
}
Vector multiply(double k) const {
return Vector(x*k, y*k, z*k);
}
Vector normalize() const {
double len = length();
return Vector(x/len, y/len, z/len);
}
void print() const {
cout << "x = " << x << "; y = " << y << "; z = " << z << endl;
}
friend Vector pluss(const Vector& a, const Vector& b);
friend Vector minuss(const Vector& a, const Vector& b);
friend Vector vectornoeproizved(const Vector& a, const Vector& b);
friend double scalar(const Vector& a, const Vector& b);
friend double cosAngle(const Vector& a, const Vector& b);
friend double sinAngle(const Vector& a, const Vector& b);
friend double angleDegrees(const Vector& a, const Vector& b);
};
int Vector::counter = 0;
Vector pluss(const Vector& a, const Vector& b) {
return Vector(a.x + b.x, a.y + b.y, a.z + b.z);
}
Vector minuss(const Vector& a, const Vector& b) {
return Vector(a.x - b.x, a.y - b.y, a.z - b.z);
}
Vector vectornoeproizved(const Vector& a, const Vector& b) {
return Vector(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
);
}
double scalar(const Vector& a, const Vector& b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
double cosAngle(const Vector& a, const Vector& b) {
return scalar(a, b) / (a.length() * b.length());
}
double sinAngle(const Vector& a, const Vector& b) {
return vectornoeproizved(a, b).length() / (a.length() * b.length());
}
double angleDegrees(const Vector& a, const Vector& b) {
return atan2(sinAngle(a, b), cosAngle(a, b)) * 180.0 / M_PI;
}
int main() {
Vector A(1, 2, 3);
Vector B(4, 5, 6);
cout << "\nA: "; A.print();
cout << "B: "; B.print();
cout << "\n|A| = " << A.length() << endl;
cout << "|B| = " << B.length() << endl;
cout << "\nA + B: "; pluss(A, B).print();
cout << "A - B: "; minuss(A, B).print();
cout << "A x B: "; vectornoeproizved(A, B).print();
cout << "\nA*B = " << scalar(A, B) << endl;
cout << "cos = " << cosAngle(A, B) << endl;
cout << "sin = " << sinAngle(A, B) << endl;
cout << "Ugol = " << angleDegrees(A, B) << " gradusov" << endl;
return 0;
}