Hiroshi Yamaguchi / LSM303DLH

Dependents:   LSM303DLH_Example Arch_Test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Vector.h Source File

Vector.h

00001 #ifndef VECTOR_H
00002 #define VECTOR_H
00003 
00004 #include "mbed.h"
00005 
00006 class Vector {
00007 public:
00008     float x, y, z;
00009     
00010     Vector(): x(0), y(0), z(0) {}
00011     
00012     Vector(float x, float y, float z) : x(x), y(y), z(z) {}
00013     
00014     Vector(const Vector& v) {
00015         x = v.x, y = v.y, z = v.z;
00016     }
00017 
00018     Vector product(const Vector& v) {
00019         return Vector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
00020     }
00021 
00022     float dot(const Vector& v) {
00023         return x * v.x + y * v.y + z * v.z;
00024     }
00025 
00026     Vector scale(const float c) {
00027         return Vector(c * x, c * y, c * z);
00028     }
00029 
00030     float norm() {
00031         return sqrt(x * x + y * y + z * z);
00032     }
00033 
00034     Vector operator*(const Vector& v) {
00035         return product(v);
00036     }
00037 
00038     Vector operator /=(float c) {
00039         return *this = scale(1 / c);
00040     }
00041 
00042     Vector operator *=(float c) {
00043         return *this = scale(c);
00044     }
00045 };
00046 
00047 #endif