diff options
Diffstat (limited to 'point3d.h')
-rw-r--r-- | point3d.h | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/point3d.h b/point3d.h new file mode 100644 index 0000000..aba6136 --- /dev/null +++ b/point3d.h @@ -0,0 +1,56 @@ +#ifndef POINT3D_H +#define POINT3D_H + +struct Point3d +{ + float x, y, z; + + Point3d() + : x(0) + , y(0) + , z(0) + { + } + + Point3d(float x_, float y_, float z_) + : x(x_) + , y(y_) + , z(z_) + { + } + + Point3d operator-(const Point3d &p) const + { + return Point3d(x - p.x, y - p.y, z - p.z); + } + + Point3d operator+(const Point3d &p) const + { + return Point3d(x + p.x, y + p.y, z + p.z); + } + + Point3d normalize() const + { + float r = 1. / sqrt(x * x + y * y + z * z); + return Point3d(x * r, y * r, z * r); + } + + Point3d operator*(float f) const + { + return Point3d(x * f, y * f, z * f); + } +}; + +inline float dot(const Point3d &a, const Point3d &b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} + +inline Point3d cross(const Point3d &a, const Point3d &b) +{ + return Point3d(a.y * b.z - a.z * b.y, + a.z * b.x - a.x * b.z, + a.x * b.y - a.y * b.x); +} + +#endif |