Adafruit 9DOF - LSM303DLHC
Fork of LSM303DLHC by
Vector.h@1:cd027bdb4892, 2016-07-14 (annotated)
- Committer:
- afmiee
- Date:
- Thu Jul 14 20:23:10 2016 +0000
- Revision:
- 1:cd027bdb4892
First Revision of 9DOF with nRF51, Adafruit 9DOF and sparkfun microSD shield.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
afmiee | 1:cd027bdb4892 | 1 | #ifndef VECTOR_H |
afmiee | 1:cd027bdb4892 | 2 | #define VECTOR_H |
afmiee | 1:cd027bdb4892 | 3 | |
afmiee | 1:cd027bdb4892 | 4 | #include "mbed.h" |
afmiee | 1:cd027bdb4892 | 5 | |
afmiee | 1:cd027bdb4892 | 6 | class Vector { |
afmiee | 1:cd027bdb4892 | 7 | public: |
afmiee | 1:cd027bdb4892 | 8 | float x, y, z; |
afmiee | 1:cd027bdb4892 | 9 | |
afmiee | 1:cd027bdb4892 | 10 | Vector(): x(0), y(0), z(0) {} |
afmiee | 1:cd027bdb4892 | 11 | |
afmiee | 1:cd027bdb4892 | 12 | Vector(float x, float y, float z) : x(x), y(y), z(z) {} |
afmiee | 1:cd027bdb4892 | 13 | |
afmiee | 1:cd027bdb4892 | 14 | Vector(const Vector& v) { |
afmiee | 1:cd027bdb4892 | 15 | x = v.x, y = v.y, z = v.z; |
afmiee | 1:cd027bdb4892 | 16 | } |
afmiee | 1:cd027bdb4892 | 17 | |
afmiee | 1:cd027bdb4892 | 18 | Vector product(const Vector& v) { |
afmiee | 1:cd027bdb4892 | 19 | return Vector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); |
afmiee | 1:cd027bdb4892 | 20 | } |
afmiee | 1:cd027bdb4892 | 21 | |
afmiee | 1:cd027bdb4892 | 22 | float dot(const Vector& v) { |
afmiee | 1:cd027bdb4892 | 23 | return x * v.x + y * v.y + z * v.z; |
afmiee | 1:cd027bdb4892 | 24 | } |
afmiee | 1:cd027bdb4892 | 25 | |
afmiee | 1:cd027bdb4892 | 26 | Vector scale(const float c) { |
afmiee | 1:cd027bdb4892 | 27 | return Vector(c * x, c * y, c * z); |
afmiee | 1:cd027bdb4892 | 28 | } |
afmiee | 1:cd027bdb4892 | 29 | |
afmiee | 1:cd027bdb4892 | 30 | float norm() { |
afmiee | 1:cd027bdb4892 | 31 | return sqrt(x * x + y * y + z * z); |
afmiee | 1:cd027bdb4892 | 32 | } |
afmiee | 1:cd027bdb4892 | 33 | |
afmiee | 1:cd027bdb4892 | 34 | Vector operator*(const Vector& v) { |
afmiee | 1:cd027bdb4892 | 35 | return product(v); |
afmiee | 1:cd027bdb4892 | 36 | } |
afmiee | 1:cd027bdb4892 | 37 | |
afmiee | 1:cd027bdb4892 | 38 | Vector operator /=(float c) { |
afmiee | 1:cd027bdb4892 | 39 | return *this = scale(1 / c); |
afmiee | 1:cd027bdb4892 | 40 | } |
afmiee | 1:cd027bdb4892 | 41 | |
afmiee | 1:cd027bdb4892 | 42 | Vector operator *=(float c) { |
afmiee | 1:cd027bdb4892 | 43 | return *this = scale(c); |
afmiee | 1:cd027bdb4892 | 44 | } |
afmiee | 1:cd027bdb4892 | 45 | }; |
afmiee | 1:cd027bdb4892 | 46 | |
afmiee | 1:cd027bdb4892 | 47 | #endif |