share
Dependencies: RotationMat_ SDFileSystem math mbed trigonometric_fanc
Vector3D.h@0:28d0b65a60fe, 2016-02-01 (annotated)
- Committer:
- shuhei2306
- Date:
- Mon Feb 01 13:53:13 2016 +0000
- Revision:
- 0:28d0b65a60fe
- Child:
- 2:b09caaa3de96
first_commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shuhei2306 | 0:28d0b65a60fe | 1 | #include "mbed.h" |
shuhei2306 | 0:28d0b65a60fe | 2 | |
shuhei2306 | 0:28d0b65a60fe | 3 | class Vector3D{ |
shuhei2306 | 0:28d0b65a60fe | 4 | public: |
shuhei2306 | 0:28d0b65a60fe | 5 | //メンバ変数 |
shuhei2306 | 0:28d0b65a60fe | 6 | float x; |
shuhei2306 | 0:28d0b65a60fe | 7 | float y; |
shuhei2306 | 0:28d0b65a60fe | 8 | float z; |
shuhei2306 | 0:28d0b65a60fe | 9 | //コンストラクタ |
shuhei2306 | 0:28d0b65a60fe | 10 | Vector3D(); |
shuhei2306 | 0:28d0b65a60fe | 11 | Vector3D(float x,float y,float z); |
shuhei2306 | 0:28d0b65a60fe | 12 | //代入演算子 |
shuhei2306 | 0:28d0b65a60fe | 13 | Vector3D& Vector3D::operator=(const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 14 | //単項演算子 |
shuhei2306 | 0:28d0b65a60fe | 15 | Vector3D& operator+=(const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 16 | Vector3D& operator-=(const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 17 | Vector3D& operator*=(float k); |
shuhei2306 | 0:28d0b65a60fe | 18 | Vector3D& operator/=(float k); |
shuhei2306 | 0:28d0b65a60fe | 19 | Vector3D operator+()const; |
shuhei2306 | 0:28d0b65a60fe | 20 | Vector3D operator-()const; |
shuhei2306 | 0:28d0b65a60fe | 21 | //添え字演算子 |
shuhei2306 | 0:28d0b65a60fe | 22 | float& operator[](int i); |
shuhei2306 | 0:28d0b65a60fe | 23 | //比較演算子 |
shuhei2306 | 0:28d0b65a60fe | 24 | bool operator==(const Vector3D& v ) const; |
shuhei2306 | 0:28d0b65a60fe | 25 | bool operator!=(const Vector3D& v ) const; |
shuhei2306 | 0:28d0b65a60fe | 26 | //べクトルの長さ |
shuhei2306 | 0:28d0b65a60fe | 27 | float norm()const; |
shuhei2306 | 0:28d0b65a60fe | 28 | //正規化 |
shuhei2306 | 0:28d0b65a60fe | 29 | void normalize(); |
shuhei2306 | 0:28d0b65a60fe | 30 | }; |
shuhei2306 | 0:28d0b65a60fe | 31 | //ベクトル演算 |
shuhei2306 | 0:28d0b65a60fe | 32 | //Vector3D+Vector3D |
shuhei2306 | 0:28d0b65a60fe | 33 | Vector3D operator+(const Vector3D& u,const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 34 | //Vector3D-Vector3D |
shuhei2306 | 0:28d0b65a60fe | 35 | Vector3D operator-(const Vector3D& u,const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 36 | //float*Vector3D |
shuhei2306 | 0:28d0b65a60fe | 37 | Vector3D operator*(float k,const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 38 | //Vector3D*float |
shuhei2306 | 0:28d0b65a60fe | 39 | Vector3D operator*(const Vector3D& v,float k); |
shuhei2306 | 0:28d0b65a60fe | 40 | //Vector3D/float |
shuhei2306 | 0:28d0b65a60fe | 41 | Vector3D operator/(const Vector3D& v,float k); |
shuhei2306 | 0:28d0b65a60fe | 42 | //内積 Vector3D*Vector3D |
shuhei2306 | 0:28d0b65a60fe | 43 | float operator*(const Vector3D& u,const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 44 | //外積 Vector3D%Vector3D |
shuhei2306 | 0:28d0b65a60fe | 45 | Vector3D operator%(const Vector3D& u,const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 46 | //2つのベクトルのなす角度 |
shuhei2306 | 0:28d0b65a60fe | 47 | float angle(const Vector3D& u,const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 48 | //出力 |
shuhei2306 | 0:28d0b65a60fe | 49 | #include <iostream> |
shuhei2306 | 0:28d0b65a60fe | 50 | inline std::ostream& operator<<(std::ostream& s, const Vector3D& v); |
shuhei2306 | 0:28d0b65a60fe | 51 | //*----------------------メンバ関数の実装--------------------------*// |
shuhei2306 | 0:28d0b65a60fe | 52 | #include <cmath> |
shuhei2306 | 0:28d0b65a60fe | 53 | //コンストラクタ |
shuhei2306 | 0:28d0b65a60fe | 54 | inline Vector3D::Vector3D(){ x = y = z = 0; } |
shuhei2306 | 0:28d0b65a60fe | 55 | inline Vector3D::Vector3D(float x,float y,float z){ |
shuhei2306 | 0:28d0b65a60fe | 56 | this->x=x; this->y=y; this->z=z; |
shuhei2306 | 0:28d0b65a60fe | 57 | } |
shuhei2306 | 0:28d0b65a60fe | 58 | //代入演算子 |
shuhei2306 | 0:28d0b65a60fe | 59 | inline Vector3D& Vector3D::operator=(const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 60 | this->x=v.x; this->y=v.y; this->z=v.z; |
shuhei2306 | 0:28d0b65a60fe | 61 | return *this; |
shuhei2306 | 0:28d0b65a60fe | 62 | } |
shuhei2306 | 0:28d0b65a60fe | 63 | //単項演算子 |
shuhei2306 | 0:28d0b65a60fe | 64 | inline Vector3D& Vector3D::operator+=(const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 65 | this->x += v.x; this->y += v.y; this->z += v.z; |
shuhei2306 | 0:28d0b65a60fe | 66 | return *this; |
shuhei2306 | 0:28d0b65a60fe | 67 | } |
shuhei2306 | 0:28d0b65a60fe | 68 | inline Vector3D& Vector3D::operator-=(const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 69 | this->x -= v.x; this->y -= v.y; this->z -= v.z; |
shuhei2306 | 0:28d0b65a60fe | 70 | return *this; |
shuhei2306 | 0:28d0b65a60fe | 71 | } |
shuhei2306 | 0:28d0b65a60fe | 72 | inline Vector3D& Vector3D::operator*=(float k){ |
shuhei2306 | 0:28d0b65a60fe | 73 | this->x *= k; this->y *= k; this->z *= k; |
shuhei2306 | 0:28d0b65a60fe | 74 | return *this; |
shuhei2306 | 0:28d0b65a60fe | 75 | } |
shuhei2306 | 0:28d0b65a60fe | 76 | inline Vector3D& Vector3D::operator/=(float k){ |
shuhei2306 | 0:28d0b65a60fe | 77 | this->x /= k; this->y /= k; this->z /= k; |
shuhei2306 | 0:28d0b65a60fe | 78 | return *this; |
shuhei2306 | 0:28d0b65a60fe | 79 | } |
shuhei2306 | 0:28d0b65a60fe | 80 | inline Vector3D Vector3D::operator+()const{ //+Vector3D |
shuhei2306 | 0:28d0b65a60fe | 81 | return *this; |
shuhei2306 | 0:28d0b65a60fe | 82 | } |
shuhei2306 | 0:28d0b65a60fe | 83 | inline Vector3D Vector3D::operator-()const{ //-Vector3D |
shuhei2306 | 0:28d0b65a60fe | 84 | return Vector3D(-x,-y,-z); |
shuhei2306 | 0:28d0b65a60fe | 85 | } |
shuhei2306 | 0:28d0b65a60fe | 86 | //添え字演算子 |
shuhei2306 | 0:28d0b65a60fe | 87 | inline float& Vector3D::operator[](int i){ |
shuhei2306 | 0:28d0b65a60fe | 88 | if(i == 0){ |
shuhei2306 | 0:28d0b65a60fe | 89 | return x; |
shuhei2306 | 0:28d0b65a60fe | 90 | } |
shuhei2306 | 0:28d0b65a60fe | 91 | else if(i == 1){ |
shuhei2306 | 0:28d0b65a60fe | 92 | return y; |
shuhei2306 | 0:28d0b65a60fe | 93 | } |
shuhei2306 | 0:28d0b65a60fe | 94 | else if(i == 2){ |
shuhei2306 | 0:28d0b65a60fe | 95 | return z; |
shuhei2306 | 0:28d0b65a60fe | 96 | } |
shuhei2306 | 0:28d0b65a60fe | 97 | else{ |
shuhei2306 | 0:28d0b65a60fe | 98 | return x; |
shuhei2306 | 0:28d0b65a60fe | 99 | } |
shuhei2306 | 0:28d0b65a60fe | 100 | } |
shuhei2306 | 0:28d0b65a60fe | 101 | //比較演算子 |
shuhei2306 | 0:28d0b65a60fe | 102 | inline bool Vector3D::operator==(const Vector3D& v ) const{ |
shuhei2306 | 0:28d0b65a60fe | 103 | return (x == v.x) && (y == v.y) && (z == v.z); |
shuhei2306 | 0:28d0b65a60fe | 104 | } |
shuhei2306 | 0:28d0b65a60fe | 105 | inline bool Vector3D::operator!=(const Vector3D& v ) const{ |
shuhei2306 | 0:28d0b65a60fe | 106 | return !(*this == v); |
shuhei2306 | 0:28d0b65a60fe | 107 | } |
shuhei2306 | 0:28d0b65a60fe | 108 | //べクトルの長さ |
shuhei2306 | 0:28d0b65a60fe | 109 | inline float Vector3D::norm()const{ |
shuhei2306 | 0:28d0b65a60fe | 110 | return pow(x*x+y*y+z*z,0.5f); |
shuhei2306 | 0:28d0b65a60fe | 111 | } |
shuhei2306 | 0:28d0b65a60fe | 112 | //正規化 |
shuhei2306 | 0:28d0b65a60fe | 113 | inline void Vector3D::normalize(){ |
shuhei2306 | 0:28d0b65a60fe | 114 | *this /= norm(); |
shuhei2306 | 0:28d0b65a60fe | 115 | } |
shuhei2306 | 0:28d0b65a60fe | 116 | //*----------------------グローバル関数の実装--------------------------*// |
shuhei2306 | 0:28d0b65a60fe | 117 | //二項演算子の定義 |
shuhei2306 | 0:28d0b65a60fe | 118 | //Vector3D+Vector3D |
shuhei2306 | 0:28d0b65a60fe | 119 | inline Vector3D operator+(const Vector3D& u,const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 120 | Vector3D w; |
shuhei2306 | 0:28d0b65a60fe | 121 | w.x=u.x+v.x; |
shuhei2306 | 0:28d0b65a60fe | 122 | w.y=u.y+v.y; |
shuhei2306 | 0:28d0b65a60fe | 123 | w.z=u.z+v.z; |
shuhei2306 | 0:28d0b65a60fe | 124 | return w; |
shuhei2306 | 0:28d0b65a60fe | 125 | } |
shuhei2306 | 0:28d0b65a60fe | 126 | //Vector3D-Vector3D |
shuhei2306 | 0:28d0b65a60fe | 127 | inline Vector3D operator-(const Vector3D& u,const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 128 | Vector3D w; |
shuhei2306 | 0:28d0b65a60fe | 129 | w.x=u.x-v.x; |
shuhei2306 | 0:28d0b65a60fe | 130 | w.y=u.y-v.y; |
shuhei2306 | 0:28d0b65a60fe | 131 | w.z=u.z-v.z; |
shuhei2306 | 0:28d0b65a60fe | 132 | return w; |
shuhei2306 | 0:28d0b65a60fe | 133 | } |
shuhei2306 | 0:28d0b65a60fe | 134 | //float*Vector3D |
shuhei2306 | 0:28d0b65a60fe | 135 | inline Vector3D operator*(float k,const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 136 | return Vector3D(k*v.x,k*v.y,k*v.z); |
shuhei2306 | 0:28d0b65a60fe | 137 | } |
shuhei2306 | 0:28d0b65a60fe | 138 | //Vector3D*float |
shuhei2306 | 0:28d0b65a60fe | 139 | inline Vector3D operator*(const Vector3D& v,float k){ |
shuhei2306 | 0:28d0b65a60fe | 140 | return Vector3D(v.x*k,v.y*k,v.z*k); |
shuhei2306 | 0:28d0b65a60fe | 141 | } |
shuhei2306 | 0:28d0b65a60fe | 142 | //Vector3D/float |
shuhei2306 | 0:28d0b65a60fe | 143 | inline Vector3D operator/(const Vector3D& v,float k){ |
shuhei2306 | 0:28d0b65a60fe | 144 | return Vector3D(v.x/k,v.y/k,v.z/k); |
shuhei2306 | 0:28d0b65a60fe | 145 | } |
shuhei2306 | 0:28d0b65a60fe | 146 | //内積 Vector3D*Vector3D |
shuhei2306 | 0:28d0b65a60fe | 147 | inline float operator*(const Vector3D& u,const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 148 | return u.x*v.x+u.y*v.y+u.z*v.z; |
shuhei2306 | 0:28d0b65a60fe | 149 | } |
shuhei2306 | 0:28d0b65a60fe | 150 | //外積 Vector3D%Vector3D |
shuhei2306 | 0:28d0b65a60fe | 151 | inline Vector3D operator%(const Vector3D& u,const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 152 | Vector3D w; |
shuhei2306 | 0:28d0b65a60fe | 153 | w.x=u.y*v.z-u.z*v.y; |
shuhei2306 | 0:28d0b65a60fe | 154 | w.y=u.z*v.x-u.x*v.z; |
shuhei2306 | 0:28d0b65a60fe | 155 | w.z=u.x*v.y-u.y*v.x; |
shuhei2306 | 0:28d0b65a60fe | 156 | return w; |
shuhei2306 | 0:28d0b65a60fe | 157 | } |
shuhei2306 | 0:28d0b65a60fe | 158 | //画面への表示 |
shuhei2306 | 0:28d0b65a60fe | 159 | #include <iostream> |
shuhei2306 | 0:28d0b65a60fe | 160 | inline std::ostream& operator<<(std::ostream& s, const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 161 | return s <<'('<<v.x<<","<<v.y<<","<<v.z<<')'; |
shuhei2306 | 0:28d0b65a60fe | 162 | } |
shuhei2306 | 0:28d0b65a60fe | 163 | |
shuhei2306 | 0:28d0b65a60fe | 164 | #define PI 3.1415926535 |
shuhei2306 | 0:28d0b65a60fe | 165 | //2つのベクトルのなす角 |
shuhei2306 | 0:28d0b65a60fe | 166 | inline float angle(const Vector3D& u,const Vector3D& v){ |
shuhei2306 | 0:28d0b65a60fe | 167 | float cos =u*v/(u.norm()*v.norm()); |
shuhei2306 | 0:28d0b65a60fe | 168 | return float(acos(cos)/PI*180); |
shuhei2306 | 0:28d0b65a60fe | 169 | } |