-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAVector.cpp
More file actions
217 lines (163 loc) · 4.68 KB
/
AVector.cpp
File metadata and controls
217 lines (163 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**************************************************************************/
/*!
@file AVector.cpp
@author Stuart Feichtinger
@license MIT (see license.txt)
Simple library for working with vectors based on processing
(processing.org). I got the idea from the awesome book: "The Nature of
Code: Simulating Natural Systems with Processing" by Daniel Shiffman
(http://natureofcode.com). You can download it for free from his
website, but it's definitely worth buying!
(http://www.amazon.com/gp/product/0985930802)
@section HISTORY
v0.0.1 - First release
*/
/**************************************************************************/
#include "AVector.h"
#include "fastTrig.h"
#include <avr/pgmspace.h>
AVector::AVector(int x, int y){
_x = x;
_y = y;
}
int AVector::x(){
return _x;
}
int AVector::y(){
return _y;
}
AVector AVector::add(int x, int y){
AVector returnVector(_x+x, _y+y);
return returnVector;
}
AVector AVector::add(AVector *v){
AVector returnVector(_x + v->x(), _y + v->y());
return returnVector;
}
AVector AVector::sub(int x, int y){
AVector returnVector(_x - x, _y - y);
return returnVector;
}
AVector AVector::sub(AVector *v){
AVector returnVector(_x - v->x(), _y - v->y());
return returnVector;
}
void AVector::set(int x, int y){
_x = x;
_y = y;
}
void AVector::set(AVector *v){
_x = v->x();
_y = v->y();
}
AVector AVector::mult(int val){
AVector returnVector(_x * val, _y * val);
return returnVector;
}
AVector AVector::div(int val){
AVector returnVector(round(_x / val), round(_y / val));
return returnVector;
}
float AVector::distance(int x, int y){
return sqrt(pow((x - _x), 2) + pow((y - _y),2));
}
float AVector::distance(AVector *v){
return distance(v->x(), v->y());
}
float AVector::mag(){
return sqrt(magSq());
}
unsigned long AVector::magSq(){
return (_x*_x + _y*_y);
}
float AVector::dot(int x, int y){
return (_x*x + _y*y);
}
float AVector::dot(AVector *v){
return dot(v->x(), v->y());
}
float AVector::heading(){
return -1*atan2(-1*_y, _x);
}
AVector AVector::setMag(float newMag){
float temp = mag();
AVector returnVector = mult(newMag);
return returnVector.div(temp);
}
AVector AVector::fromAngle(float theta){
AVector returnVector(round(cos(theta)*100), round(sin(theta)*100));
return returnVector;
}
float AVector::angleBetween(AVector *v){
// We get NaN if we pass in a zero vector which can cause problems
// Zero seems like a reasonable angle between a (0,0) vector and something else
if (_x == 0 && _y == 0) return 0;
if (v->x() == 0 && v->y() == 0) return 0;
// This should be a number between -1 and 1, since it's "normalized"
float amt = dot(v) / (mag() * v->mag());
/*
Serial.println(dot(v));
Serial.println(mag());
Serial.println(v->mag());
Serial.println(amt);
Serial.println(acos(amt)*180 / M_PI);
*/
// But if it's not, due to rounding error, then we need to fix it
// http://code.google.com/p/processing/issues/detail?id=340
// Otherwise if outside the range, acos() will return NaN
// http://www.cppreference.com/wiki/c/math/acos
if (amt <= -1) {
return M_PI;
} else if (amt >= 1) {
return 0;
}
return acos(amt);
}
float AVector::angleBetweenFast(AVector *v){
// We get NaN if we pass in a zero vector which can cause problems
// Zero seems like a reasonable angle between a (0,0) vector and something else
if (_x == 0 && _y == 0) return 0;
if (v->x() == 0 && v->y() == 0) return 0;
// This should be a number between -1 and 1, since it's "normalized"
float amt = dot(v) * Q_rsqrt(magSq() * v->magSq());
/*
Serial.println(dot(v));
Serial.println(mag());
Serial.println(v->mag());
Serial.println(amt);
Serial.println(acos(amt)*180 / M_PI);
*/
// But if it's not, due to rounding error, then we need to fix it
// http://code.google.com/p/processing/issues/detail?id=340
// Otherwise if outside the range, acos() will return NaN
// http://www.cppreference.com/wiki/c/math/acos
if (amt <= -1) {
return M_PI;
} else if (amt >= 1) {
return 0;
}
return fast_acos(amt);
}
float AVector::angleBetween(int x, int y){
AVector returnVector(x, y);
return angleBetween(&returnVector);
}
float AVector::angleBetweenFast(int x, int y){
AVector returnVector(x, y);
return angleBetweenFast(&returnVector);
}
AVector AVector::rotate(float theta){
int temp = _x;
AVector returnVector(round(_x * cos(theta) - _y*sin(theta)), round(temp*sin(theta) + _y * cos(theta)));
return returnVector;
}
int AVector::lerp(AVector *v, int tX){
return fast_acos(1.42);
int16_t tY;
int32_t tmp;
tmp = (tX - x());
tmp *= (v->y() - y());
tmp /= (v->x() - x());
tY = y()+tmp;
return(tY);
}