csgeom/vector2.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csgeom/vector2.h
Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2000 by Jorrit Tyberghein
    00003     Largely rewritten by Ivan Avramovic <ivan@avramovic.com>
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_VECTOR2_H__
    00021 #define __CS_VECTOR2_H__
    00022
    00023 #include "csextern.h"
    00024 #include "csqsqrt.h"
    00025
    00033 class csString;
    00034
    00038 class CS_CRYSTALSPACE_EXPORT csVector2
    00039 {
    00040 public:
    00042   float x;
    00044   float y;
    00045
    00047   csVector2 () {}
    00048
    00050   csVector2 (float v)
    00051     : x (v), y (v)
    00052   {}
    00053
    00055   csVector2 (float x, float y)
    00056     : x (x), y (y)
    00057   { }
    00058
    00060   csVector2 (const csVector2& o)
    00061     : x (o.x), y (o.y)
    00062   {}
    00063
    00065   csString Description() const;
    00066
    00068   inline void Set (float ix, float iy)
    00069   { x = ix; y = iy; }
    00070
    00072   inline void Set (csVector2 const& v)
    00073   { x = v.x; y = v.y; }
    00074
    00076   inline void Set (float const* v) { x = v[0]; y = v[1]; }
    00077
    00079   inline void Set (float v) { x = y = v; }
    00080
    00082   inline void Get (float* v) { v[0] = x; v[1] = y; }
    00083
    00085   inline static float Norm (csVector2 const& v)
    00086   { return csQsqrt(v * v); }
    00087
    00089   inline float Norm () const
    00090   { return csQsqrt(SquaredNorm()); }
    00091
    00093   inline float InverseNorm () const
    00094   { return csQisqrt(SquaredNorm()); }
    00095
    00097   inline static csVector2 Unit (const csVector2& v)
    00098   { return v.Unit(); }
    00099
    00105   inline csVector2 Unit () const
    00106   { return (*this)*(this->InverseNorm()); }
    00107
    00109   inline void Normalize ()
    00110   {
    00111     float sqlen = SquaredNorm();
    00112     if (sqlen < SMALL_EPSILON) return ;
    00113
    00114     float invlen = csQisqrt (sqlen);
    00115     *this *= invlen;
    00116   }
    00117
    00119   inline bool IsZero (float precision = SMALL_EPSILON) const
    00120   {
    00121     return (fabsf(x) < precision) && (fabsf(y) < precision);
    00122   }
    00123
    00125   inline csVector2 UnitAxisClamped () const
    00126   {
    00127     if (IsZero())
    00128       return csVector2(0, 0);
    00129
    00130     if (fabsf(x) > fabsf(y))
    00131       return csVector2(x / fabsf (x), 0); //X biggest
    00132     else
    00133       return csVector2(0, y / fabsf (y)); //Y biggest
    00134   }
    00135
    00137   inline float SquaredNorm () const
    00138   { return x * x + y * y; }
    00139
    00141   void Rotate (float angle);
    00142
    00147   inline float IsLeft (const csVector2& p0, const csVector2& p1)
    00148   {
    00149     return (p1.x - p0.x)*(y - p0.y) - (x - p0.x)*(p1.y - p0.y);
    00150   }
    00151
    00153   inline csVector2& operator+= (const csVector2& v)
    00154   { x += v.x;  y += v.y;  return *this; }
    00155
    00157   inline csVector2& operator-= (const csVector2& v)
    00158   { x -= v.x;  y -= v.y;  return *this; }
    00159
    00161   inline csVector2& operator*= (float f)
    00162   { x *= f;  y *= f;  return *this; }
    00163
    00165   inline csVector2& operator/= (float f)
    00166   {
    00167     f = 1.0f / f;
    00168     x *= f;
    00169     y *= f;
    00170     return *this;
    00171   }
    00172
    00174   inline csVector2 operator+ () const
    00175   { return *this; }
    00176
    00178   inline csVector2 operator- () const
    00179   { return csVector2 (-x,-y); }
    00180
    00182   inline friend csVector2 operator+ (const csVector2& v1, const csVector2& v2)
    00183   { return csVector2 (v1.x + v2.x, v1.y + v2.y); }
    00184
    00186   inline friend csVector2 operator- (const csVector2& v1, const csVector2& v2)
    00187   { return csVector2 (v1.x - v2.x, v1.y - v2.y); }
    00188
    00190   inline friend float operator* (const csVector2& v1, const csVector2& v2)
    00191   { return v1.x * v2.x + v1.y * v2.y; }
    00192
    00194   inline friend csVector2 operator* (const csVector2& v, float f)
    00195   { return csVector2 (v.x * f, v.y * f); }
    00196
    00198   inline friend csVector2 operator* (float f, const csVector2& v)
    00199   { return csVector2 (v.x * f, v.y * f); }
    00200
    00202   inline friend csVector2 operator/ (const csVector2& v, float f)
    00203   {
    00204     f = 1.0f / f;
    00205     return csVector2 (v.x * f, v.y * f);
    00206   }
    00207
    00209   inline friend bool operator== (const csVector2& v1, const csVector2& v2)
    00210   { return (v1.x == v2.x) && (v1.y == v2.y); }
    00211
    00213   inline friend bool operator!= (const csVector2& v1, const csVector2& v2)
    00214   { return (v1.x != v2.x) || (v1.y != v2.y); }
    00215
    00217   inline friend bool operator< (const csVector2& v, float f)
    00218   { return ABS(v.x)<f && ABS(v.y)<f; }
    00219
    00221   inline friend bool operator> (float f, const csVector2& v)
    00222   { return ABS(v.x)<f && ABS(v.y)<f; }
    00223
    00225   inline float operator[] (int n) const
    00226   { return !n?x:y; }
    00227
    00229   inline float & operator[] (int n)
    00230   { return !n?x:y; }
    00231 };
    00232
    00235 #endif // __CS_VECTOR2_H__