ScummVM API documentation
Vector2.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22/*
23 * Copyright (C) 2006-2010 - Frictional Games
24 *
25 * This file is part of HPL1 Engine.
26 */
27
28#ifndef HPL_VECTOR2_H
29#define HPL_VECTOR2_H
30
31#include "hpl1/engine/system/SystemTypes.h"
32
33namespace hpl {
34
35template<class T>
36class cVector2 {
37public:
38 T x, y;
39#if 0
40 union{
41 struct {
42 T x,y;
43 };
44 T v[2];
45 };
46#endif
47
49 // Constructors
51 constexpr cVector2() : x(0), y(0) {
52 }
53
54 constexpr cVector2(T aVal) : x(aVal), y(aVal) {
55 }
56
57 constexpr cVector2(T aX, T aY) : x(aX), y(aY) {
58 }
59
60 constexpr cVector2(cVector2<T> const &aVec) : x(aVec.x), y(aVec.y) {
61 }
62
64 // Copy
66
67 inline cVector2<T> &operator=(const cVector2<T> &aVec) {
68 x = aVec.x;
69 y = aVec.y;
70 return *this;
71 }
72
73 inline cVector2<T> &operator=(const T aVal) {
74 x = aVal;
75 y = aVal;
76 return *this;
77 }
78
80 // Boolean
82
83 inline bool operator==(const cVector2<T> &aVec) const {
84 if (x == aVec.x && y == aVec.y)
85 return true;
86 else
87 return false;
88 }
89
90 inline bool operator!=(const cVector2<T> &aVec) const {
91 if (x == aVec.x && y == aVec.y)
92 return false;
93 else
94 return true;
95 }
96
98 // Vector Arithmetic
100
101 inline cVector2<T> operator+(const cVector2<T> &aVec) const {
102 cVector2<T> vec;
103 vec.x = x + aVec.x;
104 vec.y = y + aVec.y;
105 return vec;
106 }
107
108 inline cVector2<T> operator-(const cVector2<T> &aVec) const {
109 cVector2<T> vec;
110 vec.x = x - aVec.x;
111 vec.y = y - aVec.y;
112 return vec;
113 }
114
115 inline cVector2<T> operator*(const cVector2<T> &aVec) const {
116 cVector2<T> vec;
117 vec.x = x * aVec.x;
118 vec.y = y * aVec.y;
119 return vec;
120 }
121
122 inline cVector2<T> operator/(const cVector2<T> &aVec) const {
123 cVector2<T> vec;
124 vec.x = x / aVec.x;
125 vec.y = y / aVec.y;
126 return vec;
127 }
128
129 inline cVector2<T> &operator-=(const cVector2<T> &aVec) {
130 x -= aVec.x;
131 y -= aVec.y;
132 return *this;
133 }
134
135 inline cVector2<T> &operator+=(const cVector2<T> &aVec) {
136 x += aVec.x;
137 y += aVec.y;
138 return *this;
139 }
140
141 inline cVector2<T> &operator*=(const cVector2<T> &aVec) {
142 x *= aVec.x;
143 y *= aVec.y;
144 return *this;
145 }
146
147 inline cVector2<T> &operator/=(const cVector2<T> &aVec) {
148 x /= aVec.x;
149 y /= aVec.y;
150 return *this;
151 }
152
154 // Real Arithmetic
156
157 inline cVector2<T> operator/(const T &aVal) const {
158 cVector2<T> vec;
159 vec.x = x / aVal;
160 vec.y = y / aVal;
161 return vec;
162 }
163
164 inline cVector2<T> operator*(const T &aVal) const {
165 cVector2<T> vec;
166 vec.x = x * aVal;
167 vec.y = y * aVal;
168 return vec;
169 }
170
171 inline cVector2<T> operator+(const T &aVal) const {
172 cVector2<T> vec;
173 vec.x = x + aVal;
174 vec.y = y + aVal;
175 return vec;
176 }
177
178 cVector2<T> operator-(const T &aVal) const {
179 cVector2<T> vec;
180 vec.x = x - aVal;
181 vec.y = y - aVal;
182 return vec;
183 }
184
186 // Methods
188
189 inline void FromVec(const T *apVec) {
190 x = apVec[0];
191 y = apVec[1];
192 }
193
199 T length = sqrt(x * x + y * y);
200
201 if (length > 1e-08) {
202 T InvLength = 1.0f / length;
203 x *= InvLength;
204 y *= InvLength;
205 }
206
207 return length;
208 }
209
211 // Printing
213
214 tString ToString() const {
215 char buf[512];
216 snprintf(buf, 512, "%f : %f", x, y);
217 tString str = buf;
218 return str;
219 }
220
221 tString ToFileString() const {
222 char buf[512];
223 snprintf(buf, 512, "%g %g", x, y);
224 tString str = buf;
225 return str;
226 }
227};
228
229} // namespace hpl
230
231#endif // HPL_VECTOR2_H
Definition: str.h:59
Definition: Vector2.h:36
T Normalise()
Definition: Vector2.h:198