_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

vec2i.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 2010 by Joris Dauphin
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29 
30 #ifndef VEC2I_H
31 #define VEC2I_H
32 
34 
35 template <typename T>
36 class Vec2T
37 {
38 public:
39  Vec2T() : x(0), y(0) {}
40  Vec2T(T x, T y) : x(x), y(y) {}
41 public:
42  T x;
43  T y;
44 };
45 
46 
47 template <typename T>
48 inline bool operator == (const Vec2T<T> &lhs, const Vec2T<T> &rhs)
49 {
50  return lhs.x == rhs.x && lhs.y == rhs.y;
51 }
52 
53 template <typename T>
54 inline bool operator != (const Vec2T<T> &lhs, const Vec2T<T> &rhs)
55 {
56  return !(lhs == rhs);
57 }
58 
59 template <typename T>
60 inline bool operator < (const Vec2T<T> &lhs, const Vec2T<T> &rhs)
61 {
62  return lhs.x < rhs.x && lhs.y < rhs.y;
63 }
64 
65 template <typename T>
66 inline bool operator > (const Vec2T<T> &lhs, const Vec2T<T> &rhs)
67 {
68  return lhs.x > rhs.x && lhs.y > rhs.y;
69 }
70 
71 template <typename T>
72 inline const Vec2T<T> &operator += (Vec2T<T> &lhs, const Vec2T<T> &rhs)
73 {
74  lhs.x += rhs.x;
75  lhs.y += rhs.y;
76  return lhs;
77 }
78 
79 template <typename T>
80 inline const Vec2T<T> &operator -= (Vec2T<T> &lhs, const Vec2T<T> &rhs)
81 {
82  lhs.x -= rhs.x;
83  lhs.y -= rhs.y;
84  return lhs;
85 }
86 
87 template <typename T>
88 inline const Vec2T<T> &operator *= (Vec2T<T> &lhs, int rhs)
89 {
90  lhs.x *= rhs;
91  lhs.y *= rhs;
92  return lhs;
93 }
94 
95 template <typename T>
96 inline const Vec2T<T> &operator /= (Vec2T<T> &lhs, int rhs)
97 {
98  lhs.x /= rhs;
99  lhs.y /= rhs;
100  return lhs;
101 }
102 
103 template <typename T>
104 inline Vec2T<T> operator + (const Vec2T<T> &lhs, const Vec2T<T> &rhs)
105 {
106  Vec2T<T> res(lhs);
107 
108  res += rhs;
109  return res;
110 }
111 
112 template <typename T>
113 inline Vec2T<T> operator - (const Vec2T<T> &lhs, const Vec2T<T> &rhs)
114 {
115  Vec2T<T> res(lhs);
116 
117  res -= rhs;
118  return res;
119 }
120 
121 template <typename T>
122 inline Vec2T<T> operator * (const Vec2T<T> &lhs, int rhs)
123 {
124  Vec2T<T> res(lhs);
125 
126  res *= rhs;
127  return res;
128 }
129 
130 template <typename T>
131 inline Vec2T<T> operator * (int lhs, const Vec2T<T> &rhs)
132 {
133  Vec2T<T> res(rhs);
134 
135  res *= lhs;
136  return res;
137 }
138 
139 template <typename T>
140 inline Vec2T<T> operator / (const Vec2T<T> &lhs, int rhs)
141 {
142  Vec2T<T> res(lhs);
143 
144  res /= rhs;
145  return res;
146 }
147 
148 template <typename T>
149 inline int SquareDistance(const Vec2T<T> &pos1, const Vec2T<T> &pos2)
150 {
151  const Vec2T<T> diff = pos2 - pos1;
152 
153  return diff.x * diff.x + diff.y * diff.y;
154 }
155 
156 template <typename T>
157 inline int Distance(const Vec2T<T> &pos1, const Vec2T<T> &pos2)
158 {
159  return isqrt(SquareDistance(pos1, pos2));
160 }
161 
167 
169 
170 #endif // !VEC2I_H
Vec2T::Vec2T
Vec2T()
Definition: vec2i.h:39
operator*=
const Vec2T< T > & operator*=(Vec2T< T > &lhs, int rhs)
Definition: vec2i.h:88
operator*
Vec2T< T > operator*(const Vec2T< T > &lhs, int rhs)
Definition: vec2i.h:122
operator<
bool operator<(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:60
Vec2T::y
T y
Definition: vec2i.h:43
operator+=
const Vec2T< T > & operator+=(Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:72
PixelDiff
Vec2T< int > PixelDiff
Definition: vec2i.h:164
PixelPos
Vec2T< int > PixelPos
Definition: vec2i.h:163
Vec2T
Definition: vec2i.h:36
operator>
bool operator>(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:66
operator/
Vec2T< T > operator/(const Vec2T< T > &lhs, int rhs)
Definition: vec2i.h:140
Distance
int Distance(const Vec2T< T > &pos1, const Vec2T< T > &pos2)
Definition: vec2i.h:157
operator-
Vec2T< T > operator-(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:113
Vec2T::Vec2T
Vec2T(T x, T y)
Definition: vec2i.h:40
operator-=
const Vec2T< T > & operator-=(Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:80
operator!=
bool operator!=(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:54
operator+
Vec2T< T > operator+(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:104
isqrt
long isqrt(long num)
Compute a square root using ints.
Definition: util.cpp:133
Vec2T::x
T x
Definition: vec2i.h:42
Vec2i
Vec2T< short int > Vec2i
Definition: vec2i.h:162
PixelPrecise
Vec2T< double > PixelPrecise
Definition: vec2i.h:166
SquareDistance
int SquareDistance(const Vec2T< T > &pos1, const Vec2T< T > &pos2)
Definition: vec2i.h:149
PixelSize
Vec2T< int > PixelSize
Definition: vec2i.h:165
operator/=
const Vec2T< T > & operator/=(Vec2T< T > &lhs, int rhs)
Definition: vec2i.h:96
operator==
bool operator==(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:48
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.