vec2.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 **    Mark Page
00028 **    Harry Storbacka
00029 */
00030 
00033 
00034 #pragma once
00035 
00036 #include "../api_core.h"
00037 #include "cl_math.h"
00038 #include "vec1.h"
00039 #include "vec3.h"
00040 #include "vec4.h"
00041 #include "origin.h"
00042 
00043 template<typename Type>
00044 class CL_Vec1;
00045 
00046 template<typename Type>
00047 class CL_Vec2;
00048 
00049 template<typename Type>
00050 class CL_Vec3;
00051 
00052 template<typename Type>
00053 class CL_Vec4;
00054 
00055 template<typename Type>
00056 class CL_Mat2;
00057 
00058 template<typename Type>
00059 class CL_Mat3;
00060 
00061 template<typename Type>
00062 class CL_Mat4;
00063 
00064 template<typename Type>
00065 class CL_Sizex;
00066 
00067 template<typename Type>
00068 class CL_Pointx;
00069 
00070 class CL_Angle;
00071 
00078 template<typename Type>
00079 class CL_Vec2
00080 {
00081 public:
00082         typedef Type datatype;
00083 
00084         union { Type x; Type s; Type r; };
00085         union { Type y; Type t; Type g; };
00086 
00087         CL_Vec2() : x(0), y(0) { }
00088         CL_Vec2(const CL_Vec1<Type> &copy) { x = copy.x; y = 0;}
00089         CL_Vec2(const CL_Vec3<Type> &copy) { x = copy.x; y = copy.y;}
00090         CL_Vec2(const CL_Vec4<Type> &copy) { x = copy.x; y = copy.y;}
00091         CL_Vec2(const Type &p1, const Type &p2 = 0) : x(p1), y(p2) { }
00092         CL_Vec2(const Type *array_xy) : x(array_xy[0]), y(array_xy[1]) { }
00093         CL_Vec2(const CL_Pointx<int> &point);
00094         CL_Vec2(const CL_Pointx<float> &point);
00095         CL_Vec2(const CL_Pointx<double> &point);
00096 
00097         CL_Vec2(const CL_Vec2<double> &copy);
00098         CL_Vec2(const CL_Vec2<float> &copy);
00099         CL_Vec2(const CL_Vec2<int> &copy);
00100 
00107         static CL_Vec2<Type> normalize(const CL_Vec2<Type>& vector);
00108 
00116         static Type dot(const CL_Vec2<Type>& vector_1, const CL_Vec2<Type>& vector_2) { return vector_1.x*vector_2.x + vector_1.y*vector_2.y; }
00117 
00124         static CL_Vec2<Type> round(const CL_Vec2<Type>& vector);
00125 
00131         static CL_Vec2<Type> rotate(const CL_Vec2<Type>& vector, const CL_Vec2<Type>& hotspot, const CL_Angle &angle);
00132 
00138         static CL_Pointx<Type> calc_origin(CL_Origin origin, const CL_Sizex<Type> &size);
00139 
00142 
00143 public:
00144 
00150         Type length() const;
00151 
00157         CL_Vec2<Type> &normalize();
00158 
00166         Type dot(const CL_Vec2<Type>& vector) const {return x*vector.x + y*vector.y;}
00167 
00173         CL_Angle angle(const CL_Vec2<Type>& vector) const;
00174 
00180         CL_Angle angle_relative(const CL_Vec2<Type>& vector) const;
00181 
00187         Type distance(const CL_Vec2<Type>& vector) const;
00188 
00194         CL_Vec2<Type> &round();
00195 
00202         CL_Vec2<Type> &rotate(const CL_Vec2<Type>& hotspot, const CL_Angle &angle);
00203 
00209         Type round_value(float value) const;
00210 
00214 
00215 public:
00216         const Type &operator[](unsigned int i) const { return ((Type *) this)[i]; }
00217         Type &operator[](unsigned int i) { return ((Type *) this)[i]; }
00218         operator Type *() { return (Type *) this; }
00219         operator Type * const() const { return (Type * const) this; }
00220 
00222         void operator += (const CL_Vec2<Type>& vector) { x+= vector.x; y+= vector.y; }
00223 
00225         void operator += ( Type value) { x+= value; y+= value; }
00226 
00228         CL_Vec2<Type> operator + (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(vector.x + x, vector.y + y);}
00229 
00231         CL_Vec2<Type> operator + (Type value) const {return CL_Vec2<Type>(value + x, value + y);}
00232 
00234         void operator -= (const CL_Vec2<Type>& vector) { x-= vector.x; y-= vector.y; }
00235 
00237         void operator -= ( Type value) { x-= value; y-= value; }
00238 
00240         CL_Vec2<Type> operator - (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(x - vector.x, y - vector.y);}
00241 
00243         CL_Vec2<Type> operator - (Type value) const {return CL_Vec2<Type>(x - value, y - value);}
00244 
00246         CL_Vec2<Type> operator - () const {return CL_Vec2<Type>(-x , -y);}
00247 
00249         void operator *= (const CL_Vec2<Type>& vector) { x*= vector.x; y*= vector.y; }
00250 
00252         void operator *= ( Type value) { x*= value; y*= value; }
00253 
00255         CL_Vec2<Type> operator * (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(vector.x * x, vector.y * y);}
00256 
00258         CL_Vec2<Type> operator * (Type value) const {return CL_Vec2<Type>(value * x, value * y);}
00259 
00261         void operator /= (const CL_Vec2<Type>& vector) { x/= vector.x; y/= vector.y; }
00262 
00264         void operator /= ( Type value) { x/= value; y/= value; }
00265 
00267         CL_Vec2<Type> operator / (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(x / vector.x, y / vector.y);}
00268 
00270         CL_Vec2<Type> operator / (Type value) const {return CL_Vec2<Type>(x / value, y / value);}
00271 
00273         CL_Vec2<Type> &operator = (const CL_Vec2<Type>& vector) { x = vector.x; y = vector.y; return *this; }
00274 
00276         bool operator == (const CL_Vec2<Type>& vector) const {return ((x == vector.x) && (y == vector.y));}
00277 
00279         bool operator != (const CL_Vec2<Type>& vector) const {return ((x != vector.x) || (y != vector.y));}
00281 };
00282 
00283 template<typename Type>
00284 CL_Vec2<Type> operator * (const CL_Vec2<Type>& v, const CL_Mat2<Type>& matrix)
00285 {
00286         return CL_Vec2<Type>(
00287                 matrix[0*2+0]*v.x + matrix[0*2+1]*v.y,
00288                 matrix[1*2+0]*v.x + matrix[1*2+1]*v.y);
00289 }
00290 
00291 template<typename Type>
00292 CL_Vec2<Type> operator * (const CL_Mat2<Type>& matrix, const CL_Vec2<Type>& v)
00293 {
00294         return CL_Vec2<Type>(
00295                 matrix[0*2+0]*v.x + matrix[1*2+0]*v.y,
00296                 matrix[0*2+1]*v.x + matrix[1*2+1]*v.y);
00297 }
00298 
00300 
00301 template<>
00302 inline CL_Vec2<unsigned char>::CL_Vec2(const CL_Vec2<float> &copy) { x = (unsigned char) floor(copy.x +0.5f); y = (unsigned char) floor(copy.y + 0.5f); }
00303 
00304 template<>
00305 inline CL_Vec2<unsigned char>::CL_Vec2(const CL_Vec2<double> &copy) { x = (unsigned char) floor(copy.x+0.5); y = (unsigned char) floor(copy.y+0.5); }
00306 
00307 template<>
00308 inline CL_Vec2<unsigned char>::CL_Vec2(const CL_Vec2<int> &copy) { x = (unsigned char) copy.x; y = (unsigned char) copy.y; }
00309 
00310 template<>
00311 inline CL_Vec2<char>::CL_Vec2(const CL_Vec2<float> &copy) { x = (char) floor(copy.x +0.5f); y = (char) floor(copy.y + 0.5f); }
00312 
00313 template<>
00314 inline CL_Vec2<char>::CL_Vec2(const CL_Vec2<double> &copy) { x = (char) floor(copy.x+0.5); y = (char) floor(copy.y+0.5); }
00315 
00316 template<>
00317 inline CL_Vec2<char>::CL_Vec2(const CL_Vec2<int> &copy) { x = (char) copy.x; y = (char) copy.y; }
00318 
00319 template<>
00320 inline CL_Vec2<unsigned short>::CL_Vec2(const CL_Vec2<float> &copy) { x = (unsigned short) floor(copy.x +0.5f); y = (unsigned short) floor(copy.y + 0.5f); }
00321 
00322 template<>
00323 inline CL_Vec2<unsigned short>::CL_Vec2(const CL_Vec2<double> &copy) { x = (unsigned short) floor(copy.x+0.5); y = (unsigned short) floor(copy.y+0.5); }
00324 
00325 template<>
00326 inline CL_Vec2<unsigned short>::CL_Vec2(const CL_Vec2<int> &copy) { x = (unsigned short) copy.x; y = (unsigned short) copy.y; }
00327 
00328 template<>
00329 inline CL_Vec2<short>::CL_Vec2(const CL_Vec2<float> &copy) { x = (short) floor(copy.x +0.5f); y = (short) floor(copy.y + 0.5f); }
00330 
00331 template<>
00332 inline CL_Vec2<short>::CL_Vec2(const CL_Vec2<double> &copy) { x = (short) floor(copy.x+0.5); y = (short) floor(copy.y+0.5); }
00333 
00334 template<>
00335 inline CL_Vec2<short>::CL_Vec2(const CL_Vec2<int> &copy) { x = (short) copy.x; y = (short) copy.y; }
00336 
00337 template<>
00338 inline CL_Vec2<int>::CL_Vec2(const CL_Vec2<float> &copy) { x = (int) floor(copy.x +0.5f); y = (int) floor(copy.y + 0.5f); }
00339 
00340 template<>
00341 inline CL_Vec2<int>::CL_Vec2(const CL_Vec2<double> &copy) { x = (int) floor(copy.x+0.5); y = (int) floor(copy.y+0.5); }
00342 
00343 template<>
00344 inline CL_Vec2<int>::CL_Vec2(const CL_Vec2<int> &copy) { x = (int) copy.x; y = (int) copy.y; }
00345 
00346 template<>
00347 inline CL_Vec2<unsigned int>::CL_Vec2(const CL_Vec2<float> &copy) { x = (unsigned int) floor(copy.x +0.5f); y = (unsigned int) floor(copy.y + 0.5f); }
00348 
00349 template<>
00350 inline CL_Vec2<unsigned int>::CL_Vec2(const CL_Vec2<double> &copy) { x = (unsigned int) floor(copy.x+0.5); y = (unsigned int) floor(copy.y+0.5); }
00351 
00352 template<>
00353 inline CL_Vec2<unsigned int>::CL_Vec2(const CL_Vec2<int> &copy) { x = (unsigned int) copy.x; y = (unsigned int) copy.y; }
00354 
00355 template<>
00356 inline CL_Vec2<float>::CL_Vec2(const CL_Vec2<float> &copy) { x = (float) copy.x; y = (float) copy.y; }
00357 
00358 template<>
00359 inline CL_Vec2<float>::CL_Vec2(const CL_Vec2<double> &copy) { x = (float) copy.x; y = (float) copy.y; }
00360 
00361 template<>
00362 inline CL_Vec2<float>::CL_Vec2(const CL_Vec2<int> &copy) { x = (float) copy.x; y = (float) copy.y; }
00363 
00364 template<>
00365 inline CL_Vec2<double>::CL_Vec2(const CL_Vec2<float> &copy) { x = (double) copy.x; y = (double) copy.y; }
00366 
00367 template<>
00368 inline CL_Vec2<double>::CL_Vec2(const CL_Vec2<double> &copy) { x = (double) copy.x; y = (double) copy.y; }
00369 
00370 template<>
00371 inline CL_Vec2<double>::CL_Vec2(const CL_Vec2<int> &copy) { x = (double) copy.x; y = (double) copy.y; }
00372 
00374 
00375 typedef CL_Vec2<unsigned char> CL_Vec2ub;
00376 typedef CL_Vec2<char> CL_Vec2b;
00377 typedef CL_Vec2<unsigned short> CL_Vec2us;
00378 typedef CL_Vec2<short> CL_Vec2s;
00379 typedef CL_Vec2<unsigned int> CL_Vec2ui;
00380 typedef CL_Vec2<int> CL_Vec2i;
00381 typedef CL_Vec2<float> CL_Vec2f;
00382 typedef CL_Vec2<double> CL_Vec2d;
00383