Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 1 | // -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*- |
2 | |||
3 | // Copyright 2010 Michael Smith, all rights reserved. |
||
4 | |||
5 | // This library is free software; you can redistribute it and / or |
||
6 | // modify it under the terms of the GNU Lesser General Public |
||
7 | // License as published by the Free Software Foundation; either |
||
8 | // version 2.1 of the License, or (at your option) any later version. |
||
9 | |||
10 | // Derived closely from: |
||
11 | /**************************************** |
||
12 | * 2D Vector Classes |
||
13 | * By Bill Perone (billperone@yahoo.com) |
||
14 | * Original: 9-16-2002 |
||
15 | * Revised: 19-11-2003 |
||
16 | * 18-12-2003 |
||
17 | * 06-06-2004 |
||
18 | * |
||
19 | * © 2003, This code is provided "as is" and you can use it freely as long as |
||
20 | * credit is given to Bill Perone in the application it is used in |
||
21 | ****************************************/ |
||
22 | |||
23 | #ifndef VECTOR2_H |
||
24 | #define VECTOR2_H |
||
25 | |||
26 | #include <math.h> |
||
27 | |||
28 | template <typename T> |
||
29 | struct Vector2 |
||
30 | { |
||
31 | T x, y; |
||
32 | |||
33 | // trivial ctor |
||
34 | Vector2<T>() { x = y = 0; } |
||
35 | |||
36 | // setting ctor |
||
37 | Vector2<T>(const T x0, const T y0): x(x0), y(y0) {} |
||
38 | |||
39 | // function call operator |
||
40 | void operator ()(const T x0, const T y0) |
||
41 | { x= x0; y= y0; } |
||
42 | |||
43 | // test for equality |
||
44 | bool operator==(const Vector2<T> &v) |
||
45 | { return (x==v.x && y==v.y); } |
||
46 | |||
47 | // test for inequality |
||
48 | bool operator!=(const Vector2<T> &v) |
||
49 | { return (x!=v.x || y!=v.y); } |
||
50 | |||
51 | // negation |
||
52 | Vector2<T> operator -(void) const |
||
53 | { return Vector2<T>(-x, -y); } |
||
54 | |||
55 | // addition |
||
56 | Vector2<T> operator +(const Vector2<T> &v) const |
||
57 | { return Vector2<T>(x+v.x, y+v.y); } |
||
58 | |||
59 | // subtraction |
||
60 | Vector2<T> operator -(const Vector2<T> &v) const |
||
61 | { return Vector2<T>(x-v.x, y-v.y); } |
||
62 | |||
63 | // uniform scaling |
||
64 | Vector2<T> operator *(const T num) const |
||
65 | { |
||
66 | Vector2<T> temp(*this); |
||
67 | return temp*=num; |
||
68 | } |
||
69 | |||
70 | // uniform scaling |
||
71 | Vector2<T> operator /(const T num) const |
||
72 | { |
||
73 | Vector2<T> temp(*this); |
||
74 | return temp/=num; |
||
75 | } |
||
76 | |||
77 | // addition |
||
78 | Vector2<T> &operator +=(const Vector2<T> &v) |
||
79 | { |
||
80 | x+=v.x; y+=v.y; |
||
81 | return *this; |
||
82 | } |
||
83 | |||
84 | // subtraction |
||
85 | Vector2<T> &operator -=(const Vector2<T> &v) |
||
86 | { |
||
87 | x-=v.x; y-=v.y; |
||
88 | return *this; |
||
89 | } |
||
90 | |||
91 | // uniform scaling |
||
92 | Vector2<T> &operator *=(const T num) |
||
93 | { |
||
94 | x*=num; y*=num; |
||
95 | return *this; |
||
96 | } |
||
97 | |||
98 | // uniform scaling |
||
99 | Vector2<T> &operator /=(const T num) |
||
100 | { |
||
101 | x/=num; y/=num; |
||
102 | return *this; |
||
103 | } |
||
104 | |||
105 | // dot product |
||
106 | T operator *(const Vector2<T> &v) const |
||
107 | { return x*v.x + y*v.y; } |
||
108 | |||
109 | // gets the length of this vector squared |
||
110 | T length_squared() const |
||
111 | { return (T)(*this * *this); } |
||
112 | |||
113 | // gets the length of this vector |
||
114 | T length() const |
||
115 | { return (T)sqrt(*this * *this); } |
||
116 | |||
117 | // normalizes this vector |
||
118 | void normalize() |
||
119 | { *this/=length(); } |
||
120 | |||
121 | // returns the normalized vector |
||
122 | Vector2<T> normalized() const |
||
123 | { return *this/length(); } |
||
124 | |||
125 | // reflects this vector about n |
||
126 | void reflect(const Vector2<T> &n) |
||
127 | { |
||
128 | Vector2<T> orig(*this); |
||
129 | project(n); |
||
130 | *this= *this*2 - orig; |
||
131 | } |
||
132 | |||
133 | // projects this vector onto v |
||
134 | void project(const Vector2<T> &v) |
||
135 | { *this= v * (*this * v)/(v*v); } |
||
136 | |||
137 | // returns this vector projected onto v |
||
138 | Vector2<T> projected(const Vector2<T> &v) |
||
139 | { return v * (*this * v)/(v*v); } |
||
140 | |||
141 | // computes the angle between 2 arbitrary vectors |
||
142 | T angle(const Vector2<T> &v1, const Vector2<T> &v2) |
||
143 | { return (T)acosf((v1*v2) / (v1.length()*v2.length())); } |
||
144 | |||
145 | // computes the angle between 2 normalized arbitrary vectors |
||
146 | T angle_normalized(const Vector2<T> &v1, const Vector2<T> &v2) |
||
147 | { return (T)acosf(v1*v2); } |
||
148 | |||
149 | }; |
||
150 | |||
151 | typedef Vector2<int16_t> Vector2i; |
||
152 | typedef Vector2<uint16_t> Vector2ui; |
||
153 | typedef Vector2<int32_t> Vector2l; |
||
154 | typedef Vector2<uint32_t> Vector2ul; |
||
155 | typedef Vector2<float> Vector2f; |
||
156 | |||
157 | #endif // VECTOR2_H |