Vec2.h
上传用户:chinafayin
上传日期:2022-04-05
资源大小:153k
文件大小:6k
源码类别:

并行计算

开发平台:

Visual C++

  1. /*
  2.     Linear Algebra / Math library
  3.     Copyright (C) 2003-2009, Marek Olsak (maraeo@gmail.com), All Rights Reserved.
  4.     Copyright (C) 2003-2005, Tomas Pastorek (tomas@tomaspastorek.cz), All Rights Reserved.
  5.     This program is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU General Public License
  7.     as published by the Free Software Foundation; either version 2
  8.     of the License, or (at your option) any later version.
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.     You should have received a copy of the GNU General Public License
  14.     along with this program; if not, write to the Free Software
  15.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  16. */
  17. #pragma once
  18. namespace Rune
  19. {
  20.     /**
  21.         Dvourozmerny vektor
  22.     **************************************************************************************************/
  23.     template<typename T>
  24.     class Vec2
  25.     {
  26.     public:
  27.         T x, y;
  28.         Vec2() {}
  29.         Vec2(T f) { x = f; y = f; }
  30.         Vec2(T X, T Y) { x = X; y = Y; }
  31.         bool operator ==(const Vec2 &v) const           { return x == v.x && y == v.y; }
  32.         bool operator !=(const Vec2 &v) const           { return x != v.x && y != v.y; }
  33.         bool operator <(const Vec2 &v) const            { return x < v.x && y < v.y; }
  34.         bool operator >(const Vec2 &v) const            { return x > v.x && y > v.y; }
  35.         bool operator <=(const Vec2 &v) const           { return x <= v.x && y <= v.y; }
  36.         bool operator >=(const Vec2 &v) const           { return x >= v.x && y >= v.y; }
  37.         bool operator ==(T f) const                     { return x == f && y == f; }
  38.         bool operator !=(T f) const                     { return x != f && y != f; }
  39.         bool operator <(T f) const                      { return x < f && y < f; }
  40.         bool operator >(T f) const                      { return x > f && y > f; }
  41.         bool operator <=(T f) const                     { return x <= f && y <= f; }
  42.         bool operator >=(T f) const                     { return x >= f && y >= f; }
  43.         void operator =(const Vec2 &v)                  { x = v.x; y = v.y; }
  44.         void operator +=(const Vec2 &v)                 { x += v.x; y += v.y; }
  45.         void operator +=(T f)                           { x += f; y += f; }
  46.         void operator -=(const Vec2 &v)                 { x -= v.x; y -= v.y; }
  47.         void operator -=(T f)                           { x -= f; y -= f; }
  48.         void operator *=(T f)                           { x *= f; y *= f; }
  49.         void operator /=(T f)                           { x /= f; y /= f; }
  50.         Vec2 operator +(const Vec2 &v) const            { return Vec2(x+v.x, y+v.y); }
  51.         Vec2 operator +(T f) const                      { return Vec2(x+f, y+f); }
  52.         Vec2 operator -(const Vec2 &v) const            { return Vec2(x-v.x, y-v.y); }
  53.         Vec2 operator -(T f) const                      { return Vec2(x-f, y-f); }
  54.         Vec2 operator *(T f) const                      { return Vec2(x*f, y*f); }
  55.         Vec2 operator *(const Vec2 &v) const            { return Vec2(x*v.x, y*v.y); }
  56.         Vec2 operator /(T f) const                      { return Vec2(x/f, y/f); }
  57.         Vec2 operator -() const                         { return Vec2(-x, -y); }
  58.         T& operator [](int i)                           { return (&x)[i]; }
  59.         T operator [](int i) const                      { return (&x)[i]; }
  60.         T Square() const                                { return x*y; }
  61.         bool SafeIsEqual(const Vec2 &v) const           { return Math<T>::SafeIsEqual(x, v.x) && Math<T>::SafeIsEqual(y, v.y); }
  62.         Vec2 GetAbs() const                             { return Vec2(Math<T>::Abs(x), Math<T>::Abs(y)); }
  63.         Vec2 GetNormalized() const                      { T f = MagnitudeInv(); return Vec2(x*f, y*f); }
  64.         Vec2 GetNormal() const                          { return Vec2(-y, x); }
  65.         T Magnitude() const                             { return Math<T>::Sqrt(MagnitudeSqr()); }
  66.         T MagnitudeInv() const                          { return Math<T>::Rsqrt(MagnitudeSqr()); }
  67.         T MagnitudeSqr() const                          { return x*x + y*y; }
  68.         void Normalize()                                { operator *=(MagnitudeInv()); }
  69.         bool IsEmpty() const                            { return x == 0 && y == 0; }
  70.         void PackTo01()                                 { operator *=(T(0.5)); operator +=(T(0.5)); }
  71.         void PackBack()                                 { operator -=(T(0.5)); operator *=(2); }
  72.         void Set(T X, T Y)                              { x = X; y = Y; }
  73.         void Inverse()                                  { x = 1/x; y = 1/y; }
  74.         Vec2 Frac() const                               { return Vec2(Math<T>::Frac(x), Math<T>::Frac(y)); }
  75.         operator T* ()                                  { return &x; }
  76.         operator const T* () const                      { return &x; }
  77.         static T Dot(const Vec2 &v1, const Vec2 &v2)            { return v1.x*v2.x + v1.y*v2.y; }
  78.         static T DistanceSqr(const Vec2 &v1, const Vec2 &v2)    { return (v1 - v2).MagnitudeSqr(); }
  79.         static T Distance(const Vec2 &v1, const Vec2 &v2)       { return Math<T>::Sqrt(DistanceSqr(v1, v2)); }
  80.         static T Angle(const Vec2 &a, const Vec2 &b)            { return Math<T>::Acos(Dot(a, b)*a.MagnitudeInv()*b.MagnitudeInv()); }
  81.         static T AngleUN(const Vec2 &a, const Vec2 &b)          { return Math<T>::Acos(Dot(a, b)); }
  82.         static Vec2 Center(const Vec2 &min, const Vec2 &max) { return (min + max) / 2; }
  83.         static Vec2 Reflect(const Vec2 &impact, const Vec2 &normal) { return impact - 2*Dot(normal, impact)*normal; }
  84.     };
  85.     template<typename T>
  86.     std::ostream& operator <<(std::ostream &stream, const Vec2<T> &v)
  87.     {
  88.         return stream << ("(" + tostrf(v.x) + ", " + tostrf(v.y) + ")");
  89.     }
  90.     template<typename T>
  91.     std::istream& operator >>(std::istream &stream, Vec2<T> &v)
  92.     {
  93.         char tmp;
  94.         return stream >> tmp >> v.x >> tmp >> v.y >> tmp;
  95.     }
  96.     typedef Vec2<float> Vec2f;
  97.     typedef Vec2<double> Vec2d;
  98.     typedef Vec2<int32> Vec2i;
  99.     typedef Vec2<bool> Vec2b;
  100. }