This commit is contained in:
aozhiwei 2019-06-25 11:08:12 +08:00
parent ca4bcdb5e2
commit 7d8aad2b14
2 changed files with 174 additions and 0 deletions

139
a8/vec2.cc Normal file
View File

@ -0,0 +1,139 @@
#include <math.h>
#include <a8/a8.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/vec2.hpp>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <a8/vec2.h>
namespace a8
{
const Vec2 Vec2::UP = Vec2(0.0f, 1.0f);
const Vec2 Vec2::RIGHT = Vec2(1.0f, 0.0f);
const Vec2 Vec2::DOWN = Vec2(0.0f, -1.0f);
const Vec2 Vec2::LEFT = Vec2(-1.0f, 0.0f);
void Vec2::Normalize()
{
#if 1
glm::vec2 v = glm::normalize(glm::vec2(x, y));
assert(!isnan(v[0]));
assert(!isnan(v[1]));
x = v[0];
y = v[1];
#else
Eigen::Vector2f v(x, y);
v.normalize();
x = v[0];
y = v[1];
#endif
}
bool Vec2::operator == (const Vec2& b) const
{
return std::abs(x - b.x) < 0.01f && std::abs(y - b.y) < 0.01f;
}
Vec2 Vec2::operator + (const Vec2& b) const
{
#if 1
glm::vec2 v = glm::vec2(x, y) + glm::vec2(b.x, b.y);
return Vec2(v[0], v[1]);
#else
Eigen::Vector2f v = Eigen::Vector2f(x, y) + Eigen::Vector2f(b.x, b.y);
return Vec2(v[0], v[1]);
#endif
}
Vec2 Vec2::operator - (const Vec2& b) const
{
#if 1
glm::vec2 v = glm::vec2(x, y) - glm::vec2(b.x, b.y);
return Vec2(v[0], v[1]);
#else
Eigen::Vector2f v = Eigen::Vector2f(x, y) - Eigen::Vector2f(b.x, b.y);
return Vec2(v[0], v[1]);
#endif
}
Vec2 Vec2::operator * (float scale) const
{
#if 1
glm::vec2 v = glm::vec2(x, y) * scale;
return Vec2(v[0], v[1]);
#else
Eigen::Vector2f v = Eigen::Vector2f(x, y) * scale;
return Vec2(v[0], v[1]);
#endif
}
Vec2 Vec2::operator / (float scale) const
{
#if 1
glm::vec2 v = glm::vec2(x, y) / scale;
return Vec2(v[0], v[1]);
#else
Eigen::Vector2f v = Eigen::Vector2f(x, y) * (1.0 / scale);
return Vec2(v[0], v[1]);
#endif
}
void Vec2::Rotate(float angle)
{
Eigen::Vector3f v(x, y, 0);
v = Eigen::AngleAxisf(angle * 3.1415926f, Eigen::Vector3f::UnitZ()) * v;
x = v[0];
y = v[1];
}
float Vec2::CalcAngle(const Vec2& b)
{
float a1 = acos(Dot(b) / Norm() / b.Norm());
float a2 = atan2(y, x);
float a3 = atan2(y, x) / 0.017 - 90.0f;
#if 0
return a2;
#else
bool at_right_side = Vec2::RIGHT.Dot(*this) > 0.0001f;
if (at_right_side) {
a1 = -a1;
}
return a1 / 3.1415926f;
// return a3 / 360.0f;
#endif
}
Vec2 Vec2::FromAngle(float angle)
{
Vec2 vec2;
float hu = angle * 3.1415926f / 180.0f;
vec2.x = cos(hu);
vec2.y = sin(hu);
vec2.Normalize();
return vec2;
}
float Vec2::Distance(const Vec2& b)
{
Vec2 v = b - *this;
return v.Norm();
}
Vec2 Vec2::Perp()
{
return Vec2(y, -x);
}
float Vec2::Dot(const Vec2& v) const
{
return x*v.x + y*v.y;
}
float Vec2::Norm() const
{
return fabs(sqrt(x*x + y*y));
}
}

35
a8/vec2.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef A8_VEC2_H
#define A8_VEC2_H
namespace a8
{
struct Vec2
{
float x = 0.0f;
float y = 0.0f;
Vec2(float _x = 0.0f, float _y = 0.0f):x(_x), y(_y) {};
void Normalize();
void Rotate(float angle);
float CalcAngle(const Vec2& b);
static Vec2 FromAngle(float angle);
float Distance(const Vec2& b);
bool operator == (const Vec2& b) const;
Vec2 operator + (const Vec2& b) const;
Vec2 operator - (const Vec2& b) const;
Vec2 operator * (float scale) const;
Vec2 operator / (float scale) const;
Vec2 Perp();
float Dot(const Vec2& v) const;
float Norm() const;
static const Vec2 UP;
static const Vec2 DOWN;
static const Vec2 LEFT;
static const Vec2 RIGHT;
};
}
#endif