diff --git a/.vs/3dMath/v15/.suo b/.vs/3dMath/v15/.suo index e36491f..521fac9 100644 Binary files a/.vs/3dMath/v15/.suo and b/.vs/3dMath/v15/.suo differ diff --git a/GMath.cpp b/GMath.cpp index ff5810d..4264ba8 100644 --- a/GMath.cpp +++ b/GMath.cpp @@ -1095,4 +1095,40 @@ float dist(const GPoint3 & q, const GLine & l) GVector3 u = q - l.p; GVector3 p = proj(u, l.v); return norm(u - p); -} \ No newline at end of file +} + +GPlane::GPlane(const GVector3 & _n, const GPoint3 & _p) +{ + n = _n; + d = -n * _p; +} + +GPlane::GPlane(const GPoint3 & p1, const GPoint3 & p2, const GPoint3 & p3) +{ + n = (p2 - p1) ^ (p3 - p1); + d = -n * p1; //是平面上的一点点乘法线再取负号 +} + +GPlane::GPlane(const float & a, const float & b, const float & c, const float & d) +{ + n = GVector3(a, b, c); + this->d = d; +} + +GPlane::GPlane(const GPlane & copy) +{ + n = copy.n; + d = copy.d; +} + +GPlane& GPlane::operator=(const GPlane & rhs) +{ + n = rhs.n; + d = rhs.d; + return *this; +} + +GVector3 GPlane::GetNormal() const +{ + return n; +} diff --git a/GMath.h b/GMath.h index 8d7efd2..72a6879 100644 --- a/GMath.h +++ b/GMath.h @@ -21,6 +21,7 @@ using namespace std; class GVector3; class GVector; class GMatrix; +class GPlane; typedef GVector3 GPoint3; class GVector3 { @@ -225,4 +226,19 @@ public: bool IsOnLine(const GPoint3& q); friend float dist(const GPoint3& q, const GLine& l); +}; + +class GPlane { +private: + //ax+by+cz+d=0 a b c 其实就是法线,d是平面上的一点点乘法线再取负号 + GVector3 n; + float d; +public: + GPlane(const GVector3& _n = GVector3(0.0f, 0.0f, 0.0f), const GPoint3& _p = GVector3(0.0f, 0.0f, 0.0f)); + GPlane(const GPoint3& p1, const GPoint3& p2, const GPoint3& p3); + GPlane(const float& a, const float& b, const float&c, const float& d); + GPlane(const GPlane& copy); + + GPlane& operator =(const GPlane& rhs); + GVector3 GetNormal() const; }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index cad1ad6..055bc8f 100644 --- a/main.cpp +++ b/main.cpp @@ -2,15 +2,11 @@ int main() { - GLine l(GPoint3(0.0f, 0.0f, 0.0f), GVector3(1.0f, 0.0f, 0.0f)); - GPoint3 q(0.0f, 0.0f, 1.0f); + GPlane PI1(GVector3(0.0f, 0.0f, 1.0f), GPoint3(0.0f, 0.0f, 0.0f)); + GPlane PI2(GPoint3(1.0,0.0f,0.0f), GPoint3(0.0, 1.0f, 0.0f), GPoint3(0.0, 0.0f, 1.0f)); - float d = dist(q, l); + GPlane PI = PI1; + cout << PI.GetNormal() << endl; - cout << d << endl; - - bool b = l.IsOnLine(GPoint3(0.0f, 0.0f, 0.0f)); - bool b2 = l.IsOnLine(GPoint3(0.5f, 0.0f, 0.0f)); - bool b3 = l.IsOnLine(GPoint3(0.5f, 0.5f, 0.0f)); return 0; } \ No newline at end of file