shooter/engine/Vec4D.cpp

75 lines
1.7 KiB
C++
Raw Normal View History

2021-10-28 16:58:02 +03:00
//
// Created by Иван Ильин on 12.01.2021.
//
#include <cmath>
#include <stdexcept>
2021-10-31 11:39:08 +03:00
#include "Vec4D.h"
#include "Consts.h"
Vec4D::Vec4D(double x, double y, double z, double w) {
2021-10-28 16:58:02 +03:00
_arr_point[0] = x;
_arr_point[1] = y;
_arr_point[2] = z;
_arr_point[3] = w;
}
Vec4D::Vec4D(const Vec4D &point4D) {
_arr_point[0] = point4D.x();
_arr_point[1] = point4D.y();
_arr_point[2] = point4D.z();
_arr_point[3] = point4D.w();
}
[[nodiscard]] Vec4D Vec4D::operator-() const {
return Vec4D(-x(), -y(), -z(), -w());
}
2021-10-31 11:39:08 +03:00
bool Vec4D::operator==(const Vec4D &point4D) const {
2021-10-28 16:58:02 +03:00
return (*this - point4D).sqrAbs() < Consts::EPS;
}
2021-10-31 11:39:08 +03:00
bool Vec4D::operator!=(const Vec4D &point4D) const {
2021-10-28 16:58:02 +03:00
return !(*this == point4D);
}
// Operations with Vec4D
2021-10-31 11:39:08 +03:00
Vec4D Vec4D::operator+(const Vec4D &point4D) const {
2021-10-28 16:58:02 +03:00
return Vec4D(x() + point4D.x(), y() + point4D.y(), z() + point4D.z(), w() + point4D.w());
}
2021-10-31 11:39:08 +03:00
Vec4D Vec4D::operator-(const Vec4D &point4D) const {
2021-11-04 03:35:34 +03:00
return *this + -point4D;
2021-10-28 16:58:02 +03:00
}
Vec4D Vec4D::operator*(double number) const {
return Vec4D(x() * number, y() * number, z() * number, w() * number);
}
Vec4D Vec4D::operator/(double number) const {
2021-10-31 11:39:08 +03:00
if (std::abs(number) > Consts::EPS) {
2021-11-04 03:35:34 +03:00
return *this * (1.0 / number);
2021-10-28 16:58:02 +03:00
} else {
throw std::domain_error{"Vec4D::operator/(double number): division by zero"};
}
}
// Other useful methods
double Vec4D::sqrAbs() const {
2022-03-03 21:54:01 +03:00
return x() * x() + y() * y() + z() * z() + w() * w();
2021-10-28 16:58:02 +03:00
}
double Vec4D::abs() const {
return sqrt(sqrAbs());
}
Vec4D Vec4D::normalized() const {
2021-11-04 03:35:34 +03:00
double vecAbs = sqrAbs();
2021-10-31 11:39:08 +03:00
if (vecAbs > Consts::EPS) {
2021-11-04 03:35:34 +03:00
return *this / sqrt(vecAbs);
2021-10-28 16:58:02 +03:00
} else {
return Vec4D(1);
}
}