diff --git a/.vs/3dMath/v15/.suo b/.vs/3dMath/v15/.suo index 521fac9..ed7b394 100644 Binary files a/.vs/3dMath/v15/.suo and b/.vs/3dMath/v15/.suo differ diff --git a/GMath.cpp b/GMath.cpp index 4264ba8..dc2810f 100644 --- a/GMath.cpp +++ b/GMath.cpp @@ -1132,3 +1132,24 @@ GVector3 GPlane::GetNormal() const { return n; } + +float dist(const GPlane & pi, const GPoint3 & p) +{ + float D; + D = (p*pi.n + pi.d) / norm(pi.n); + return D; +} + +bool intersect_line_plane(GPoint3 & p, const GLine & l, const GPlane & pi) +{ + //如果直线的方向和平面的法线垂直,则直线和平面平行,没有交点 + if (EQ_ZERO((l.v*pi.n), PRECISION)) { + return false; + } + + float t = -(l.p*pi.n + pi.d) / (l.v * pi.n); + + p = l(t); + + return true; +} diff --git a/GMath.h b/GMath.h index 72a6879..102ed3c 100644 --- a/GMath.h +++ b/GMath.h @@ -226,6 +226,8 @@ public: bool IsOnLine(const GPoint3& q); friend float dist(const GPoint3& q, const GLine& l); + + friend bool intersect_line_plane(GPoint3& p, const GLine& l, const GPlane& pi); }; class GPlane { @@ -241,4 +243,8 @@ public: GPlane& operator =(const GPlane& rhs); GVector3 GetNormal() const; + + friend float dist(const GPlane& pi, const GPoint3& p); + + friend bool intersect_line_plane(GPoint3& p, const GLine& l, const GPlane& pi); }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 055bc8f..7fe3e78 100644 --- a/main.cpp +++ b/main.cpp @@ -2,11 +2,14 @@ int main() { - 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)); + GPlane PI(1.0f, 1.0f, 1.0f, -1.0); + GLine l(GPoint3(0.0f, 0.0f, 0.0f), GVector3(1.0f, 1.0f, 1.0f)); - GPlane PI = PI1; - cout << PI.GetNormal() << endl; + GPoint3 p; + + bool ret = intersect_line_plane(p, l, PI); + + cout << p << endl; return 0; } \ No newline at end of file