Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef VECTOR_H_
- #define VECTOR_H_
- #include <math.h>
- #define M_PI 3.14159265
- // 3D Vektor
- struct vec3{
- float x, y, z;
- vec3(float X, float Y, float Z):
- x(X),
- y(Y),
- z(Z){}
- vec3():
- x(0.0f),
- y(0.0f),
- z(0.0f){}
- vec3(float f):
- x(f),
- y(f),
- z(f){}
- vec3(const vec3 &v):
- x(v.x),
- y(v.y),
- z(v.z){}
- vec3(float a, float b): // Vektor aus Winkeln
- x(float(sin(a))),
- y(float(tan((b-(floor(b/M_PI))*M_PI-M_PI/2)))),
- z(float(cos(a))){}
- float operator[](const int v){
- if(v == 0){
- return x;
- }
- else if(v == 1){
- return y;
- }
- else{
- return z;
- }
- }
- vec3 operator=(const vec3 v){
- x = v.x;
- y = v.y;
- z = v.z;
- return *this;
- }
- vec3 operator+(const vec3 v){
- return vec3(x+v.x, y+v.y, z+v.z);
- }
- vec3 operator+(const float s){
- return vec3(x+s, y+s, z+s);
- }
- vec3 operator-(const vec3 v){
- return vec3(x-v.x, y-v.y, z-v.z);
- }
- vec3 operator-(const float s){
- return vec3(x-s, y-s, z-s);
- }
- vec3 operator*(const vec3 &v){
- return vec3(x*v.x, y*v.y, z*v.z);
- }
- vec3 operator*(const float s){
- return vec3(x*s, y*s, z*s);
- }
- vec3 operator/(const vec3 &v){
- return vec3(x/v.x, y/v.y, z/v.z);
- }
- vec3 operator/(const float s){
- return vec3(x/s, y/s, z/s);
- }
- vec3 operator+=(const vec3 v){
- x += v.x;
- y += v.y;
- z += v.z;
- return *this;
- }
- vec3 operator+=(const float s){
- x += s;
- y += s;
- z += s;
- return *this;
- }
- vec3 operator-=(const vec3 v){
- x -= v.x;
- y -= v.y;
- z -= v.z;
- return *this;
- }
- vec3 operator-=(const float s){
- x -= s;
- y -= s;
- z -= s;
- return *this;
- }
- vec3 operator*=(const vec3 v){
- x *= v.x;
- y *= v.y;
- z *= v.z;
- return *this;
- }
- vec3 operator*=(const float s){
- x *= s;
- y *= s;
- z *= s;
- return *this;
- }
- vec3 operator/=(const vec3 v){
- x /= v.x;
- y /= v.y;
- z /= v.z;
- return *this;
- }
- vec3 operator/=(const float s){
- x /= s;
- y /= s;
- z /= s;
- return *this;
- }
- float length(){
- return sqrtf(dot());
- }
- float dot(){
- return x*x+y*y+z*z;
- }
- float dot(const vec3 v){
- return x*v.x+y*v.y+z*v.z;
- }
- void normalize(){
- float l = length();
- x /= l;
- y /= l;
- z /= l;
- }
- vec3 cross(const vec3 &v){
- x = (y*v.z)-(z*v.y);
- y = (z*v.x)-(x*v.z);
- z = (x*v.y)-(y*v.x);
- return *this;
- }
- vec3 cross(const vec3 &a, const vec3 &b){
- x = (a.y*b.z)-(a.z*b.y);
- y = (a.z*b.x)-(a.x*b.z);
- z = (a.x*b.y)-(a.y*b.x);
- return *this;
- }
- };
- // 2D Vektor
- struct vec2{
- float x, y;
- vec2(float x, float y):
- x(x),
- y(y){}
- vec2():
- x(0.0f),
- y(0.0f){}
- vec2(float f):
- x(f),
- y(f){}
- vec2(const vec2 &v):
- x(v.x),
- y(v.y){}
- float operator[](const int v){
- if(v == 0){
- return x;
- }
- else{
- return y;
- }
- }
- vec2 operator=(const vec2 v){
- if (this == &v) {
- return *this;
- }
- x = v.x;
- y = v.y;
- return *this;
- }
- vec2 operator+(const vec2 v){
- return vec2(x+v.x, y+v.y);
- }
- vec2 operator+(const float s){
- return vec2(x+s, y+s);
- }
- vec2 operator-(const vec2 v){
- return vec2(x-v.x, y-v.y);
- }
- vec2 operator-(const float s){
- return vec2(x-s, y-s);
- }
- vec2 operator*(const vec2 &v){
- return vec2(x*v.x, y*v.y);
- }
- vec2 operator*(const float s){
- return vec2(x*s, y*s);
- }
- vec2 operator/(const vec2 &v){
- return vec2(x/v.x, y/v.y);
- }
- vec2 operator/(const float s){
- return vec2(x/s, y/s);
- }
- vec2 operator+=(const vec2 v){
- x += v.x;
- y += v.y;
- return *this;
- }
- vec2 operator+=(const float s){
- x += s;
- y += s;
- return *this;
- }
- vec2 operator-=(const vec2 v){
- x -= v.x;
- y -= v.y;
- return *this;
- }
- vec2 operator-=(const float s){
- x -= s;
- y -= s;
- return *this;
- }
- vec2 operator*=(const vec2 v){
- x *= v.x;
- y *= v.y;
- return *this;
- }
- vec2 operator*=(const float s){
- x *= s;
- y *= s;
- return *this;
- }
- vec2 operator/=(const vec2 v){
- x /= v.x;
- y /= v.y;
- return *this;
- }
- vec2 operator/=(const float s){
- x /= s;
- y /= s;
- return *this;
- }
- float length(){
- return sqrtf(dot());
- }
- float dot(){
- return x*x+y*y;
- }
- float dot(const vec2 v){
- return x*v.x+y*v.y;
- }
- void normalize(){
- float l = length();
- x /= l;
- y /= l;
- }
- };
- // Vektorfunktionen
- float vec3Length(vec3 v);
- float vec3Dot(vec3 v);
- float vec3Dot(const vec3 a, const vec3 b);
- vec3 vec3Normalize(vec3 v);
- vec3 vec3Cross(const vec3 a, const vec3 b);
- vec3 vec3Abs(vec3 v);
- bool vec3HaveEqualComponent(const vec3 a, const vec3 b);
- float vec3Max(vec3 v);
- float vec2Dot(const vec2 v);
- float vec2Dot(const vec2 a, const vec2 b);
- float vec2Length(vec2 v);
- vec2 vec2Normalize(vec2 v);
- vec2 vec2Abs(vec2 v);
- bool vec2HaveEqualComponent(const vec2 a, const vec2 b);
- float vec2Max(vec2 v);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement