Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 KB | None | 0 0
  1. #ifndef VECTOR_H_
  2. #define VECTOR_H_
  3.  
  4. #include <math.h>
  5.  
  6. #define M_PI 3.14159265
  7.  
  8. // 3D Vektor
  9. struct vec3{
  10.     float x, y, z;
  11.  
  12.     vec3(float X, float Y, float Z):
  13.         x(X),
  14.         y(Y),
  15.         z(Z){}
  16.  
  17.     vec3():
  18.         x(0.0f),
  19.         y(0.0f),
  20.         z(0.0f){}
  21.  
  22.     vec3(float f):
  23.         x(f),
  24.         y(f),
  25.         z(f){}
  26.  
  27.     vec3(const vec3 &v):
  28.         x(v.x),
  29.         y(v.y),
  30.         z(v.z){}
  31.  
  32.     vec3(float a, float b): // Vektor aus Winkeln
  33.         x(float(sin(a))),
  34.         y(float(tan((b-(floor(b/M_PI))*M_PI-M_PI/2)))),
  35.         z(float(cos(a))){}
  36.  
  37.     float operator[](const int v){
  38.         if(v == 0){
  39.             return x;
  40.         }
  41.         else if(v == 1){
  42.             return y;
  43.         }
  44.         else{
  45.             return z;
  46.         }
  47.     }
  48.    
  49.     vec3 operator=(const vec3 v){
  50.         x = v.x;
  51.         y = v.y;
  52.         z = v.z;
  53.  
  54.         return *this;
  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.         return vec3(x/v.x, y/v.y, z/v.z);
  83.     }
  84.  
  85.     vec3 operator/(const float s){
  86.         return vec3(x/s, y/s, z/s);
  87.     }
  88.    
  89.     vec3 operator+=(const vec3 v){
  90.         x += v.x;
  91.         y += v.y;
  92.         z += v.z;
  93.  
  94.         return *this;
  95.     }
  96.    
  97.     vec3 operator+=(const float s){
  98.         x += s;
  99.         y += s;
  100.         z += s;
  101.  
  102.         return *this;
  103.     }
  104.  
  105.     vec3 operator-=(const vec3 v){
  106.         x -= v.x;
  107.         y -= v.y;
  108.         z -= v.z;
  109.  
  110.         return *this;
  111.     }
  112.  
  113.     vec3 operator-=(const float s){
  114.         x -= s;
  115.         y -= s;
  116.         z -= s;
  117.  
  118.         return *this;
  119.     }
  120.  
  121.     vec3 operator*=(const vec3 v){
  122.         x *= v.x;
  123.         y *= v.y;
  124.         z *= v.z;
  125.  
  126.         return *this;
  127.     }
  128.    
  129.     vec3 operator*=(const float s){
  130.         x *= s;
  131.         y *= s;
  132.         z *= s;
  133.  
  134.         return *this;
  135.     }
  136.  
  137.     vec3 operator/=(const vec3 v){
  138.         x /= v.x;
  139.         y /= v.y;
  140.         z /= v.z;
  141.  
  142.         return *this;
  143.     }
  144.  
  145.     vec3 operator/=(const float s){
  146.         x /= s;
  147.         y /= s;
  148.         z /= s;
  149.  
  150.         return *this;
  151.     }
  152.  
  153.     float length(){
  154.         return sqrtf(dot());
  155.     }
  156.  
  157.     float dot(){
  158.         return x*x+y*y+z*z;
  159.     }
  160.  
  161.     float dot(const vec3 v){
  162.         return x*v.x+y*v.y+z*v.z;
  163.     }
  164.  
  165.     void normalize(){
  166.         float l = length();
  167.        
  168.         x /= l;
  169.         y /= l;
  170.         z /= l;
  171.     }
  172.  
  173.     vec3 cross(const vec3 &v){
  174.         x = (y*v.z)-(z*v.y);
  175.         y = (z*v.x)-(x*v.z);
  176.         z = (x*v.y)-(y*v.x);
  177.  
  178.         return *this;
  179.     }
  180.  
  181.     vec3 cross(const vec3 &a, const vec3 &b){
  182.         x = (a.y*b.z)-(a.z*b.y);
  183.         y = (a.z*b.x)-(a.x*b.z);
  184.         z = (a.x*b.y)-(a.y*b.x);
  185.  
  186.         return *this;
  187.     }
  188. };
  189.  
  190. // 2D Vektor
  191. struct vec2{
  192.     float x, y;
  193.  
  194.     vec2(float x, float y):
  195.         x(x),
  196.         y(y){}
  197.  
  198.     vec2():
  199.         x(0.0f),
  200.         y(0.0f){}
  201.  
  202.     vec2(float f):
  203.         x(f),
  204.         y(f){}
  205.  
  206.     vec2(const vec2 &v):
  207.         x(v.x),
  208.         y(v.y){}
  209.  
  210.     float operator[](const int v){
  211.         if(v == 0){
  212.             return x;
  213.         }
  214.         else{
  215.             return y;
  216.         }
  217.     }
  218.  
  219.     vec2 operator=(const vec2 v){
  220.         if (this == &v) {
  221.             return *this;
  222.         }
  223.  
  224.         x = v.x;
  225.         y = v.y;
  226.  
  227.         return *this;
  228.     }
  229.    
  230.     vec2 operator+(const vec2 v){
  231.         return vec2(x+v.x, y+v.y);
  232.     }
  233.  
  234.     vec2 operator+(const float s){
  235.         return vec2(x+s, y+s);
  236.     }
  237.  
  238.     vec2 operator-(const vec2 v){
  239.         return vec2(x-v.x, y-v.y);
  240.     }
  241.  
  242.     vec2 operator-(const float s){
  243.         return vec2(x-s, y-s);
  244.     }
  245.    
  246.     vec2 operator*(const vec2 &v){
  247.         return vec2(x*v.x, y*v.y);
  248.     }
  249.  
  250.     vec2 operator*(const float s){
  251.         return vec2(x*s, y*s);
  252.     }
  253.    
  254.     vec2 operator/(const vec2 &v){
  255.         return vec2(x/v.x, y/v.y);
  256.     }
  257.  
  258.     vec2 operator/(const float s){
  259.         return vec2(x/s, y/s);
  260.     }    
  261.  
  262.     vec2 operator+=(const vec2 v){
  263.         x += v.x;
  264.         y += v.y;
  265.  
  266.         return *this;
  267.     }
  268.    
  269.     vec2 operator+=(const float s){
  270.         x += s;
  271.         y += s;
  272.  
  273.         return *this;
  274.     }
  275.  
  276.     vec2 operator-=(const vec2 v){
  277.         x -= v.x;
  278.         y -= v.y;
  279.  
  280.         return *this;
  281.     }
  282.  
  283.     vec2 operator-=(const float s){
  284.         x -= s;
  285.         y -= s;
  286.  
  287.         return *this;
  288.     }
  289.    
  290.     vec2 operator*=(const vec2 v){
  291.         x *= v.x;
  292.         y *= v.y;
  293.  
  294.         return *this;
  295.     }
  296.    
  297.     vec2 operator*=(const float s){
  298.         x *= s;
  299.         y *= s;
  300.  
  301.         return *this;
  302.     }
  303.  
  304.     vec2 operator/=(const vec2 v){
  305.         x /= v.x;
  306.         y /= v.y;
  307.  
  308.         return *this;
  309.     }  
  310.  
  311.     vec2 operator/=(const float s){
  312.         x /= s;
  313.         y /= s;
  314.  
  315.         return *this;
  316.     }
  317.  
  318.     float length(){
  319.         return sqrtf(dot());
  320.     }
  321.  
  322.     float dot(){
  323.         return x*x+y*y;
  324.     }
  325.  
  326.     float dot(const vec2 v){
  327.         return x*v.x+y*v.y;
  328.     }
  329.  
  330.     void normalize(){
  331.         float l = length();
  332.        
  333.         x /= l;
  334.         y /= l;
  335.     }
  336. };
  337.  
  338. // Vektorfunktionen
  339. float vec3Length(vec3 v);
  340. float vec3Dot(vec3 v);
  341. float vec3Dot(const vec3 a, const vec3 b);
  342. vec3 vec3Normalize(vec3 v);
  343. vec3 vec3Cross(const vec3 a, const vec3 b);
  344. vec3 vec3Abs(vec3 v);
  345. bool vec3HaveEqualComponent(const vec3 a, const vec3 b);
  346. float vec3Max(vec3 v);
  347.  
  348. float vec2Dot(const vec2 v);
  349. float vec2Dot(const vec2 a, const vec2 b);
  350. float vec2Length(vec2 v);
  351. vec2 vec2Normalize(vec2 v);
  352. vec2 vec2Abs(vec2 v);
  353. bool vec2HaveEqualComponent(const vec2 a, const vec2 b);
  354. float vec2Max(vec2 v);
  355.  
  356. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement