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 (x * x + y * y); } 00091 00093 inline float SquaredNorm () const 00094 { return x * x + y * y; } 00095 00097 void Rotate (float angle); 00098 00103 inline float IsLeft (const csVector2& p0, const csVector2& p1) 00104 { 00105 return (p1.x - p0.x)*(y - p0.y) - (x - p0.x)*(p1.y - p0.y); 00106 } 00107 00109 inline csVector2& operator+= (const csVector2& v) 00110 { x += v.x; y += v.y; return *this; } 00111 00113 inline csVector2& operator-= (const csVector2& v) 00114 { x -= v.x; y -= v.y; return *this; } 00115 00117 inline csVector2& operator*= (float f) 00118 { x *= f; y *= f; return *this; } 00119 00121 inline csVector2& operator/= (float f) 00122 { 00123 f = 1.0f / f; 00124 x *= f; 00125 y *= f; 00126 return *this; 00127 } 00128 00130 inline csVector2 operator+ () const 00131 { return *this; } 00132 00134 inline csVector2 operator- () const 00135 { return csVector2 (-x,-y); } 00136 00138 inline friend csVector2 operator+ (const csVector2& v1, const csVector2& v2) 00139 { return csVector2 (v1.x + v2.x, v1.y + v2.y); } 00140 00142 inline friend csVector2 operator- (const csVector2& v1, const csVector2& v2) 00143 { return csVector2 (v1.x - v2.x, v1.y - v2.y); } 00144 00146 inline friend float operator* (const csVector2& v1, const csVector2& v2) 00147 { return v1.x * v2.x + v1.y * v2.y; } 00148 00150 inline friend csVector2 operator* (const csVector2& v, float f) 00151 { return csVector2 (v.x * f, v.y * f); } 00152 00154 inline friend csVector2 operator* (float f, const csVector2& v) 00155 { return csVector2 (v.x * f, v.y * f); } 00156 00158 inline friend csVector2 operator/ (const csVector2& v, float f) 00159 { 00160 f = 1.0f / f; 00161 return csVector2 (v.x * f, v.y * f); 00162 } 00163 00165 inline friend bool operator== (const csVector2& v1, const csVector2& v2) 00166 { return (v1.x == v2.x) && (v1.y == v2.y); } 00167 00169 inline friend bool operator!= (const csVector2& v1, const csVector2& v2) 00170 { return (v1.x != v2.x) || (v1.y != v2.y); } 00171 00173 inline friend bool operator< (const csVector2& v, float f) 00174 { return ABS(v.x)<f && ABS(v.y)<f; } 00175 00177 inline friend bool operator> (float f, const csVector2& v) 00178 { return ABS(v.x)<f && ABS(v.y)<f; } 00179 00181 inline float operator[] (int n) const 00182 { return !n?x:y; } 00183 00185 inline float & operator[] (int n) 00186 { return !n?x:y; } 00187 }; 00188 00191 #endif // __CS_VECTOR2_H__
Generated for Crystal Space 1.4.1 by doxygen 1.7.1