From 7d8aad2b14a8b519e624ab44aed24e0b7e4a0ac7 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jun 2019 11:08:12 +0800 Subject: [PATCH] add vec2 --- a8/vec2.cc | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++ a8/vec2.h | 35 ++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 a8/vec2.cc create mode 100644 a8/vec2.h diff --git a/a8/vec2.cc b/a8/vec2.cc new file mode 100644 index 0000000..a8c755e --- /dev/null +++ b/a8/vec2.cc @@ -0,0 +1,139 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include + +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)); + } +} diff --git a/a8/vec2.h b/a8/vec2.h new file mode 100644 index 0000000..3357e7e --- /dev/null +++ b/a8/vec2.h @@ -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