80 lines
1.5 KiB
C++
80 lines
1.5 KiB
C++
/*
|
|
# ______ ____ ___
|
|
# | \/ ____| |___|
|
|
# | | | \ | |
|
|
#-----------------------------------------------------------------------
|
|
# Copyright 2022, tyra - https://github.com/h4570/tyra
|
|
# Licenced under Apache License 2.0
|
|
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
|
|
*/
|
|
|
|
#include "math/vec2.hpp"
|
|
#include <iomanip>
|
|
|
|
namespace Tyra {
|
|
|
|
Vec2::Vec2(const float& t_x, const float& t_y) {
|
|
x = t_x;
|
|
y = t_y;
|
|
}
|
|
|
|
Vec2::Vec2(const Vec2& v) {
|
|
x = v.x;
|
|
y = v.y;
|
|
}
|
|
|
|
Vec2::Vec2() {
|
|
x = 0;
|
|
y = 0;
|
|
}
|
|
|
|
Vec2::~Vec2() {}
|
|
|
|
void Vec2::set(const float& t_x, const float& t_y) {
|
|
x = t_x;
|
|
y = t_y;
|
|
}
|
|
|
|
void Vec2::set(const Vec2& v) {
|
|
x = v.x;
|
|
y = v.y;
|
|
}
|
|
|
|
void Vec2::rotate(const float& t_angle, const float& t_x, const float& t_y) {
|
|
float s = Math::sin(t_angle);
|
|
float c = Math::cos(t_angle);
|
|
|
|
x -= t_x;
|
|
y -= t_y;
|
|
|
|
float xnew = x * c - y * s;
|
|
float ynew = x * s + y * c;
|
|
|
|
x = xnew + t_x;
|
|
y = ynew + t_y;
|
|
}
|
|
|
|
void Vec2::print() const {
|
|
auto text = getPrint(nullptr);
|
|
printf("%s\n", text.c_str());
|
|
}
|
|
|
|
void Vec2::print(const char* name) const {
|
|
auto text = getPrint(name);
|
|
printf("%s\n", text.c_str());
|
|
}
|
|
|
|
std::string Vec2::getPrint(const char* name) const {
|
|
std::stringstream res;
|
|
if (name) {
|
|
res << name << "(";
|
|
} else {
|
|
res << "Vec2(";
|
|
}
|
|
res << std::fixed << std::setprecision(4);
|
|
res << x << ", " << y << ")";
|
|
return res.str();
|
|
}
|
|
|
|
} // namespace Tyra
|