Angel 3.2
A 2D Game Prototyping Engine
Vector2.cpp
1 
2 // Copyright (C) 2008-2013, Shane Liesegang
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of the copyright holder nor the names of any
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "stdafx.h"
31 #include "../Infrastructure/Vector2.h"
32 
33 #include "../Util/MathUtil.h"
34 
35 #include <math.h>
36 
37 Vector2 Vector2::Zero(0.0f, 0.0f);
38 Vector2 Vector2::One(1.0f, 1.0f);
39 Vector2 Vector2::UnitX(1.0f, 0.0f);
40 Vector2 Vector2::UnitY(0.0f, 1.0f);
41 
42 Vector2::Vector2(float x, float y)
43 : X(x)
44 , Y(y)
45 {}
46 
47 Vector2::Vector2(float value)
48 : X(value)
49 , Y(value)
50 {}
51 
53 : X(0)
54 , Y(0)
55 {}
56 
57 Vector2::Vector2(const Vec2i& copy)
58 : X(copy.X)
59 , Y(copy.Y)
60 {}
61 
63 {
64  return sqrt(LengthSquared());
65 }
66 
68 {
69  return (X * X) + (Y * Y);
70 }
71 
72 /*static*/ float Vector2::Distance(const Vector2& value1, const Vector2& value2)
73 {
74  return Vector2(value1 - value2).Length();
75 }
76 
77 /*static*/ float Vector2::DistanceSquared(const Vector2& value1, const Vector2& value2)
78 {
79  return Vector2(value1 - value2).LengthSquared();
80 }
81 
82 /*static*/ float Vector2::Dot(const Vector2& value1, const Vector2& value2)
83 {
84  return (value1.X * value2.X) + (value1.Y * value2.Y);
85 }
86 
87 /*static*/ float Vector2::Cross(const Vector2& value1, const Vector2& value2)
88 {
89  return (value1.X * value2.Y) - (value1.Y * value2.X);
90 }
91 
93 {
94  float len = Length();
95 
96 
97  if( len < 1e-7f )
98  {
99  if( Y > X )
100  *this = UnitY;
101  else
102  *this = UnitX;
103  }
104  else
105  {
106  *this = *this / len;
107  }
108 }
109 
110 /*static*/ Vector2 Vector2::Normalize(const Vector2& value)
111 {
112  Vector2 retVal(value);
113  retVal.Normalize();
114  return retVal;
115 }
116 
117 /*static*/ Vector2 Vector2::Reflect(const Vector2& vector, const Vector2& normal)
118 {
119  return vector - (normal * 2.0f * Dot(vector, normal));
120 }
121 
122 /*static*/ Vector2 Vector2::Min(const Vector2& value1, const Vector2& value2)
123 {
124  return Vector2(MathUtil::Min(value1.X, value2.X), MathUtil::Min(value1.Y, value2.Y));
125 }
126 
127 /*static*/ Vector2 Vector2::Max(const Vector2& value1, const Vector2& value2)
128 {
129  return Vector2(MathUtil::Max(value1.X, value2.X), MathUtil::Max(value1.Y, value2.Y));
130 }
131 
132 /*static*/ Vector2 Vector2::Clamp(const Vector2& value, const Vector2& min, const Vector2& max)
133 {
134  return Vector2(MathUtil::Clamp(value.X, min.X, max.X), MathUtil::Clamp(value.Y, min.Y, max.Y));
135 }
136 
137 /*static*/ Vector2 Vector2::Lerp(const Vector2& value1, const Vector2& value2, float amount)
138 {
139  return Vector2( MathUtil::Lerp( value1.X, value2.X, amount ), MathUtil::Lerp( value1.Y, value2.Y, amount ) );
140 }
141 
142 /*static*/ Vector2 Vector2::Negate(const Vector2& value)
143 {
144  return -value;
145 }
146 
147 /*static*/ Vector2 Vector2::Rotate(const Vector2& value, const float radians)
148 {
149  float c = cos(radians);
150  float s = sin(radians);
151  return Vector2(value.X*c-value.Y*s,value.Y*c+value.X*s);
152 }
153 
154 bool Vector2::operator==(const Vector2 &v) const
155 {
156  return X == v.X && Y == v.Y;
157 }
158 
159 bool Vector2::operator!=(const Vector2 &v) const
160 {
161  return !(*this == v);
162 }
163 
164 Vector2 Vector2::operator-() const
165 {
166  return Vector2::Zero - *this;
167 }
168 
169 Vector2 Vector2::operator-(const Vector2 &v) const
170 {
171  return Vector2(X - v.X, Y - v.Y);
172 }
173 
174 Vector2 Vector2::operator+(const Vector2 &v) const
175 {
176  return Vector2(X + v.X, Y + v.Y);
177 }
178 
179 Vector2 Vector2::operator/(float divider) const
180 {
181  return Vector2(X / divider, Y / divider);
182 }
183 
184 Vector2 Vector2::operator*(float scaleFactor) const
185 {
186  return Vector2(X * scaleFactor, Y * scaleFactor);
187 }
188 
189 Vector2& Vector2::operator+=(const Vector2 &v)
190 {
191  X += v.X;
192  Y += v.Y;
193  return *this;
194 }
195 
196 Vector2& Vector2::operator-=(const Vector2 &v)
197 {
198  X -= v.X;
199  Y -= v.Y;
200  return *this;
201 }
202 
203 Vector2& Vector2::operator*=(float scaleFactor)
204 {
205  X *= scaleFactor;
206  Y *= scaleFactor;
207  return *this;
208 }
209 
210 Vector2& Vector2::operator/=(float scaleFactor)
211 {
212  X /= scaleFactor;
213  Y /= scaleFactor;
214  return *this;
215 }
216 
217 
218 #include <iostream>
219 using namespace std;
220 
221 ostream& operator<<(ostream& stream, const Vector2& out)
222 {
223  return stream << out.X << ", " << out.Y;
224 }