zeus/src/CVector2f.cpp

50 lines
1.1 KiB
C++
Raw Normal View History

2016-03-04 15:03:26 -08:00
#include "zeus/CVector2f.hpp"
2015-05-03 22:41:38 -07:00
#include <memory.h>
#include <cmath>
2017-12-29 00:06:22 -08:00
#include <cassert>
2016-03-04 15:03:26 -08:00
#include "zeus/Math.hpp"
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
namespace zeus {
2015-05-03 22:41:38 -07:00
const CVector2f CVector2f::skOne = CVector2f(1.0);
const CVector2f CVector2f::skNegOne = CVector2f(-1.0);
2017-03-17 16:30:14 -07:00
const CVector2f CVector2f::skZero(0.f, 0.f);
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
float CVector2f::getAngleDiff(const CVector2f& a, const CVector2f& b) {
float mag1 = a.magnitude();
float mag2 = b.magnitude();
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
if (!mag1 || !mag2)
return 0;
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
float dot = a.dot(b);
float theta = std::acos(dot / (mag1 * mag2));
return theta;
2015-05-03 22:41:38 -07:00
}
2018-12-07 17:16:50 -08:00
CVector2f CVector2f::slerp(const CVector2f& a, const CVector2f& b, float t) {
if (t <= 0.0f)
return a;
if (t >= 1.0f)
return b;
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
CVector2f ret;
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
float mag = std::sqrt(a.dot(a) * b.dot(b));
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
float prod = a.dot(b) / mag;
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
if (std::fabs(prod) < 1.0f) {
const double sign = (prod < 0.0f) ? -1.0f : 1.0f;
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
const double theta = std::acos(sign * prod);
const double s1 = std::sin(sign * t * theta);
const double d = 1.0 / std::sin(theta);
const double s0 = std::sin((1.0f - t) * theta);
2015-05-03 22:41:38 -07:00
2018-12-07 17:16:50 -08:00
ret = (a * s0 + b * s1) * d;
return ret;
}
return a;
2015-05-03 22:41:38 -07:00
}
2018-12-07 21:23:50 -08:00
} // namespace zeus