208 lines
3.4 KiB
C++
208 lines
3.4 KiB
C++
#include "punkt3d.h"
|
||
|
||
Punkt3D::Punkt3D() {
|
||
x = y = z = 0.0f;
|
||
}
|
||
|
||
Punkt3D::Punkt3D(float _x, float _y, float _z) {
|
||
set(_x, _y, _z);
|
||
}
|
||
|
||
void Punkt3D::set(float _x, float _y, float _z) {
|
||
x = _x;
|
||
y = _y;
|
||
z = _z;
|
||
}
|
||
|
||
float Punkt3D::abs() {
|
||
return sqrt(x*x+y*y+z*z);
|
||
}
|
||
|
||
Punkt3D Punkt3D::kreuzprodukt(const Punkt3D &b) {
|
||
Punkt3D erg;
|
||
erg.x = y*b.z - z*b.y;
|
||
erg.y = z*b.x - x*b.z;
|
||
erg.z = x*b.y - y*b.x;
|
||
|
||
return erg;
|
||
}
|
||
|
||
void Punkt3D::normalize() {
|
||
float a = abs();
|
||
x /= a;
|
||
y /= a;
|
||
z /= a;
|
||
}
|
||
|
||
Punkt3D Punkt3D::getNormalized() {
|
||
Punkt3D ret(*this);
|
||
ret.normalize();
|
||
return ret;
|
||
}
|
||
|
||
bool Punkt3D::isNormalized() {
|
||
return (abs()==1);
|
||
}
|
||
|
||
float Punkt3D::calcAngle(Punkt3D b) {
|
||
if(abs()*b.abs()==0.0f)
|
||
return 0.0f;
|
||
return rad2deg(std::acos(((*this)*b)/(abs()*b.abs())));
|
||
}
|
||
|
||
Punkt3D Punkt3D::getOrtographic() {
|
||
Punkt3D erg;
|
||
if(!x) {
|
||
erg.x = 0.0f;
|
||
erg.y = z;
|
||
erg.z = -y;
|
||
} else if(!y) {
|
||
erg.x = z;
|
||
erg.y = 0;
|
||
erg.z = -x;
|
||
} else {
|
||
// z, oder genereller fall
|
||
erg.x = y;
|
||
erg.y = -x;
|
||
erg.z = 0.0f;
|
||
}
|
||
return erg;
|
||
}
|
||
|
||
void Punkt3D::print(std::string coordname) {
|
||
if(coordname!="")
|
||
coordname.append(" ");
|
||
std::cout << coordname << "Coord: (" << x << ", " << y << ", " << z << ")" << std::endl;
|
||
}
|
||
|
||
|
||
|
||
Punkt3D Punkt3D::operator+(const Punkt3D &b) {
|
||
Punkt3D c;
|
||
c.x = x + b.x;
|
||
c.y = y + b.y;
|
||
c.z = z + b.z;
|
||
|
||
return c;
|
||
}
|
||
|
||
Punkt3D Punkt3D::operator-(const Punkt3D &b) {
|
||
Punkt3D c;
|
||
c.x = x - b.x;
|
||
c.y = y - b.y;
|
||
c.z = z - b.z;
|
||
|
||
return c;
|
||
}
|
||
|
||
Punkt3D& Punkt3D::operator+=(const Punkt3D &b) {
|
||
x += b.x;
|
||
y += b.y;
|
||
z += b.z;
|
||
|
||
return *this;
|
||
}
|
||
|
||
Punkt3D& Punkt3D::operator-=(const Punkt3D &b) {
|
||
x -= b.x;
|
||
y -= b.y;
|
||
z -= b.z;
|
||
|
||
return *this;
|
||
}
|
||
|
||
Punkt3D Punkt3D::operator+(const float &_m) {
|
||
return Punkt3D(x+_m, y+_m, z+_m);
|
||
}
|
||
|
||
Punkt3D Punkt3D::operator-(const float &_m) {
|
||
return Punkt3D(x-_m, y-_m, z-_m);
|
||
}
|
||
|
||
Punkt3D Punkt3D::operator*(const float &_m) {
|
||
return Punkt3D(x*_m, y*_m, z*_m);
|
||
}
|
||
|
||
Punkt3D Punkt3D::operator/(const float &_m) {
|
||
return Punkt3D(x/_m, y/_m, z/_m);
|
||
}
|
||
|
||
Punkt3D& Punkt3D::operator+=(const float &_m) {
|
||
x += _m;
|
||
y += _m;
|
||
z += _m;
|
||
return *this;
|
||
}
|
||
Punkt3D& Punkt3D::operator-=(const float &_m) {
|
||
x -= _m;
|
||
y -= _m;
|
||
z -= _m;
|
||
return *this;
|
||
}
|
||
Punkt3D& Punkt3D::operator*=(const float &_m) {
|
||
x *= _m;
|
||
y *= _m;
|
||
z *= _m;
|
||
return *this;
|
||
}
|
||
Punkt3D& Punkt3D::operator/=(const float &_m) {
|
||
x /= _m;
|
||
y /= _m;
|
||
z /= _m;
|
||
return *this;
|
||
}
|
||
|
||
float Punkt3D::operator*(const Punkt3D& _m) {
|
||
return x * _m.x + y * _m.y + z * _m.z;
|
||
}
|
||
|
||
Punkt3D Punkt3D::operator-() {
|
||
return Punkt3D(-x, -y, -z);
|
||
}
|
||
|
||
bool Punkt3D::operator==(const Punkt3D& b) {
|
||
return ( x==b.x && y==b.y && z==b.z);
|
||
}
|
||
bool Punkt3D::operator!=(const Punkt3D& b) {
|
||
return !(*this==b);
|
||
}
|
||
// Freunde
|
||
|
||
Punkt3D operator+(const float& _m, const Punkt3D& b) {
|
||
return Punkt3D(b.x+_m, b.y+_m, b.z+_m);
|
||
}
|
||
|
||
Punkt3D operator-(const float& _m, const Punkt3D& b) {
|
||
return Punkt3D(b.x-_m, b.y-_m, b.z-_m);
|
||
}
|
||
|
||
Punkt3D operator*(const float& _m, const Punkt3D& b) {
|
||
return Punkt3D(b.x*_m, b.y*_m, b.z*_m);
|
||
}
|
||
|
||
Punkt3D operator/(const float& _m, const Punkt3D& b) {
|
||
return Punkt3D(b.x/_m, b.y/_m, b.z/_m);
|
||
}
|
||
|
||
float abs(Punkt3D p) {
|
||
return p.abs();
|
||
}
|
||
|
||
// OpenGL Funktionen f<>r Punkt3D
|
||
|
||
void glVertex3f(Punkt3D p) {
|
||
glVertex3f(p.x, p.y, p.z);
|
||
}
|
||
|
||
void glTranslatef(Punkt3D p) {
|
||
glTranslatef(p.x, p.y, p.z);
|
||
}
|
||
|
||
void glNormal3f(Punkt3D p) {
|
||
glNormal3f(p.x, p.y, p.z);
|
||
}
|
||
|
||
void glRotatef(float deg, Punkt3D vec) {
|
||
glRotatef(deg, vec.x, vec.y, vec.z);
|
||
}
|