Advertisement
Guest User

Untitled

a guest
Aug 13th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. struct vec3{
  2.     float x, y, z;
  3.  
  4.     vec3(float X, float Y, float Z):
  5.         x(X),
  6.         y(Y),
  7.         z(Z){}
  8.  
  9.     vec3():
  10.         x(0.0f),
  11.         y(0.0f),
  12.         z(0.0f){}
  13.  
  14.     vec3(float f):
  15.         x(f),
  16.         y(f),
  17.         z(f){}
  18.  
  19.     vec3(const vec3 &v):
  20.         x(v.x),
  21.         y(v.y),
  22.         z(v.z){}
  23.  
  24.     vec3(float a, float b): // Vektor aus Winkeln
  25.         x(float(sin(a))),
  26.         y(float(tan((b-(floor(b/M_PI))*M_PI-M_PI/2)))),
  27.         z(float(cos(a))){}
  28.  
  29.     float operator[](const int v){
  30.         if(v == 0){
  31.             return x;
  32.         }
  33.         else if(v == 1){
  34.             return y;
  35.         }
  36.         else{
  37.             return z;
  38.         }
  39.     }
  40.    
  41.     vec3 operator=(const vec3 v){
  42.         x = v.x;
  43.         y = v.y;
  44.         z = v.z;
  45.  
  46.         return *this;
  47.     }
  48.    
  49.     vec3 operator+(const vec3 v){
  50.         return vec3(x+v.x, y+v.y, z+v.z);
  51.     }
  52.  
  53.     vec3 operator+(const float s){
  54.         return vec3(x+s, y+s, z+s);
  55.     }
  56.  
  57.     vec3 operator-(const vec3 v){
  58.         return vec3(x-v.x, y-v.y, z-v.z);
  59.     }
  60.  
  61.     vec3 operator-(const float s){
  62.         return vec3(x-s, y-s, z-s);
  63.     }
  64.  
  65.     vec3 operator*(const vec3 &v){
  66.         return vec3(x*v.x, y*v.y, z*v.z);
  67.     }
  68.  
  69.     vec3 operator*(const float s){
  70.         return vec3(x*s, y*s, z*s);
  71.     }
  72.    
  73.     vec3 operator/(const vec3 &v){
  74.         return vec3(x/v.x, y/v.y, z/v.z);
  75.     }
  76.  
  77.     vec3 operator/(const float s){
  78.         return vec3(x/s, y/s, z/s);
  79.     }
  80.    
  81.     vec3 operator+=(const vec3 v){
  82.         x += v.x;
  83.         y += v.y;
  84.         z += v.z;
  85.  
  86.         return *this;
  87.     }
  88.    
  89.     vec3 operator+=(const float s){
  90.         x += s;
  91.         y += s;
  92.         z += s;
  93.  
  94.         return *this;
  95.     }
  96.  
  97.     vec3 operator-=(const vec3 v){
  98.         x -= v.x;
  99.         y -= v.y;
  100.         z -= v.z;
  101.  
  102.         return *this;
  103.     }
  104.  
  105.     vec3 operator-=(const float s){
  106.         x -= s;
  107.         y -= s;
  108.         z -= s;
  109.  
  110.         return *this;
  111.     }
  112.  
  113.     vec3 operator*=(const vec3 v){
  114.         x *= v.x;
  115.         y *= v.y;
  116.         z *= v.z;
  117.  
  118.         return *this;
  119.     }
  120.    
  121.     vec3 operator*=(const float s){
  122.         x *= s;
  123.         y *= s;
  124.         z *= s;
  125.  
  126.         return *this;
  127.     }
  128.  
  129.     vec3 operator/=(const vec3 v){
  130.         x /= v.x;
  131.         y /= v.y;
  132.         z /= v.z;
  133.  
  134.         return *this;
  135.     }
  136.  
  137.     vec3 operator/=(const float s){
  138.         x /= s;
  139.         y /= s;
  140.         z /= s;
  141.  
  142.         return *this;
  143.     }
  144.  
  145.     float length(){
  146.         return sqrtf(dot());
  147.     }
  148.  
  149.     float dot(){
  150.         return x*x+y*y+z*z;
  151.     }
  152.  
  153.     float dot(const vec3 v){
  154.         return x*v.x+y*v.y+z*v.z;
  155.     }
  156.  
  157.     void normalize(){
  158.         float l = length();
  159.        
  160.         x /= l;
  161.         y /= l;
  162.         z /= l;
  163.     }
  164.  
  165.     vec3 cross(const vec3 &v){
  166.         x = (y*v.z)-(z*v.y);
  167.         y = (z*v.x)-(x*v.z);
  168.         z = (x*v.y)-(y*v.x);
  169.  
  170.         return *this;
  171.     }
  172.  
  173.     vec3 cross(const vec3 &a, const vec3 &b){
  174.         x = (a.y*b.z)-(a.z*b.y);
  175.         y = (a.z*b.x)-(a.x*b.z);
  176.         z = (a.x*b.y)-(a.y*b.x);
  177.  
  178.         return *this;
  179.     }
  180. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement