#pragma once #include "base.h" class Point { public: double _x; double _y; double _z; Point() { Zero(); } Point(const Point& p) { *this = p; } Point(double x, double y, double z) { Set(x,y,z); } void Zero() { _x = 0.0; _y = 0.0; _z = 0.0; } void Set(double x, double y, double z) { _x = x; _y = y; _z = z; } double Dot() { return _x*_x + _y*_y + _z*_z; } double Dot(const Point& p) { return p._x*_x + p._y*_y + p._z*_z; } void operator=(const Point& p) { _x = p._x; _y = p._y; _z = p._z; } void operator*=(double a) { _x *= a; _y *= a; _z *= a; } void operator+=(const Point& p) { _x += p._x; _y += p._y; _z += p._z; } void operator-=(const Point& p) { _x -= p._x; _y -= p._y; _z -= p._z; } void PlusAB(double a, const Point& b) { _x += a * b._x; _y += a * b._y; _z += a * b._z; } std::string ToString() { std::ostringstream s; s << "(" << _x << "," << _y << "," << _z << ")"; return s.str(); } static void UnitTest() { Point p; // default initialize to zero, test reading _x, _y, _z ASSERTFG(p._x, 0.0); ASSERTFG(p._y, 0.0); ASSERTFG(p._z, 0.0); // alternative initialization Point q(1.0, 2.0, 3.0); ASSERTFG(q._x, 1.0); ASSERTFG(q._y, 2.0); ASSERTFG(q._z, 3.0); // 1+4+9=14, test dot products ASSERTFG(q.Dot(), 14.0); ASSERTFG(q.Dot(q), 14.0); ASSERTFG(q.Dot(p), 0.0); p = q; ASSERTFG(q.Dot(p), 14.0); // test p.Zero() p.Zero(); ASSERTFG(p._x, 0.0); ASSERTFG(p._y, 0.0); ASSERTFG(p._z, 0.0); // test Set p.Set(0.0, 1.0, 0.0); ASSERTFG(p._x, 0.0); ASSERTFG(p._y, 1.0); ASSERTFG(p._z, 0.0); ASSERTFG(q.Dot(p), 2.0); // test += q += q; ASSERTFG(q._x, 2.0); ASSERTFG(q._y, 4.0); ASSERTFG(q._z, 6.0); // test *= q *= 0.5; ASSERTFG(q._x, 1.0); ASSERTFG(q._y, 2.0); ASSERTFG(q._z, 3.0); // test -= q -= p; ASSERTFG(q._y, 1.0); // test PlusAB q.PlusAB(4.0, p); ASSERTFG(p._y, 1.0); ASSERTFG(q._x, 1.0); ASSERTFG(q._y, 5.0); ASSERTFG(q._z, 3.0); // test that this is not integer arithmetic q.PlusAB(-0.25, q); ASSERTFG(q._x, 0.75); ASSERTFG(q._y, 3.75); ASSERTFG(q._z, 2.25); printf("unit tested Point\n"); } };