1// tVector2.h 
2// 
3// A 2D vector class with the expected member functions and overloads. Backs off of the tVec2 POD type and the 
4// tLinearAlgebra library functions. 
5// 
6// Copyright (c) 2004-2006, 2015, 2017 Tristan Grimmer. 
7// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby 
8// granted, provided that the above copyright notice and this permission notice appear in all copies. 
9// 
10// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 
11// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 
12// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
13// AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 
14// PERFORMANCE OF THIS SOFTWARE. 
15 
16#pragma once 
17#include "Math/tLinearAlgebra.h" 
18 
19 
20namespace tMath 
21
22 
23 
24// This is the type you should use most of the time for 2D vectors. Extends the POD type with operators etc. Note the 
25// intentional empty default constructor. 
26struct tVector2 : public tVec2 
27
28 tVector2() { } 
29 tVector2(const tVec2& v) { tSet(*this, v); } 
30 tVector2(const tVec3& v) /* Discards v.z. */ { tSet(*this, v); } 
31 tVector2(const tVec4& v) /* Discards v.z and v.w. */ { tSet(*this, v); } 
32 tVector2(float xy) /* Sets both x and y to xy. */ { tSet(*this, xy); } 
33 tVector2(float x, float y) { tSet(*this, x, y); } 
34 tVector2(const float* a) { tSet(*this, a); } 
35 const tVec2& Pod() const { return *this; } 
36 
37 void Set(const tVec2& v) { tSet(*this, v); } 
38 void Set(const tVec3& v) { tSet(*this, v); } 
39 void Set(const tVec4& v) { tSet(*this, v); } 
40 void Set(float xy) { tSet(*this, xy); } 
41 void Set(float x, float y) { tSet(*this, x, y); } 
42 void Set(const float* a) { tSet(*this, a); } 
43 void Get(float& x, float& y) const { tGet(x, y, *this); } 
44 void Get(float* a) const { tGet(a, *this); } 
45 
46 void Zero() { tZero(*this); } 
47 void Zero(tComponents c) { tZero(*this, c); } 
48 bool IsZero() const { return tIsZero(*this); } 
49 bool IsZero(tComponents c) const { return tIsZero(*this, c); } 
50 bool ApproxEqual(const tVec2& v, float e = Epsilon) const { return tApproxEqual(*this, v, e); } 
51 bool ApproxEqual(const tVec2& v, tComponents c, float e = Epsilon) const { return tApproxEqual(*this, v, c, e); } 
52 
53 float LengthSq() const { return tLengthSq(*this); } 
54 float Length() const { return tLength(*this); } 
55 float LengthFast() const { return tLengthFast(*this); } 
56 
57 void Normalize() { tNormalize(*this); } 
58 float NormalizeGetLength() /* Returns pre-normalized length. */ { return tNormalizeGetLength(*this); } 
59 bool NormalizeSafe() /* Returns success. */ { return tNormalizeSafe(*this); } 
60 float NormalizeSafeGetLength() /* Returns pre-normalized length. 0.0f if no go. */ { return tNormalizeSafeGetLength(*this); } 
61 void NormalizeScale(float s) /* Normalize then scale. Resizes the vector. */ { tNormalizeScale(*this, s); } 
62 bool NormalizeScaleSafe(float s) { return tNormalizeScaleSafe(*this, s); } 
63 void NormalizeFast() { tNormalizeFast(*this); } 
64 bool NormalizeSafeFast() { return tNormalizeSafeFast(*this); } 
65 
66 void Lerp(const tVec2& a, const tVec2& b, float t) /* Also extrapolates. See tLerp comments. */ { tLerp(*this, a, b, t); } 
67 inline static int GetNumComponents() { return 2; } 
68 
69 // Friend is used for commutative binary operators where one of the operators is a built-in type. This essentially 
70 // puts these operators outside of the class where they belong. If the other operand type is user-defined, the 
71 // operator should be completely outside as there would be ambiguity as to which one owned the friend declaration. 
72 tVector2& operator=(const tVec2& v) { tSet(*this, v); return *this; } 
73 tVector2& operator=(const tVec3& v) { tSet(*this, v); return *this; } 
74 tVector2& operator=(const tVec4& v) { tSet(*this, v); return *this; } 
75 
76 // Addition is not defined for types other than the same type as the object itself, so no friend needed. 
77 tVector2& operator+=(const tVec2& a) { tAdd(*this, a); return *this; } 
78 const tVector2 operator+(const tVec2& a) const { tVector2 d; tAdd(d, *this, a); return d; } 
79 
80 tVector2& operator-=(const tVec2& a) { tSub(*this, a); return *this; } 
81 const tVector2 operator-(const tVec2& a) const { tVector2 d; tSub(d, *this, a); return d; } 
82 const tVector2 operator-() const { tVector2 d; tNeg(d, *this); return d; } 
83 
84 tVector2& operator*=(float a) { tMul(*this, a); return *this; } 
85 inline friend const tVector2 operator*(const tVec2& a, float b) { tVector2 d; tMul(d, a, b); return d; } 
86 inline friend const tVector2 operator*(float a, const tVec2& b) { tVector2 d; tMul(d, a, b); return d; } 
87 float operator*(const tVec2& a) const /* Dot (inner) product. Same as Transpose(this) * a. */ { return tDot(*this, a); } 
88 
89 // Divide is not commutative so no need for friend. 
90 tVector2& operator/=(float a) { tDiv(*this, a); return *this; } 
91 const tVector2 operator/(float a) const { tVector2 d; tDiv(d, *this, a); return d; } 
92 
93 bool operator==(const tVec2& a) const { return tEqual(*this, a); } 
94 bool operator!=(const tVec2& a) const { return tNotEqual(*this, a); } 
95 operator const float*() { return E; } 
96 operator const float*() const { return E; } 
97 float& operator[](int i) { return E[i]; } 
98 float operator[](int i) const { return E[i]; } 
99 
100 const static tVector2 zero; // Zero vector (0, 0). 
101 const static tVector2 i; // Basis vector (1, 0). 
102 const static tVector2 j; // Basis vector (0, 1). 
103}; 
104 
105 
106
107