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

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 const Vec2T<T> &operator += (Vec2T<T> &lhs, const Vec2T<T> &rhs)
61 {
62  lhs.x += rhs.x;
63  lhs.y += rhs.y;
64  return lhs;
65 }
66 
67 template <typename T>
68 inline const Vec2T<T> &operator -= (Vec2T<T> &lhs, const Vec2T<T> &rhs)
69 {
70  lhs.x -= rhs.x;
71  lhs.y -= rhs.y;
72  return lhs;
73 }
74 
75 template <typename T>
76 inline const Vec2T<T> &operator *= (Vec2T<T> &lhs, int rhs)
77 {
78  lhs.x *= rhs;
79  lhs.y *= rhs;
80  return lhs;
81 }
82 
83 template <typename T>
84 inline const Vec2T<T> &operator /= (Vec2T<T> &lhs, int rhs)
85 {
86  lhs.x /= rhs;
87  lhs.y /= rhs;
88  return lhs;
89 }
90 
91 template <typename T>
92 inline Vec2T<T> operator + (const Vec2T<T> &lhs, const Vec2T<T> &rhs)
93 {
94  Vec2T<T> res(lhs);
95 
96  res += rhs;
97  return res;
98 }
99 
100 template <typename T>
101 inline Vec2T<T> operator - (const Vec2T<T> &lhs, const Vec2T<T> &rhs)
102 {
103  Vec2T<T> res(lhs);
104 
105  res -= rhs;
106  return res;
107 }
108 
109 template <typename T>
110 inline Vec2T<T> operator * (const Vec2T<T> &lhs, int rhs)
111 {
112  Vec2T<T> res(lhs);
113 
114  res *= rhs;
115  return res;
116 }
117 
118 template <typename T>
119 inline Vec2T<T> operator * (int lhs, const Vec2T<T> &rhs)
120 {
121  Vec2T<T> res(rhs);
122 
123  res *= lhs;
124  return res;
125 }
126 
127 template <typename T>
128 inline Vec2T<T> operator / (const Vec2T<T> &lhs, int rhs)
129 {
130  Vec2T<T> res(lhs);
131 
132  res /= rhs;
133  return res;
134 }
135 
136 template <typename T>
137 inline int SquareDistance(const Vec2T<T> &pos1, const Vec2T<T> &pos2)
138 {
139  const Vec2T<T> diff = pos2 - pos1;
140 
141  return diff.x * diff.x + diff.y * diff.y;
142 }
143 
144 template <typename T>
145 inline int Distance(const Vec2T<T> &pos1, const Vec2T<T> &pos2)
146 {
147  return isqrt(SquareDistance(pos1, pos2));
148 }
149 
155 
157 
158 #endif // !VEC2I_H
Vec2T< T > operator+(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:92
const Vec2T< T > & operator*=(Vec2T< T > &lhs, int rhs)
Definition: vec2i.h:76
long isqrt(long num)
Compute a square root using ints.
Definition: util.cpp:118
T y
Definition: vec2i.h:43
const Vec2T< T > & operator/=(Vec2T< T > &lhs, int rhs)
Definition: vec2i.h:84
Vec2T< int > PixelDiff
Definition: vec2i.h:152
Vec2T< short int > Vec2i
Definition: vec2i.h:150
Vec2T< int > PixelPos
Definition: vec2i.h:151
int Distance(const Vec2T< T > &pos1, const Vec2T< T > &pos2)
Definition: vec2i.h:145
Vec2T< T > operator/(const Vec2T< T > &lhs, int rhs)
Definition: vec2i.h:128
Vec2T< double > PixelPrecise
Definition: vec2i.h:154
Vec2T< T > operator*(const Vec2T< T > &lhs, int rhs)
Definition: vec2i.h:110
Vec2T< int > PixelSize
Definition: vec2i.h:153
bool operator!=(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:54
T x
Definition: vec2i.h:42
bool operator==(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:48
Vec2T< T > operator-(const Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:101
Vec2T(T x, T y)
Definition: vec2i.h:40
const Vec2T< T > & operator+=(Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:60
int SquareDistance(const Vec2T< T > &pos1, const Vec2T< T > &pos2)
Definition: vec2i.h:137
const Vec2T< T > & operator-=(Vec2T< T > &lhs, const Vec2T< T > &rhs)
Definition: vec2i.h:68
Vec2T()
Definition: vec2i.h:39
Definition: vec2i.h:36
(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.