vec2.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2009 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 CL_Pointx<int> &point);
00093         CL_Vec2(const CL_Pointx<float> &point);
00094         CL_Vec2(const CL_Pointx<double> &point);
00095 
00096         CL_Vec2(const CL_Vec2<double> &copy);
00097         CL_Vec2(const CL_Vec2<float> &copy);
00098         CL_Vec2(const CL_Vec2<int> &copy);
00099 
00106         static CL_Vec2<Type> normalize(const CL_Vec2<Type>& vector);
00107 
00115         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; }
00116 
00123         static CL_Vec2<Type> round(const CL_Vec2<Type>& vector);
00124 
00130         static CL_Vec2<Type> rotate(const CL_Vec2<Type>& vector, const CL_Vec2<Type>& hotspot, const CL_Angle &angle);
00131 
00137         static CL_Pointx<Type> calc_origin(CL_Origin origin, const CL_Sizex<Type> &size);
00138 
00141 
00142 public:
00143 
00149         Type length() const;
00150 
00156         CL_Vec2<Type> &normalize();
00157 
00165         Type dot(const CL_Vec2<Type>& vector) const {return x*vector.x + y*vector.y;}
00166 
00172         CL_Angle angle(const CL_Vec2<Type>& vector) const;
00173 
00179         Type distance(const CL_Vec2<Type>& vector) const;
00180 
00186         CL_Vec2<Type> &round();
00187 
00194         CL_Vec2<Type> &rotate(const CL_Vec2<Type>& hotspot, const CL_Angle &angle);
00195 
00201         Type round_value(float value) const;
00202 
00206 
00207 public:
00208         const Type &operator[](unsigned int i) const { return ((Type *) this)[i]; }
00209         Type &operator[](unsigned int i) { return ((Type *) this)[i]; }
00210         operator Type *() { return (Type *) this; }
00211         operator Type * const() const { return (Type * const) this; }
00212 
00214         void operator += (const CL_Vec2<Type>& vector) { x+= vector.x; y+= vector.y; }
00215 
00217         void operator += ( Type value) { x+= value; y+= value; }
00218 
00220         CL_Vec2<Type> operator + (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(vector.x + x, vector.y + y);}
00221 
00223         CL_Vec2<Type> operator + (Type value) const {return CL_Vec2<Type>(value + x, value + y);}
00224 
00226         void operator -= (const CL_Vec2<Type>& vector) { x-= vector.x; y-= vector.y; }
00227 
00229         void operator -= ( Type value) { x-= value; y-= value; }
00230 
00232         CL_Vec2<Type> operator - (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(x - vector.x, y - vector.y);}
00233 
00235         CL_Vec2<Type> operator - (Type value) const {return CL_Vec2<Type>(x - value, y - value);}
00236 
00238         CL_Vec2<Type> operator - () const {return CL_Vec2<Type>(-x , -y);}
00239 
00241         void operator *= (const CL_Vec2<Type>& vector) { x*= vector.x; y*= vector.y; }
00242 
00244         void operator *= ( Type value) { x*= value; y*= value; }
00245 
00247         CL_Vec2<Type> operator * (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(vector.x * x, vector.y * y);}
00248 
00250         CL_Vec2<Type> operator * (Type value) const {return CL_Vec2<Type>(value * x, value * y);}
00251 
00253         void operator /= (const CL_Vec2<Type>& vector) { x/= vector.x; y/= vector.y; }
00254 
00256         void operator /= ( Type value) { x/= value; y/= value; }
00257 
00259         CL_Vec2<Type> operator / (const CL_Vec2<Type>& vector) const {return CL_Vec2<Type>(x / vector.x, y / vector.y);}
00260 
00262         CL_Vec2<Type> operator / (Type value) const {return CL_Vec2<Type>(x / value, y / value);}
00263 
00265         CL_Vec2<Type> &operator = (const CL_Vec2<Type>& vector) { x = vector.x; y = vector.y; return *this; }
00266 
00268         bool operator == (const CL_Vec2<Type>& vector) const {return ((x == vector.x) && (y == vector.y));}
00269 
00271         bool operator != (const CL_Vec2<Type>& vector) const {return ((x != vector.x) || (y != vector.y));}
00273 };
00274 
00275 template<typename Type>
00276 CL_Vec2<Type> operator * (const CL_Vec2<Type>& v, const CL_Mat2<Type>& matrix)
00277 {
00278         return CL_Vec2<Type>(
00279                 matrix[0*2+0]*v.x + matrix[0*2+1]*v.y,
00280                 matrix[1*2+0]*v.x + matrix[1*2+1]*v.y);
00281 }
00282 
00283 template<typename Type>
00284 CL_Vec2<Type> operator * (const CL_Mat2<Type>& matrix, const CL_Vec2<Type>& v)
00285 {
00286         return CL_Vec2<Type>(
00287                 matrix[0*2+0]*v.x + matrix[1*2+0]*v.y,
00288                 matrix[0*2+1]*v.x + matrix[1*2+1]*v.y);
00289 }
00290 
00292 
00293 template<>
00294 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); }
00295 
00296 template<>
00297 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); }
00298 
00299 template<>
00300 inline CL_Vec2<unsigned char>::CL_Vec2(const CL_Vec2<int> &copy) { x = (unsigned char) copy.x; y = (unsigned char) copy.y; }
00301 
00302 template<>
00303 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); }
00304 
00305 template<>
00306 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); }
00307 
00308 template<>
00309 inline CL_Vec2<char>::CL_Vec2(const CL_Vec2<int> &copy) { x = (char) copy.x; y = (char) copy.y; }
00310 
00311 template<>
00312 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); }
00313 
00314 template<>
00315 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); }
00316 
00317 template<>
00318 inline CL_Vec2<unsigned short>::CL_Vec2(const CL_Vec2<int> &copy) { x = (unsigned short) copy.x; y = (unsigned short) copy.y; }
00319 
00320 template<>
00321 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); }
00322 
00323 template<>
00324 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); }
00325 
00326 template<>
00327 inline CL_Vec2<short>::CL_Vec2(const CL_Vec2<int> &copy) { x = (short) copy.x; y = (short) copy.y; }
00328 
00329 template<>
00330 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); }
00331 
00332 template<>
00333 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); }
00334 
00335 template<>
00336 inline CL_Vec2<int>::CL_Vec2(const CL_Vec2<int> &copy) { x = (int) copy.x; y = (int) copy.y; }
00337 
00338 template<>
00339 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); }
00340 
00341 template<>
00342 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); }
00343 
00344 template<>
00345 inline CL_Vec2<unsigned int>::CL_Vec2(const CL_Vec2<int> &copy) { x = (unsigned int) copy.x; y = (unsigned int) copy.y; }
00346 
00347 template<>
00348 inline CL_Vec2<float>::CL_Vec2(const CL_Vec2<float> &copy) { x = (float) copy.x; y = (float) copy.y; }
00349 
00350 template<>
00351 inline CL_Vec2<float>::CL_Vec2(const CL_Vec2<double> &copy) { x = (float) copy.x; y = (float) copy.y; }
00352 
00353 template<>
00354 inline CL_Vec2<float>::CL_Vec2(const CL_Vec2<int> &copy) { x = (float) copy.x; y = (float) copy.y; }
00355 
00356 template<>
00357 inline CL_Vec2<double>::CL_Vec2(const CL_Vec2<float> &copy) { x = (double) copy.x; y = (double) copy.y; }
00358 
00359 template<>
00360 inline CL_Vec2<double>::CL_Vec2(const CL_Vec2<double> &copy) { x = (double) copy.x; y = (double) copy.y; }
00361 
00362 template<>
00363 inline CL_Vec2<double>::CL_Vec2(const CL_Vec2<int> &copy) { x = (double) copy.x; y = (double) copy.y; }
00364 
00366 
00367 typedef CL_Vec2<unsigned char> CL_Vec2ub;
00368 typedef CL_Vec2<char> CL_Vec2b;
00369 typedef CL_Vec2<unsigned short> CL_Vec2us;
00370 typedef CL_Vec2<short> CL_Vec2s;
00371 typedef CL_Vec2<unsigned int> CL_Vec2ui;
00372 typedef CL_Vec2<int> CL_Vec2i;
00373 typedef CL_Vec2<float> CL_Vec2f;
00374 typedef CL_Vec2<double> CL_Vec2d;
00375 

Generated on Thu Dec 3 02:39:32 2009 for ClanLib by  doxygen 1.4.6