share

Dependencies:   RotationMat_ SDFileSystem mbed

Committer:
shuhei2306
Date:
Wed Feb 03 11:43:40 2016 +0000
Revision:
1:28cfbd0cc92e
Parent:
0:cbd607fc4caa
share

Who changed what in which revision?

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