1 /***************************************************************************
2 * Copyright (C) 2004 by Murray Evans *
3 * m.evans@rdg.ac.uk *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 /************************************************************************
21 * 2D vector class - Matlib *
22 * *
23 * Murray Evans 2003 *
24 ************************************************************************/
25
26 #include "Vec2f.h"
27
28
CVec2f(void)29 CVec2f::CVec2f(void)
30 {
31 }
32
CVec2f(float in_X,float in_Y)33 CVec2f::CVec2f(float in_X, float in_Y)
34 {
35 xy_af[0] = in_X;
36 xy_af[1] = in_Y;
37 }
38
~CVec2f(void)39 CVec2f::~CVec2f(void)
40 {
41 }
42
X()43 float CVec2f::X()
44 {
45 return xy_af[0];
46 }
47
Y()48 float CVec2f::Y()
49 {
50 return xy_af[1];
51 }
52
SetX(float in_X)53 void CVec2f::SetX(float in_X)
54 {
55 xy_af[0] = in_X;
56 }
57
SetY(float in_Y)58 void CVec2f::SetY(float in_Y)
59 {
60 xy_af[1] = in_Y;
61 }
62
SetXY(float in_X,float in_Y)63 void CVec2f::SetXY(float in_X, float in_Y)
64 {
65 xy_af[0] = in_X;
66 xy_af[1] = in_Y;
67 }
68
Dot(CVec2f in_Vec)69 float CVec2f::Dot(CVec2f in_Vec)
70 {
71 return ((xy_af[1] * in_Vec.xy_af[1]) + (xy_af[0] * in_Vec.xy_af[0]));
72 }
73
Normalise()74 void CVec2f::Normalise()
75 {
76 // if we have a 0 vector then it's hard to normalise, so just leave it alone
77 if((xy_af[0] != 0) && (xy_af[1] != 0))
78 {
79 float mag_f;
80 mag_f = Magnitude();
81 mag_f = 1/Magnitude();
82 xy_af[0] *= mag_f;
83 xy_af[1] *= mag_f;
84 }
85 }
86
Magnitude()87 float CVec2f::Magnitude()
88 {
89 // magnitude is square root of dot product with self
90 return sqrt(Dot(*this));
91 }
92
MagSquare()93 float CVec2f::MagSquare()
94 {
95 return Dot(*this);
96 }
97
WriteOut()98 string CVec2f::WriteOut()
99 {
100 string out_s;
101 out_s = "|" + Cast<string> (xy_af[0]) + "|\n|" + Cast<string> (xy_af[1]) + "|\n";
102 return out_s;
103 }
104
105 // Overload operator +
operator +(int in_i)106 CVec2f CVec2f::operator+(int in_i)
107 {
108 CVec2f out_v2f;
109 out_v2f.xy_af[0] += in_i;
110 out_v2f.xy_af[1] += in_i;
111 return out_v2f;
112 }
113
operator +(CVec2f in_v2f)114 CVec2f CVec2f::operator+(CVec2f in_v2f)
115 {
116 CVec2f out_v2f;
117 out_v2f.xy_af[0] += in_v2f.xy_af[0];
118 out_v2f.xy_af[1] += in_v2f.xy_af[1];
119 return out_v2f;
120 }
121
122 // Overload operator -
operator -(int in_i)123 CVec2f CVec2f::operator-(int in_i)
124 {
125 CVec2f out_v2f;
126 out_v2f.xy_af[0] -= in_i;
127 out_v2f.xy_af[1] -= in_i;
128 return out_v2f;
129 }
130
operator -(CVec2f in_v2f)131 CVec2f CVec2f::operator-(CVec2f in_v2f)
132 {
133 CVec2f out_v2f;
134 out_v2f.xy_af[0] -= in_v2f.xy_af[0];
135 out_v2f.xy_af[1] -= in_v2f.xy_af[1];
136 return out_v2f;
137 }
138
139 // Overload operator *
operator *(int in_i)140 CVec2f CVec2f::operator*(int in_i)
141 {
142 CVec2f out_v2f;
143 out_v2f.xy_af[0] *= in_i;
144 out_v2f.xy_af[1] *= in_i;
145 return out_v2f;
146 }
147
148 // Overload operator /
operator /(int in_i)149 CVec2f CVec2f::operator/(int in_i)
150 {
151 CVec2f out_v2f;
152 out_v2f.xy_af[0] /= in_i;
153 out_v2f.xy_af[1] /= in_i;
154 return out_v2f;
155 }
156
157