## Public API Reference

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

- 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
    00032 class csString;
    00033
    00037 class CS_CRYSTALSPACE_EXPORT csVector2
    00038 {
    00039 public:
    00041   float x;
    00043   float y;
    00044
    00046   csVector2 () {}
    00047
    00049   csVector2 (float v)
    00050     : x (v), y (v)
    00051   {}
    00052
    00054   csVector2 (float x, float y)
    00055     : x (x), y (y)
    00056   { }
    00057
    00059   csVector2 (const csVector2& o)
    00060     : x (o.x), y (o.y)
    00061   {}
    00062
    00064   csString Description() const;
    00065
    00067   inline void Set (float ix, float iy)
    00068   { x = ix; y = iy; }
    00069
    00071   inline void Set (csVector2 const& v)
    00072   { x = v.x; y = v.y; }
    00073
    00075   inline void Set (float const* v) { x = v[0]; y = v[1]; }
    00076
    00078   inline void Set (float v) { x = y = v; }
    00079
    00081   inline void Get (float* v) { v[0] = x; v[1] = y; }
    00082
    00084   static float Norm (csVector2 const& v);
    00085
    00087   float Norm () const;
    00088
    00090   inline float SquaredNorm () const
    00091   { return x * x + y * y; }
    00092
    00094   void Rotate (float angle);
    00095
    00100   inline float IsLeft (const csVector2& p0, const csVector2& p1)
    00101   {
    00102     return (p1.x - p0.x)*(y - p0.y) - (x - p0.x)*(p1.y - p0.y);
    00103   }
    00104
    00106   csVector2& operator+= (const csVector2& v)
    00107   { x += v.x;  y += v.y;  return *this; }
    00108
    00110   csVector2& operator-= (const csVector2& v)
    00111   { x -= v.x;  y -= v.y;  return *this; }
    00112
    00114   csVector2& operator*= (float f) { x *= f;  y *= f;  return *this; }
    00115
    00117   csVector2& operator/= (float f)
    00118   {
    00119     f = 1.0f / f;
    00120     x *= f;
    00121     y *= f;
    00122     return *this;
    00123   }
    00124
    00126   inline csVector2 operator+ () const { return *this; }
    00127
    00129   inline csVector2 operator- () const { return csVector2(-x,-y); }
    00130
    00132   friend CS_CRYSTALSPACE_EXPORT csVector2 operator+ (const csVector2& v1,
    00133     const csVector2& v2);
    00135   friend CS_CRYSTALSPACE_EXPORT csVector2 operator- (const csVector2& v1,
    00136     const csVector2& v2);
    00138   friend CS_CRYSTALSPACE_EXPORT float operator* (const csVector2& v1,
    00139     const csVector2& v2);
    00141   friend CS_CRYSTALSPACE_EXPORT csVector2 operator* (const csVector2& v,
    00142         float f);
    00144   friend CS_CRYSTALSPACE_EXPORT csVector2 operator* (float f,
    00145         const csVector2& v);
    00147   friend CS_CRYSTALSPACE_EXPORT csVector2 operator/ (const csVector2& v,
    00148         float f);
    00150   friend CS_CRYSTALSPACE_EXPORT bool operator== (const csVector2& v1,
    00151     const csVector2& v2);
    00153   friend CS_CRYSTALSPACE_EXPORT bool operator!= (const csVector2& v1,
    00154     const csVector2& v2);
    00155
    00157   inline friend bool operator< (const csVector2& v, float f)
    00158   { return ABS(v.x)<f && ABS(v.y)<f; }
    00159
    00161   inline friend bool operator> (float f, const csVector2& v)
    00162   { return ABS(v.x)<f && ABS(v.y)<f; }
    00163
    00164
    00166   inline float operator[] (int n) const { return !n?x:y; }
    00168   inline float & operator[] (int n) { return !n?x:y; }
    00169 };
    00170
    00173 #endif // __CS_VECTOR2_H__