-
Notifications
You must be signed in to change notification settings - Fork 2
/
PVector.cpp
60 lines (53 loc) · 978 Bytes
/
PVector.cpp
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
#ifndef PVECTOR_H
#define PVECTOR_H
#include "Arduino.h"
class PVector {
public:
float x, y;
PVector() {};
PVector(float _x, float _y) {
x = _x;
y = _y;
};
void add(PVector v) {
y = y + v.y;
x = x + v.x;
};
void mult(float n) {
x = x * n;
y = y * n;
};
void sub(PVector v) {
x = x - v.x;
y = y - v.y;
}
float mag() {
return sqrt(x * x + y * y);
}
void normalize() {
float m = mag();
if (m != 0) {
div(m);
}
}
void div(float n) {
x /= n;
y /= n;
}
float magSq() {
return (x * x + y * y);
}
void limit(float max) {
if (magSq() > max*max) {
normalize();
mult(max);
}
}
static PVector sub(PVector v1, PVector v2) {
return PVector(v1.x - v2.x, v1.y - v2.y);
}
static PVector createVector(float _x, float _y) {
return PVector(_x, _y);
};
};
#endif