1 // Copyright (C) 2009 Ben Asselstine
2 // This program is free software; you can redistribute it and/or modify
3 // it under the terms of the GNU General Public License as published by
4 // the Free Software Foundation; either version 3 of the License, or
5 // (at your option) any later version.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU Library General Public License for more details.
12 // You should have received a copy of the GNU General Public License
13 // along with this program; if not, write to the Free Software
14 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 //! A simple 2d point structure.
25 * Implements a lot of overloaded operators to ease calculations.
26 * x is the axis in the left/right direction.
27 * y is the axis in the up/down direction.
29 extern int max_vector_width;
35 static void setMaximumWidth(int width) {max_vector_width = width;};
37 template <typename OT>
38 Vector(OT other_point_type): x(other_point_type.x), y(other_point_type.y) { }
39 Vector(T px, T py): x(px), y(py) { }
41 // conversion from another compatible vector type
42 template <typename OT>
43 Vector(const Vector<OT> &v)
48 template <typename OT>
49 Vector<T> operator +=(Vector<OT> other)
56 template <typename OT>
57 Vector<T> operator -=(Vector<OT> other)
64 template <typename OT>
65 Vector<T> operator *=(OT number)
72 template <typename OT>
73 Vector<T> operator /=(OT number)
80 Vector<T> operator -()
82 return Vector<T>(-x, -y);
85 //size_t operator()(const Vector<T>&v) const
87 //size_t size = v.x * max_vector_width + v.y;
90 T toIndex() {return y*max_vector_width+x; }
94 inline Vector<T> operator +(Vector<T> lhs, Vector<T> rhs)
96 return Vector<T>(lhs.x + rhs.x, lhs.y + rhs.y);
100 inline Vector<T> operator -(Vector<T> lhs, Vector<T> rhs)
102 return Vector<T>(lhs.x - rhs.x, lhs.y - rhs.y);
105 template <typename T, typename OT>
106 inline Vector<T> operator *(Vector<T> v, OT number)
108 return Vector<T>(v.x * number, v.y * number);
111 template <typename T, typename OT>
112 inline Vector<T> operator *(OT number, Vector<T> v)
114 return Vector<T>(v.x * number, v.y * number);
117 template <typename T, typename OT>
118 inline Vector<T> operator %(Vector<T> v, OT number)
120 return Vector<T>(v.x % number, v.y % number);
123 template <typename T, typename OT>
124 inline Vector<T> operator /(Vector<T> v, OT number)
126 return Vector<T>(v.x / number, v.y / number);
129 template <typename T>
130 inline bool operator !=(Vector<T> lhs, Vector<T> rhs)
132 return !(lhs == rhs);
135 template <typename T>
136 inline bool operator ==(Vector<T> lhs, Vector<T> rhs)
138 return lhs.x == rhs.x && lhs.y == rhs.y;
141 template <typename T>
142 inline bool operator <(Vector<T> lhs, Vector<T> rhs)
144 T l = lhs.y * max_vector_width + lhs.x;
145 T r = rhs.y * max_vector_width + rhs.x;
150 inline bool operator ==<double>(Vector<double> lhs, Vector<double> rhs)
152 return std::abs(lhs.x - rhs.x) < 0.001 && std::abs(lhs.y - rhs.y) < 0.001;
156 inline bool operator ==<float>(Vector<float> lhs, Vector<float> rhs)
158 return std::abs(lhs.x - rhs.x) < 0.001 && std::abs(lhs.y - rhs.y) < 0.001;
163 template <typename T>
164 inline Vector<T> make_vector(T x, T y)
166 return Vector<T>(x, y);
170 template <typename T>
171 inline T dist(Vector<T> v1, Vector<T> v2)
173 return std::sqrt((v2.x - v1.x) * (v2.x - v1.x)
174 + (v2.y - v1.y) * (v2.y - v1.y));
177 // specialization to make int case work smoother
179 inline int dist(Vector<int> v1, Vector<int> v2)
181 return static_cast<int>(std::sqrt(float((v2.x - v1.x) * (v2.x - v1.x)
182 + (v2.y - v1.y) * (v2.y - v1.y))));
186 template <typename T>
187 inline T length(Vector<T> v)
189 return static_cast<T>(std::sqrt(v.x * v.x + v.y * v.y));
193 template <typename T>
194 inline Vector<T> clip(Vector<T> lower, Vector<T> val, Vector<T> upper)
200 else if (val.x < lower.x)
207 else if (val.y < lower.y)
216 template <typename T>
217 inline Vector<T> round(Vector<T> v)
219 return Vector<T>(round(v.x), round(v.y));
223 // type cast template, e.g. vector_cast<int>(some_float_vector)
224 template <typename Dest, typename Src>
225 inline Vector<Dest> vector_cast(Vector<Src> v)
227 return Vector<Dest>(static_cast<Dest>(v.x), static_cast<Dest>(v.y));