libnxter  0.1
Vector.nxc
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  Vector.nxc
4  Copyright (C) 2008 Naba Kumar <naba@gnome.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #ifndef __VECTOR_H_
22 #define __VECTOR_H_
23 
24 #include "Debug.nxc"
25 #include "Matrix.nxc"
26 
43 struct Vector
44 {
45  long x;
46  long y;
47  long theta;
48 };
49 
54 long VectorGet(Vector &a, int i)
55 {
56  if (i == 0) return a.x;
57  if (i == 1) return a.y;
58  return a.theta;
59 }
60 
65 void VectorSet(Vector &a, int i, long value)
66 {
67  if (i == 0) a.x = value;
68  if (i == 1) a.y = value;
69  a.theta = value;
70 }
71 
77 {
78  long distance = (a.x * a.x) + (a.y * a.y);
79  distance = Sqrt(distance);
80  return distance;
81 }
82 
89 {
90  long distance = VectorGetDistance(a);
91  long distanceRatio = ((a.x * 100)/distance);
92  int angle = Acos(distanceRatio);
93  if (angle > 0 && a.y < 0) angle = 360 - angle;
94  return angle;
95 }
96 
101 {
102  long distance = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
103  distance = Sqrt(distance);
104  return distance;
105 }
106 
111 {
112  long distance = VectorGetDistanceVec(a, b);
113  long distanceRatio = (((b.x - a.x) * 100)/distance);
114  int angle = Acos(distanceRatio);
115  if (angle > 0 && (b.y - a.y) < 0) angle = 360 - angle;
116  return angle;
117 }
118 
122 void VectorInit(Vector &a, long x, long y, long theta)
123 {
124  a.x = x;
125  a.y = y;
126  a.theta = theta;
127 }
128 
137 void VectorInitPolar(Vector &a, long distance, int angle, int theta)
138 {
139  a.x = (distance * Cos(angle))/100;
140  a.y = (distance * Sin(angle))/100;
141  a.theta = theta;
142 }
143 
150 void VectorInitBound(Vector &retVec, long x, long y)
151 {
152  retVec.x = x;
153  retVec.y = y;
154  retVec.theta = VectorGetAngle(retVec);
155 }
156 
163 void VectorTranslate(Vector &a, long distance, int angle)
164 {
165  a.x = a.x + ((distance * Cos(angle))/100);
166  a.y = a.y + ((distance * Sin(angle))/100);
167 }
168 
172 void VectorAdd(Vector &a, Vector &b)
173 {
174  a.x += b.x;
175  a.y += b.y;
176  a.theta += b.theta;
177 }
178 
183 {
184  a.x -= b.x;
185  a.y -= b.y;
186  a.theta -= b.theta;
187 }
188 
192 void VectorScale(Vector &a, long S)
193 {
194  a.x *= S;
195  a.y *= S;
196 }
197 
201 void VectorReduce(Vector &a, long S)
202 {
203  a.x /= S;
204  a.y /= S;
205 }
206 
211 void VectorRotate(Vector &vec, long theta)
212 {
213  long newX = (vec.x * Cos(theta) - vec.y * Sin(theta))/100;
214  long newY = (vec.x * Sin(theta) + vec.y * Cos(theta))/100;
215  vec.x = newX;
216  vec.y = newY;
217 }
218 
224 void VectorRotateAtPoint(Vector &vec, long theta, long pivotX, long pivotY)
225 {
226  vec.x -= pivotX;
227  vec.y -= pivotY;
228  long newX = (vec.x * Cos(theta) - vec.y * Sin(theta))/100;
229  long newY = (vec.x * Sin(theta) + vec.y * Cos(theta))/100;
230  vec.x = newX + pivotX;
231  vec.y = newY + pivotY;
232 }
233 
240 void VectorTransform(Vector &vec, Matrix &matrix, Vector &retVec)
241 {
242  ASSERT(matrix.rows == 3 && matrix.cols == 3,
243  "Transformation matrix is not 3x3");
244 
245  retVec.x = MatrixGet(matrix, 0, 0) * vec.x +
246  MatrixGet(matrix, 0, 1) * vec.y +
247  MatrixGet(matrix, 0, 2) * vec.theta;
248  retVec.y = MatrixGet(matrix, 1, 0) * vec.x +
249  MatrixGet(matrix, 1, 1) * vec.y +
250  MatrixGet(matrix, 1, 2) * vec.theta;
251  retVec.theta = MatrixGet(matrix, 2, 0) * vec.x +
252  MatrixGet(matrix, 2, 1) * vec.y +
253  MatrixGet(matrix, 2, 2) * vec.theta;
254 }
255 
256 void VectorPrint(Vector &vec, string name, int waitPeriod)
257 {
258  TextOut(0, LCD_LINE1, name, true);
259  NumOut(0, LCD_LINE2, vec.x, false);
260  NumOut(0, LCD_LINE3, vec.y, false);
261  NumOut(0, LCD_LINE4, vec.theta, false);
262  Wait(waitPeriod);
263 }
264 
265 #endif /* __VECTOR_H_ */
266 
A vector that represents a 2D point by x-y coordinates and a direction angle.
Definition: Vector.nxc:43
long VectorGet(Vector &a, int i)
Gets the vector members with an index number rather than component names. Useful in a loop...
Definition: Vector.nxc:54
The matrix class. Don't access the members directly. Use the macros MIJ() or MI() or the methods prov...
Definition: Matrix.nxc:64
long VectorGetDistanceVec(Vector &a, Vector &b)
Gets the distance between given two vectors.
Definition: Vector.nxc:100
void VectorRotate(Vector &vec, long theta)
Rotates the point in the vector by given angle. The direction component in the vector is not touched...
Definition: Vector.nxc:211
long VectorGetDistance(Vector &a)
Gets the polar distance of the given vector. It's the distance of its x-y coordicate from origin...
Definition: Vector.nxc:76
int VectorGetAngle(Vector &a)
Gets the polar angle of the given vectors. It's the angle of its x-y coordinate from origin...
Definition: Vector.nxc:88
void VectorReduce(Vector &a, long S)
Reduces x and y vector components by the given factor. i.e. a = a / S.
Definition: Vector.nxc:201
Debugging utility macros.
int rows
Definition: Matrix.nxc:67
void VectorSet(Vector &a, int i, long value)
Sets the vector members with an index number rather than component names. Useful in a loop...
Definition: Vector.nxc:65
long theta
Definition: Vector.nxc:47
void VectorInitBound(Vector &retVec, long x, long y)
Initializes the vector into a bound vector with the point given by x and y. Bound vector is a vector ...
Definition: Vector.nxc:150
void VectorSubtract(Vector &a, Vector &b)
Subtracts vector b from a. i.e. a -= b.
Definition: Vector.nxc:182
void VectorInitPolar(Vector &a, long distance, int angle, int theta)
Initializes the vector with given distance and angle polar components.
Definition: Vector.nxc:137
void VectorRotateAtPoint(Vector &vec, long theta, long pivotX, long pivotY)
Rotates the point in the vector by given angle around the pivot point given by pivotX and pivotY...
Definition: Vector.nxc:224
Integer matrix implementation. Provides matrix algebra, cofactor, adjugate and inverse computation...
void VectorTranslate(Vector &a, long distance, int angle)
Translate the vector point to a new point moved the given distance towards the given direction angle...
Definition: Vector.nxc:163
long y
Definition: Vector.nxc:46
void VectorTransform(Vector &vec, Matrix &matrix, Vector &retVec)
Transforms the given vector with the given transformation matrix and returns the transformed vector i...
Definition: Vector.nxc:240
long x
Definition: Vector.nxc:45
void VectorScale(Vector &a, long S)
Scales x and y vector components by the given factor. i.e. a = a * S.
Definition: Vector.nxc:192
int cols
Definition: Matrix.nxc:68
void VectorAdd(Vector &a, Vector &b)
Adds vector b to a. i.e. a += b.
Definition: Vector.nxc:172
long MatrixGet(Matrix &matrix, int row, int col)
Returns the value of element in matrix given by row x col. This is exactly same as macro MIJ()...
Definition: Matrix.nxc:133
long VectorGetAngleVec(Vector &a, Vector &b)
Gets the angle of the line formed by the given two vectors.
Definition: Vector.nxc:110
void VectorInit(Vector &a, long x, long y, long theta)
Initializes the vector with given x, y and theta components.
Definition: Vector.nxc:122